Update app.py
Browse files
app.py
CHANGED
|
@@ -21,21 +21,24 @@ def image_segmentation(image, compactness):
|
|
| 21 |
|
| 22 |
# 圖像修復函式
|
| 23 |
def inpaint_image(image, mask_threshold):
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 26 |
-
# 根據遮罩閾值生成遮罩
|
| 27 |
_, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
|
| 28 |
mask = mask.astype(bool)
|
| 29 |
|
| 30 |
-
# 檢查遮罩是否正確
|
| 31 |
if not mask.any():
|
| 32 |
raise ValueError("遮罩生成失敗,請調整遮罩閾值參數。")
|
| 33 |
|
| 34 |
-
# 使用 biharmonic 方法修復圖像
|
| 35 |
inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
|
| 36 |
return inpainted
|
| 37 |
|
| 38 |
|
|
|
|
| 39 |
# 主應用程式
|
| 40 |
def app():
|
| 41 |
with gr.Blocks() as demo:
|
|
|
|
| 21 |
|
| 22 |
# 圖像修復函式
|
| 23 |
def inpaint_image(image, mask_threshold):
|
| 24 |
+
# 確保圖片格式為 RGB
|
| 25 |
+
if image.shape[-1] == 4: # 若圖片有 alpha 通道
|
| 26 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 27 |
+
elif image.shape[-1] == 3:
|
| 28 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 29 |
+
|
| 30 |
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
|
|
|
| 31 |
_, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
|
| 32 |
mask = mask.astype(bool)
|
| 33 |
|
|
|
|
| 34 |
if not mask.any():
|
| 35 |
raise ValueError("遮罩生成失敗,請調整遮罩閾值參數。")
|
| 36 |
|
|
|
|
| 37 |
inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
|
| 38 |
return inpainted
|
| 39 |
|
| 40 |
|
| 41 |
+
|
| 42 |
# 主應用程式
|
| 43 |
def app():
|
| 44 |
with gr.Blocks() as demo:
|