HariLogicgo commited on
Commit
29a0692
·
1 Parent(s): f81c825
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -127,19 +127,31 @@ api = FastAPI()
127
 
128
  @api.post("/api/faceswap")
129
  async def faceswap(src_img: UploadFile = File(...), tgt_img: UploadFile = File(...)):
130
- src = cv2.imdecode(np.frombuffer(await src_img.read(), np.uint8), cv2.IMREAD_COLOR)
131
- tgt = cv2.imdecode(np.frombuffer(await tgt_img.read(), np.uint8), cv2.IMREAD_COLOR)
 
 
 
 
 
 
 
132
 
133
- result_img, err = face_swap_and_enhance(
134
- cv2.cvtColor(src, cv2.COLOR_BGR2RGB),
135
- cv2.cvtColor(tgt, cv2.COLOR_BGR2RGB)
136
- )
137
- if result_img is None:
138
- return {"error": err}
139
 
140
- _, buffer = cv2.imencode(".png", cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR))
141
- return StreamingResponse(io.BytesIO(buffer.tobytes()), media_type="image/png")
142
 
 
 
 
 
 
 
143
  # -------------------------------------------------
144
  # Gradio UI
145
  # -------------------------------------------------
 
127
 
128
  @api.post("/api/faceswap")
129
  async def faceswap(src_img: UploadFile = File(...), tgt_img: UploadFile = File(...)):
130
+ """
131
+ REST API endpoint for face swapping.
132
+ Accepts source and target images as file uploads.
133
+ Returns enhanced face-swapped image as PNG.
134
+ """
135
+ try:
136
+ # Read images from uploads
137
+ src = cv2.imdecode(np.frombuffer(await src_img.read(), np.uint8), cv2.IMREAD_COLOR)
138
+ tgt = cv2.imdecode(np.frombuffer(await tgt_img.read(), np.uint8), cv2.IMREAD_COLOR)
139
 
140
+ # Run face swap + CodeFormer enhancement
141
+ result_img, err = face_swap_and_enhance(
142
+ cv2.cvtColor(src, cv2.COLOR_BGR2RGB),
143
+ cv2.cvtColor(tgt, cv2.COLOR_BGR2RGB)
144
+ )
 
145
 
146
+ if result_img is None:
147
+ return {"error": err}
148
 
149
+ # Encode as PNG and return
150
+ _, buffer = cv2.imencode(".png", cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR))
151
+ return StreamingResponse(io.BytesIO(buffer.tobytes()), media_type="image/png")
152
+
153
+ except Exception as e:
154
+ return {"error": f"Exception: {str(e)}"}
155
  # -------------------------------------------------
156
  # Gradio UI
157
  # -------------------------------------------------