Ashrafb commited on
Commit
5fee82c
·
verified ·
1 Parent(s): 9664264

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -42
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
- try:
37
- # Your existing code for swapping faces...
38
-
39
- source_bytes = await source_file.read()
40
- destination_bytes = await destination_file.read()
41
-
42
- # Decode images
43
- source_image = cv2.imdecode(np.frombuffer(source_bytes, np.uint8), cv2.IMREAD_COLOR)
44
- destination_image = cv2.imdecode(np.frombuffer(destination_bytes, np.uint8), cv2.IMREAD_COLOR)
45
-
46
- # Face detection and sorting
47
- faces_source = sort_faces(face_analysis.get(source_image))
48
- source_face = get_face(faces_source, source_face_index)
49
-
50
- faces_destination = sort_faces(face_analysis.get(destination_image))
51
- destination_face = get_face(faces_destination, destination_face_index)
52
-
53
- # Check if there are faces in both images
54
- if not faces_source:
55
- raise ValueError("Source image has no faces")
56
- if not faces_destination:
57
- raise ValueError("Destination image has no faces")
58
-
59
- # Swap faces
60
- result_image = swapper.get(destination_image, destination_face, source_face, paste_back=True)
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