Ashrafb commited on
Commit
22b5e60
·
verified ·
1 Parent(s): c71a000

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -33
main.py CHANGED
@@ -1,9 +1,8 @@
1
- from fastapi import FastAPI, File, UploadFile, Form
2
  from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import FileResponse
4
- from io import BytesIO
5
- import cv2
6
- import numpy as np
7
  import insightface
8
  from insightface.app import FaceAnalysis
9
 
@@ -23,10 +22,10 @@ def sort_faces(faces):
23
  def get_face(faces, face_id):
24
  try:
25
  if len(faces) < face_id or face_id < 1:
26
- raise ValueError(f"The image includes only {len(faces)} faces, however, you asked for face {face_id}")
27
  return faces[face_id-1]
28
  except Exception as e:
29
- raise ValueError(f"An error occurred: {str(e)}")
30
 
31
  app, swapper = prepare_app()
32
 
@@ -41,39 +40,22 @@ def swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIn
41
  result = swapper.get(destinationImage, res_face, source_face, paste_back=True)
42
  return result
43
 
44
- @app.post("/swap-faces/")
45
- async def swap_faces_endpoint(source_image: UploadFile = File(...),
46
- source_face_index: int = Form(...),
47
- destination_image: UploadFile = File(...),
48
- destination_face_index: int = Form(...)):
49
  """
50
  Endpoint to swap faces between two images.
51
-
52
  Parameters:
53
- - source_image: UploadFile for the source image.
54
- - source_face_index: Index of the face in the source image to be swapped.
55
- - destination_image: UploadFile for the destination image.
56
- - destination_face_index: Index of the face in the destination image to be swapped.
57
  """
58
- # Read source image
59
- source_image_content = await source_image.read()
60
- nparr = np.fromstring(source_image_content, np.uint8)
61
- source_image_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
62
-
63
- # Read destination image
64
- destination_image_content = await destination_image.read()
65
- nparr = np.fromstring(destination_image_content, np.uint8)
66
- destination_image_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
67
-
68
- # Swap faces
69
- result_image_np = swap_faces(source_image_np, source_face_index, destination_image_np, destination_face_index)
70
-
71
- # Encode the result image to send back
72
- _, img_encoded = cv2.imencode('.jpg', result_image_np)
73
- return img_encoded.tobytes()
74
 
75
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
76
 
77
  @app.get("/")
78
  def index() -> FileResponse:
79
- return FileResponse(path="/app/static/index.html", media_type="text/html")
 
1
+ from fastapi import FastAPI
2
  from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import FileResponse
4
+
5
+
 
6
  import insightface
7
  from insightface.app import FaceAnalysis
8
 
 
22
  def get_face(faces, face_id):
23
  try:
24
  if len(faces) < face_id or face_id < 1:
25
+ raise gr.Error(f"The image includes only {len(faces)} faces, however, you asked for face {face_id}")
26
  return faces[face_id-1]
27
  except Exception as e:
28
+ raise gr.Error(f"An error occurred: {str(e)}")
29
 
30
  app, swapper = prepare_app()
31
 
 
40
  result = swapper.get(destinationImage, res_face, source_face, paste_back=True)
41
  return result
42
 
43
+ @app.get("/swap-faces/")
44
+ async def swap_faces_endpoint(sourceImage: str, sourceFaceIndex: int, destinationImage: str, destinationFaceIndex: int):
 
 
 
45
  """
46
  Endpoint to swap faces between two images.
47
+
48
  Parameters:
49
+ - sourceImage: URL or path to the source image.
50
+ - sourceFaceIndex: Index of the face in the source image to be swapped.
51
+ - destinationImage: URL or path to the destination image.
52
+ - destinationFaceIndex: Index of the face in the destination image to be swapped.
53
  """
54
+ result = swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex)
55
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
58
 
59
  @app.get("/")
60
  def index() -> FileResponse:
61
+ return FileResponse(path="/app/static/index.html", media_type="text/html")