Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -769,14 +769,20 @@ async def face_recognize(
|
|
| 769 |
image: UploadFile = File(...),
|
| 770 |
face_image: UploadFile = File(...),
|
| 771 |
option: int = Form(...),
|
| 772 |
-
stamp_image: UploadFile = File(...)
|
|
|
|
| 773 |
):
|
| 774 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 775 |
unique_id = uuid.uuid4().hex
|
| 776 |
input_path = save_image(image.file, f"./input_{timestamp}_{unique_id}.jpg")
|
| 777 |
stamp_path = save_image(stamp_image.file, f"./stamp_{timestamp}_{unique_id}.jpg")
|
| 778 |
face_path = save_image(face_image.file, f"./face_{timestamp}_{unique_id}.jpg")
|
| 779 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
# 画像の読み込み
|
| 781 |
target_image = face_recognition.load_image_file(input_path)
|
| 782 |
rgb_image = cv2.cvtColor(target_image, cv2.COLOR_RGB2BGR)
|
|
@@ -786,22 +792,34 @@ async def face_recognize(
|
|
| 786 |
face_locations = face_recognition.face_locations(target_image)
|
| 787 |
face_encodings = face_recognition.face_encodings(target_image, face_locations)
|
| 788 |
|
| 789 |
-
# 顔
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 798 |
|
| 799 |
# 結果を保存または返却 (ここでは仮に保存)
|
| 800 |
output_path = f"./output_{timestamp}_{unique_id}.jpg"
|
| 801 |
cv2.imwrite(output_path, rgb_image)
|
| 802 |
|
| 803 |
return FileResponse(output_path)
|
| 804 |
-
|
| 805 |
@app.post("/create-mask-and-inpaint-sum")
|
| 806 |
async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(...),
|
| 807 |
x1: float = Form(...),
|
|
|
|
| 769 |
image: UploadFile = File(...),
|
| 770 |
face_image: UploadFile = File(...),
|
| 771 |
option: int = Form(...),
|
| 772 |
+
stamp_image: UploadFile = File(...),
|
| 773 |
+
threshold: float = Form(0.8)
|
| 774 |
):
|
| 775 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 776 |
unique_id = uuid.uuid4().hex
|
| 777 |
input_path = save_image(image.file, f"./input_{timestamp}_{unique_id}.jpg")
|
| 778 |
stamp_path = save_image(stamp_image.file, f"./stamp_{timestamp}_{unique_id}.jpg")
|
| 779 |
face_path = save_image(face_image.file, f"./face_{timestamp}_{unique_id}.jpg")
|
| 780 |
+
|
| 781 |
+
# 認識済みの顔データのエンコード
|
| 782 |
+
known_face_encodings = []
|
| 783 |
+
reference_image = face_recognition.load_image_file(face_path)
|
| 784 |
+
known_face_encodings.append(face_recognition.face_encodings(reference_image)[0])
|
| 785 |
+
|
| 786 |
# 画像の読み込み
|
| 787 |
target_image = face_recognition.load_image_file(input_path)
|
| 788 |
rgb_image = cv2.cvtColor(target_image, cv2.COLOR_RGB2BGR)
|
|
|
|
| 792 |
face_locations = face_recognition.face_locations(target_image)
|
| 793 |
face_encodings = face_recognition.face_encodings(target_image, face_locations)
|
| 794 |
|
| 795 |
+
# 最も信頼度の高い顔を見つける
|
| 796 |
+
best_match_index = None
|
| 797 |
+
min_distance = float("inf")
|
| 798 |
+
for i, (top, right, bottom, left), face_encoding in zip(range(len(face_locations)), face_locations, face_encodings):
|
| 799 |
+
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
|
| 800 |
+
if np.any(face_distances < threshold):
|
| 801 |
+
best_index = np.argmin(face_distances)
|
| 802 |
+
if face_distances[best_index] < min_distance:
|
| 803 |
+
min_distance = face_distances[best_index]
|
| 804 |
+
best_match_index = i # 最も信頼度の高い顔のインデックス
|
| 805 |
+
|
| 806 |
+
# 最も信頼度の高い顔以外を隠す
|
| 807 |
+
for i, (top, right, bottom, left) in enumerate(face_locations):
|
| 808 |
+
if i != best_match_index: # 信頼度の高い顔以外を隠す
|
| 809 |
+
if option == 1: # スタンプで隠す
|
| 810 |
+
stamp_resized = cv2.resize(stamp_img, (right - left, bottom - top))
|
| 811 |
+
rgb_image[top:bottom, left:right] = stamp_resized
|
| 812 |
+
elif option == 2: # モザイクで隠す
|
| 813 |
+
face_region = rgb_image[top:bottom, left:right]
|
| 814 |
+
blurred_face = cv2.GaussianBlur(face_region, (99, 99), 30)
|
| 815 |
+
rgb_image[top:bottom, left:right] = blurred_face
|
| 816 |
|
| 817 |
# 結果を保存または返却 (ここでは仮に保存)
|
| 818 |
output_path = f"./output_{timestamp}_{unique_id}.jpg"
|
| 819 |
cv2.imwrite(output_path, rgb_image)
|
| 820 |
|
| 821 |
return FileResponse(output_path)
|
| 822 |
+
|
| 823 |
@app.post("/create-mask-and-inpaint-sum")
|
| 824 |
async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(...),
|
| 825 |
x1: float = Form(...),
|