Spaces:
Running
on
T4
Running
on
T4
Commit
·
f81c825
1
Parent(s):
8c09773
again revised app.py
Browse files- app.py +21 -37
- requirements.txt +0 -2
app.py
CHANGED
|
@@ -10,11 +10,9 @@ import insightface
|
|
| 10 |
from insightface.app import FaceAnalysis
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from fastapi import FastAPI, UploadFile, File
|
| 13 |
-
from fastapi.responses import StreamingResponse
|
| 14 |
import io
|
| 15 |
-
import threading
|
| 16 |
import gradio as gr
|
| 17 |
-
import uvicorn
|
| 18 |
|
| 19 |
# -------------------------------------------------
|
| 20 |
# Paths
|
|
@@ -88,18 +86,19 @@ def face_swap_and_enhance(src_img, tgt_img):
|
|
| 88 |
if not src_faces or not tgt_faces:
|
| 89 |
return None, "❌ Face not detected in one of the images."
|
| 90 |
|
|
|
|
| 91 |
shutil.rmtree(UPLOAD_DIR, ignore_errors=True)
|
| 92 |
shutil.rmtree(RESULT_DIR, ignore_errors=True)
|
| 93 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 94 |
os.makedirs(RESULT_DIR, exist_ok=True)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
unique_name = f"swapped_{uuid.uuid4().hex[:8]}.jpg"
|
| 98 |
swapped_path = os.path.join(UPLOAD_DIR, unique_name)
|
| 99 |
swapped_bgr = swapper.get(tgt_bgr, tgt_faces[0], src_faces[0])
|
| 100 |
cv2.imwrite(swapped_path, swapped_bgr)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
cmd = f"python {CODEFORMER_PATH} -w 0.7 --input_path {swapped_path} --output_path {RESULT_DIR} --bg_upsampler realesrgan --face_upsample"
|
| 104 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 105 |
if result.returncode != 0:
|
|
@@ -128,34 +127,24 @@ api = FastAPI()
|
|
| 128 |
|
| 129 |
@api.post("/api/faceswap")
|
| 130 |
async def faceswap(src_img: UploadFile = File(...), tgt_img: UploadFile = File(...)):
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
tgt_bytes = await tgt_img.read()
|
| 134 |
-
|
| 135 |
-
src = cv2.imdecode(np.frombuffer(src_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 136 |
-
tgt = cv2.imdecode(np.frombuffer(tgt_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 137 |
-
|
| 138 |
-
if src is None or tgt is None:
|
| 139 |
-
return JSONResponse({"error": "Invalid images"}, status_code=400)
|
| 140 |
-
|
| 141 |
-
img_rgb_src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
|
| 142 |
-
img_rgb_tgt = cv2.cvtColor(tgt, cv2.COLOR_BGR2RGB)
|
| 143 |
-
|
| 144 |
-
result_img, err = face_swap_and_enhance(img_rgb_src, img_rgb_tgt)
|
| 145 |
-
if result_img is None:
|
| 146 |
-
return JSONResponse({"error": err}, status_code=500)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
# -------------------------------------------------
|
| 155 |
-
# Gradio UI
|
| 156 |
# -------------------------------------------------
|
| 157 |
with gr.Blocks() as demo:
|
| 158 |
-
gr.Markdown("Face Swap
|
| 159 |
|
| 160 |
with gr.Row():
|
| 161 |
src_input = gr.Image(type="numpy", label="Upload Your Face")
|
|
@@ -167,17 +156,12 @@ with gr.Blocks() as demo:
|
|
| 167 |
|
| 168 |
def process(src, tgt):
|
| 169 |
img, err = face_swap_and_enhance(src, tgt)
|
| 170 |
-
|
| 171 |
-
return None, err
|
| 172 |
-
return img, ""
|
| 173 |
|
| 174 |
btn.click(process, [src_input, tgt_input], [output_img, error_box])
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
# -------------------------------------------------
|
| 179 |
-
def run_api():
|
| 180 |
-
uvicorn.run(api, host="0.0.0.0", port=7861)
|
| 181 |
|
| 182 |
-
|
| 183 |
-
demo.launch(
|
|
|
|
| 10 |
from insightface.app import FaceAnalysis
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from fastapi import FastAPI, UploadFile, File
|
| 13 |
+
from fastapi.responses import StreamingResponse
|
| 14 |
import io
|
|
|
|
| 15 |
import gradio as gr
|
|
|
|
| 16 |
|
| 17 |
# -------------------------------------------------
|
| 18 |
# Paths
|
|
|
|
| 86 |
if not src_faces or not tgt_faces:
|
| 87 |
return None, "❌ Face not detected in one of the images."
|
| 88 |
|
| 89 |
+
# clean dirs
|
| 90 |
shutil.rmtree(UPLOAD_DIR, ignore_errors=True)
|
| 91 |
shutil.rmtree(RESULT_DIR, ignore_errors=True)
|
| 92 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 93 |
os.makedirs(RESULT_DIR, exist_ok=True)
|
| 94 |
|
| 95 |
+
# save swapped
|
| 96 |
unique_name = f"swapped_{uuid.uuid4().hex[:8]}.jpg"
|
| 97 |
swapped_path = os.path.join(UPLOAD_DIR, unique_name)
|
| 98 |
swapped_bgr = swapper.get(tgt_bgr, tgt_faces[0], src_faces[0])
|
| 99 |
cv2.imwrite(swapped_path, swapped_bgr)
|
| 100 |
|
| 101 |
+
# run CodeFormer
|
| 102 |
cmd = f"python {CODEFORMER_PATH} -w 0.7 --input_path {swapped_path} --output_path {RESULT_DIR} --bg_upsampler realesrgan --face_upsample"
|
| 103 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 104 |
if result.returncode != 0:
|
|
|
|
| 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 |
# -------------------------------------------------
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
+
gr.Markdown("### Face Swap + CodeFormer Enhancement")
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
src_input = gr.Image(type="numpy", label="Upload Your Face")
|
|
|
|
| 156 |
|
| 157 |
def process(src, tgt):
|
| 158 |
img, err = face_swap_and_enhance(src, tgt)
|
| 159 |
+
return img, err
|
|
|
|
|
|
|
| 160 |
|
| 161 |
btn.click(process, [src_input, tgt_input], [output_img, error_box])
|
| 162 |
|
| 163 |
+
# Mount FastAPI into Gradio
|
| 164 |
+
demo.app = api
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
# Launch (only once)
|
| 167 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
|
@@ -31,8 +31,6 @@ onnx
|
|
| 31 |
# App deps
|
| 32 |
gradio
|
| 33 |
huggingface_hub
|
| 34 |
-
|
| 35 |
-
# FastAPI REST API + server
|
| 36 |
fastapi
|
| 37 |
uvicorn
|
| 38 |
|
|
|
|
| 31 |
# App deps
|
| 32 |
gradio
|
| 33 |
huggingface_hub
|
|
|
|
|
|
|
| 34 |
fastapi
|
| 35 |
uvicorn
|
| 36 |
|