Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
CHANGED
|
@@ -12,7 +12,23 @@ def edge_detection(image, threshold1, threshold2):
|
|
| 12 |
edges = cv2.Canny(image, threshold1, threshold2)
|
| 13 |
return edges
|
| 14 |
|
|
|
|
| 15 |
def image_inpainting(image, mask):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
inpainted = cv2.inpaint(image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
| 17 |
return inpainted
|
| 18 |
|
|
@@ -48,11 +64,18 @@ def app_interface():
|
|
| 48 |
|
| 49 |
# 影像修復
|
| 50 |
with gr.Tab("影像修復"):
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
inpaint_output = gr.Image(label="修復後影像")
|
|
|
|
|
|
|
| 54 |
inpaint_button = gr.Button("執行修復")
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# 模板匹配
|
| 58 |
with gr.Tab("模板匹配"):
|
|
|
|
| 12 |
edges = cv2.Canny(image, threshold1, threshold2)
|
| 13 |
return edges
|
| 14 |
|
| 15 |
+
# 影像修復函數
|
| 16 |
def image_inpainting(image, mask):
|
| 17 |
+
"""
|
| 18 |
+
使用 OpenCV 的 inpaint 方法進行影像修復。
|
| 19 |
+
:param image: numpy array, 原始影像
|
| 20 |
+
:param mask: numpy array, 修復遮罩
|
| 21 |
+
:return: numpy array, 修復後影像
|
| 22 |
+
"""
|
| 23 |
+
# 確保影像和遮罩有相同的尺寸
|
| 24 |
+
if image.shape[:2] != mask.shape[:2]:
|
| 25 |
+
raise ValueError("影像和遮罩的尺寸不一致!")
|
| 26 |
+
|
| 27 |
+
# 將遮罩轉換為單通道(灰度)
|
| 28 |
+
if len(mask.shape) == 3:
|
| 29 |
+
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
|
| 31 |
+
# 修復影像
|
| 32 |
inpainted = cv2.inpaint(image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
| 33 |
return inpainted
|
| 34 |
|
|
|
|
| 64 |
|
| 65 |
# 影像修復
|
| 66 |
with gr.Tab("影像修復"):
|
| 67 |
+
# 輸入影像與遮罩
|
| 68 |
+
inpaint_input = gr.Image(label="上傳影像", type="numpy", tool="editor")
|
| 69 |
+
mask_input = gr.Image(label="上傳遮罩 (白色為修復區域)", type="numpy", tool="sketch")
|
| 70 |
+
|
| 71 |
+
# 輸出修復後影像
|
| 72 |
inpaint_output = gr.Image(label="修復後影像")
|
| 73 |
+
|
| 74 |
+
# 按鈕操作
|
| 75 |
inpaint_button = gr.Button("執行修復")
|
| 76 |
+
|
| 77 |
+
# 綁定影像修復函數到按鈕
|
| 78 |
+
inpaint_button.click(image_inpainting, inputs=[inpaint_input, mask_input], outputs=inpaint_output)
|
| 79 |
|
| 80 |
# 模板匹配
|
| 81 |
with gr.Tab("模板匹配"):
|