Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -26,54 +26,37 @@ from fastapi.responses import StreamingResponse
|
|
| 26 |
|
| 27 |
|
| 28 |
from fastapi import Form
|
| 29 |
-
from fastapi import HTTPException
|
| 30 |
|
| 31 |
@app.post("/swap_faces/")
|
| 32 |
async def swap_faces(source_file: UploadFile = File(...),
|
| 33 |
source_face_index: int = Form(...),
|
| 34 |
destination_file: UploadFile = File(...),
|
| 35 |
destination_face_index: int = Form(...)):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Convert result_image back to bytes
|
| 63 |
-
_, result_bytes = cv2.imencode('.jpg', result_image)
|
| 64 |
-
|
| 65 |
-
# Return the image bytes as a streaming response
|
| 66 |
-
return StreamingResponse(io.BytesIO(result_bytes), media_type="image/jpeg")
|
| 67 |
-
except ValueError as ve:
|
| 68 |
-
# Log the error message to console
|
| 69 |
-
print(f"An error occurred: {ve}")
|
| 70 |
-
# Return the error message as part of the response
|
| 71 |
-
return {"error": str(ve)}
|
| 72 |
-
except Exception as e:
|
| 73 |
-
# Log other types of errors to console
|
| 74 |
-
print(f"An error occurred: {e}")
|
| 75 |
-
# Raise HTTPException with 500 Internal Server Error status code and a generic error message
|
| 76 |
-
raise HTTPException(status_code=500, detail="An unexpected error occurred")
|
| 77 |
|
| 78 |
|
| 79 |
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
from fastapi import Form
|
|
|
|
| 29 |
|
| 30 |
@app.post("/swap_faces/")
|
| 31 |
async def swap_faces(source_file: UploadFile = File(...),
|
| 32 |
source_face_index: int = Form(...),
|
| 33 |
destination_file: UploadFile = File(...),
|
| 34 |
destination_face_index: int = Form(...)):
|
| 35 |
+
"""Swaps faces between the source and destination images based on the specified face indices."""
|
| 36 |
+
# Your existing code for swapping faces...
|
| 37 |
+
|
| 38 |
+
source_bytes = await source_file.read()
|
| 39 |
+
destination_bytes = await destination_file.read()
|
| 40 |
+
|
| 41 |
+
# Decode images
|
| 42 |
+
source_image = cv2.imdecode(np.frombuffer(source_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 43 |
+
destination_image = cv2.imdecode(np.frombuffer(destination_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 44 |
+
|
| 45 |
+
# Face detection and sorting
|
| 46 |
+
faces_source = sort_faces(face_analysis.get(source_image))
|
| 47 |
+
source_face = get_face(faces_source, source_face_index)
|
| 48 |
+
|
| 49 |
+
faces_destination = sort_faces(face_analysis.get(destination_image))
|
| 50 |
+
destination_face = get_face(faces_destination, destination_face_index)
|
| 51 |
+
|
| 52 |
+
# Swap faces
|
| 53 |
+
result_image = swapper.get(destination_image, destination_face, source_face, paste_back=True)
|
| 54 |
+
|
| 55 |
+
# Convert result_image back to bytes
|
| 56 |
+
_, result_bytes = cv2.imencode('.jpg', result_image)
|
| 57 |
+
|
| 58 |
+
# Return the image bytes as a streaming response
|
| 59 |
+
return StreamingResponse(io.BytesIO(result_bytes), media_type="image/jpeg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
|