Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -332,7 +332,48 @@ def convert_image_format(input_path, output_format="png"):
|
|
| 332 |
return output_path
|
| 333 |
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
|
| 338 |
#この下は、openCV
|
|
@@ -498,6 +539,18 @@ async def create_mask_and_inpaint_opencv(image: UploadFile = File(...), risk_lev
|
|
| 498 |
# OpenCVでインペイント
|
| 499 |
inpaint_image_with_mask(input_path, mask_path, output_path)
|
| 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
return FileResponse(output_path)
|
| 502 |
@app.post("/create-mask-and-inpaint-stamp")
|
| 503 |
async def create_mask_and_inpaint_opencv(image: UploadFile = File(...), risk_level: int = Form(...)):
|
|
@@ -736,7 +789,7 @@ async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(
|
|
| 736 |
output_path = f"./output_simple_lama_{timestamp}_{unique_id}.jpg"
|
| 737 |
|
| 738 |
# OpenCVでインペイント
|
| 739 |
-
|
| 740 |
|
| 741 |
return FileResponse(output_path)
|
| 742 |
|
|
@@ -831,8 +884,8 @@ async def read_root():
|
|
| 831 |
<select id="processingType" class="custom-select">
|
| 832 |
<option value="opencv">OpenCVインペイント</option>
|
| 833 |
<option value="simple_lama">Simple Lamaインペイント</option>
|
| 834 |
-
<option value="
|
| 835 |
-
<option value="
|
| 836 |
</select>
|
| 837 |
</div>
|
| 838 |
|
|
@@ -907,10 +960,10 @@ async def read_root():
|
|
| 907 |
apiEndpoint = "/create-mask-and-inpaint-opencv";
|
| 908 |
} else if (processingType === "simple_lama") {
|
| 909 |
apiEndpoint = "/create-mask-and-inpaint-simple-lama";
|
| 910 |
-
} else if (processingType === "
|
| 911 |
-
apiEndpoint = "/create-mask-and-inpaint-
|
| 912 |
-
} else if (processingType=="
|
| 913 |
-
apiEndpoint = "/create-mask-and-inpaint-
|
| 914 |
}
|
| 915 |
|
| 916 |
const url = "https://rein0421-aidentify.hf.space" + apiEndpoint;
|
|
|
|
| 332 |
return output_path
|
| 333 |
|
| 334 |
|
| 335 |
+
def mosaic_image_with_mask(image_path, mask_path, output_path, mosaic_level=15):
|
| 336 |
+
"""
|
| 337 |
+
マスク画像を使用して元画像の指定領域にモザイクをかける関数。
|
| 338 |
+
Parameters:
|
| 339 |
+
- image_path: 元画像のパス
|
| 340 |
+
- mask_path: モザイクをかけたい領域を白、その他を黒としたマスク画像のパス
|
| 341 |
+
- output_path: モザイク処理結果の出力パス
|
| 342 |
+
- mosaic_level: モザイクの強さ(値が大きいほど粗いモザイクになる)
|
| 343 |
+
Returns:
|
| 344 |
+
- output_path: モザイク処理された画像の出力パス
|
| 345 |
+
"""
|
| 346 |
+
# 画像とマスクを読み込み
|
| 347 |
+
image = cv2.imread(image_path)
|
| 348 |
+
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
|
| 349 |
+
|
| 350 |
+
# 画像とマスクの読み込みチェック
|
| 351 |
+
if image is None:
|
| 352 |
+
raise ValueError(f"元画像が見つかりません: {image_path}")
|
| 353 |
+
if mask is None:
|
| 354 |
+
raise ValueError(f"マスク画像が見つかりません: {mask_path}")
|
| 355 |
+
|
| 356 |
+
# マスク画像が元画像と同じサイズでない場合、リサイズ
|
| 357 |
+
if image.shape[:2] != mask.shape[:2]:
|
| 358 |
+
print(f"マスク画像のサイズを元画像に合わせてリサイズします: {mask.shape} -> {image.shape[:2]}")
|
| 359 |
+
mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
|
| 360 |
+
|
| 361 |
+
# モザイクをかける領域を抽出
|
| 362 |
+
mosaic_area = cv2.bitwise_and(image, image, mask=mask)
|
| 363 |
+
|
| 364 |
+
# モザイク処理
|
| 365 |
+
small = cv2.resize(mosaic_area, (image.shape[1] // mosaic_level, image.shape[0] // mosaic_level), interpolation=cv2.INTER_LINEAR)
|
| 366 |
+
mosaic = cv2.resize(small, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 367 |
|
| 368 |
+
# マスクを使って元画像にモザイク部分を合成
|
| 369 |
+
mosaic_result = cv2.bitwise_and(mosaic, mosaic, mask=mask)
|
| 370 |
+
image_no_mosaic = cv2.bitwise_and(image, image, mask=cv2.bitwise_not(mask))
|
| 371 |
+
result_image = cv2.add(image_no_mosaic, mosaic_result)
|
| 372 |
+
|
| 373 |
+
# モザイク処理結果を保存
|
| 374 |
+
cv2.imwrite(output_path, result_image)
|
| 375 |
+
|
| 376 |
+
return output_path
|
| 377 |
|
| 378 |
|
| 379 |
#この下は、openCV
|
|
|
|
| 539 |
# OpenCVでインペイント
|
| 540 |
inpaint_image_with_mask(input_path, mask_path, output_path)
|
| 541 |
|
| 542 |
+
return FileResponse(output_path)
|
| 543 |
+
@app.post("/create-mask-and-inpaint-mosaic")
|
| 544 |
+
async def create_mask_and_inpaint_opencv(image: UploadFile = File(...), risk_level: int = Form(...)):
|
| 545 |
+
point1 = (0.00000000000002, 0.00000000000002)
|
| 546 |
+
point2 = (0.00000000000001, 0.00000000000001)
|
| 547 |
+
input_path = save_image(image.file, "input.jpg")
|
| 548 |
+
mask_path = special_process_image_yolo(risk_level, input_path, point1, point2, thresholds)
|
| 549 |
+
|
| 550 |
+
output_path = SAVE_DIR / "output_opencv.jpg"
|
| 551 |
+
# OpenCVでインペイント
|
| 552 |
+
mosaic_image_with_mask(input_path, mask_path, output_path)
|
| 553 |
+
|
| 554 |
return FileResponse(output_path)
|
| 555 |
@app.post("/create-mask-and-inpaint-stamp")
|
| 556 |
async def create_mask_and_inpaint_opencv(image: UploadFile = File(...), risk_level: int = Form(...)):
|
|
|
|
| 789 |
output_path = f"./output_simple_lama_{timestamp}_{unique_id}.jpg"
|
| 790 |
|
| 791 |
# OpenCVでインペイント
|
| 792 |
+
mosaic_image_with_mask(input_path, mask_path, output_path)
|
| 793 |
|
| 794 |
return FileResponse(output_path)
|
| 795 |
|
|
|
|
| 884 |
<select id="processingType" class="custom-select">
|
| 885 |
<option value="opencv">OpenCVインペイント</option>
|
| 886 |
<option value="simple_lama">Simple Lamaインペイント</option>
|
| 887 |
+
<option value="stamp">stampインペイント</option>
|
| 888 |
+
<option value="mosaic">mosaicインペイント</option>
|
| 889 |
</select>
|
| 890 |
</div>
|
| 891 |
|
|
|
|
| 960 |
apiEndpoint = "/create-mask-and-inpaint-opencv";
|
| 961 |
} else if (processingType === "simple_lama") {
|
| 962 |
apiEndpoint = "/create-mask-and-inpaint-simple-lama";
|
| 963 |
+
} else if (processingType === "stamp") {
|
| 964 |
+
apiEndpoint = "/create-mask-and-inpaint-stamp";
|
| 965 |
+
} else if (processingType=="mosaic"){
|
| 966 |
+
apiEndpoint = "/create-mask-and-inpaint-mosaic";
|
| 967 |
}
|
| 968 |
|
| 969 |
const url = "https://rein0421-aidentify.hf.space" + apiEndpoint;
|