Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,13 @@
|
|
5 |
|
6 |
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import os
|
9 |
import supervision as sv
|
10 |
from PIL import Image, ImageFilter
|
@@ -845,7 +852,8 @@ def apply_mosaic(image, bbox, mosaic_size=10):
|
|
845 |
face_region = cv2.resize(face_region, (w, h), interpolation=cv2.INTER_NEAREST)
|
846 |
image[y:y+h, x:x+w] = face_region
|
847 |
return image
|
848 |
-
|
|
|
849 |
@app.post("/mosaic_faces")
|
850 |
async def mosaic_faces(reference_image: UploadFile = File(...), test_image: UploadFile = File(...)):
|
851 |
try:
|
@@ -856,41 +864,30 @@ async def mosaic_faces(reference_image: UploadFile = File(...), test_image: Uplo
|
|
856 |
# Extract face data from reference image
|
857 |
reference_embeddings, _, _ = get_face_data(ref_image)
|
858 |
if reference_embeddings is None:
|
859 |
-
|
860 |
|
861 |
# Extract face data from test image
|
862 |
test_embeddings, test_bboxes, test_image_processed = get_face_data(test_image_data)
|
863 |
if test_embeddings is None:
|
864 |
-
|
865 |
|
866 |
# Process each detected face in the test image
|
867 |
-
recognized = False
|
868 |
for test_embedding, bbox in zip(test_embeddings, test_bboxes):
|
869 |
-
# Calculate cosine similarity between embeddings
|
870 |
similarity = np.dot(reference_embeddings[0], test_embedding) / (
|
871 |
np.linalg.norm(reference_embeddings[0]) * np.linalg.norm(test_embedding))
|
872 |
|
873 |
-
#
|
874 |
-
if similarity
|
875 |
-
recognized = True
|
876 |
-
cv2.rectangle(test_image_processed, (int(bbox[0]), int(bbox[1])),
|
877 |
-
(int(bbox[2]), int(bbox[3])), (0, 255, 0), 2) # Green box for recognized face
|
878 |
-
else:
|
879 |
-
# Apply mosaic to other faces
|
880 |
test_image_processed = apply_mosaic(test_image_processed, bbox)
|
881 |
|
882 |
-
|
883 |
-
|
|
|
884 |
|
885 |
-
|
886 |
-
_, buffer = cv2.imencode('.jpg', test_image_processed)
|
887 |
-
return JSONResponse(status_code=200, content={"message": "Faces processed successfully."})
|
888 |
|
889 |
except Exception as e:
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
|
895 |
@app.get("/", response_class=HTMLResponse)
|
896 |
async def read_root():
|
@@ -1072,7 +1069,5 @@ async def read_root():
|
|
1072 |
|
1073 |
"""
|
1074 |
return HTMLResponse(content=html_content)
|
1075 |
-
if __name__ == "__main__":
|
1076 |
|
1077 |
-
app.run(host="0.0.0.0", port=7860)
|
1078 |
|
|
|
5 |
|
6 |
|
7 |
|
8 |
+
# -*- coding: utf-8 -*-
|
9 |
+
|
10 |
+
# Commented out IPython magic to ensure Python compatibility.
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
import os
|
16 |
import supervision as sv
|
17 |
from PIL import Image, ImageFilter
|
|
|
852 |
face_region = cv2.resize(face_region, (w, h), interpolation=cv2.INTER_NEAREST)
|
853 |
image[y:y+h, x:x+w] = face_region
|
854 |
return image
|
855 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
856 |
+
from io import BytesIO #
|
857 |
@app.post("/mosaic_faces")
|
858 |
async def mosaic_faces(reference_image: UploadFile = File(...), test_image: UploadFile = File(...)):
|
859 |
try:
|
|
|
864 |
# Extract face data from reference image
|
865 |
reference_embeddings, _, _ = get_face_data(ref_image)
|
866 |
if reference_embeddings is None:
|
867 |
+
raise HTTPException(status_code=400, detail="No face detected in reference image.")
|
868 |
|
869 |
# Extract face data from test image
|
870 |
test_embeddings, test_bboxes, test_image_processed = get_face_data(test_image_data)
|
871 |
if test_embeddings is None:
|
872 |
+
raise HTTPException(status_code=400, detail="No face detected in test image.")
|
873 |
|
874 |
# Process each detected face in the test image
|
|
|
875 |
for test_embedding, bbox in zip(test_embeddings, test_bboxes):
|
|
|
876 |
similarity = np.dot(reference_embeddings[0], test_embedding) / (
|
877 |
np.linalg.norm(reference_embeddings[0]) * np.linalg.norm(test_embedding))
|
878 |
|
879 |
+
# Apply mosaic to unrecognized faces only
|
880 |
+
if similarity <= 0.4:
|
|
|
|
|
|
|
|
|
|
|
881 |
test_image_processed = apply_mosaic(test_image_processed, bbox)
|
882 |
|
883 |
+
# Save processed image to a temporary file
|
884 |
+
temp_file = "/tmp/processed_image.jpg"
|
885 |
+
cv2.imwrite(temp_file, test_image_processed)
|
886 |
|
887 |
+
return FileResponse(temp_file, media_type="image/jpeg", filename="processed_image.jpg")
|
|
|
|
|
888 |
|
889 |
except Exception as e:
|
890 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
891 |
|
892 |
@app.get("/", response_class=HTMLResponse)
|
893 |
async def read_root():
|
|
|
1069 |
|
1070 |
"""
|
1071 |
return HTMLResponse(content=html_content)
|
|
|
1072 |
|
|
|
1073 |
|