Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -752,73 +752,6 @@ async def create_mask_and_inpaint_simple_lama(
|
|
752 |
|
753 |
|
754 |
|
755 |
-
from PIL import Image
|
756 |
-
|
757 |
-
def resize_mask_to_match(image_path, mask_path):
|
758 |
-
# オリジナル画像とマスク画像を読み込む
|
759 |
-
original_image = Image.open(image_path)
|
760 |
-
mask_image = Image.open(mask_path)
|
761 |
-
|
762 |
-
# マスク画像をオリジナル画像のサイズにリサイズ
|
763 |
-
resized_mask = mask_image.resize(original_image.size)
|
764 |
-
|
765 |
-
# マスク画像を上書き保存
|
766 |
-
resized_mask.save(mask_path)
|
767 |
-
@app.post("/inpaint_with_face")
|
768 |
-
async def face_recognize(
|
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)
|
789 |
-
stamp_img = cv2.imread(stamp_path)
|
790 |
-
|
791 |
-
# 顔の位置とエンコーディングを取得
|
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(...),
|
|
|
752 |
|
753 |
|
754 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
755 |
|
756 |
@app.post("/create-mask-and-inpaint-sum")
|
757 |
async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(...),
|