Spaces:
Running
Running
File size: 4,575 Bytes
847ca61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse
import gradio as gr
from deepface import DeepFace
import os
from threading import Thread
import asyncio
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# FastAPI instance
app = FastAPI()
# Gradio Interface Function
def face_verification_uii(img1, img2, dist="cosine", model="Facenet", detector="ssd"):
"""
Gradio function for face verification
"""
try:
result = DeepFace.verify(
img1_path=img1,
img2_path=img2,
distance_metric=dist,
model_name=model,
detector_backend=detector,
enforce_detection=False,
)
return {
"verified": result["verified"],
"distance": result["distance"],
"threshold": result["threshold"],
"model": result["model"],
"detector_backend": result["detector_backend"],
"similarity_metric": result["similarity_metric"],
}
except Exception as e:
return {"error": str(e)}
# FastAPI Endpoint
@app.get("/", response_class=HTMLResponse)
async def gradio_ui():
html_content = """
<html>
<head>
<title>Gradio UI</title>
</head>
<body>
<iframe src="http://localhost:7861" width="100%" height="100%" frameborder="0"></iframe>
</body>
</html>
"""
return HTMLResponse(content=html_content)
@app.post("/face_verification")
async def face_verification(
img1: UploadFile = File(...),
img2: UploadFile = File(...),
dist: str = Form("cosine"),
model: str = Form("Facenet"),
detector: str = Form("ssd")
):
"""
Endpoint to verify if two images belong to the same person.
"""
try:
# Ensure uploads directory exists
if not os.path.exists("uploads"):
os.makedirs("uploads")
# Save uploaded images to disk
img1_path = os.path.join("uploads", img1.filename)
img2_path = os.path.join("uploads", img2.filename)
with open(img1_path, "wb") as f:
f.write(await img1.read())
with open(img2_path, "wb") as f:
f.write(await img2.read())
# Run DeepFace verification
result = DeepFace.verify(
img1_path=img1_path,
img2_path=img2_path,
distance_metric=dist,
model_name=model,
detector_backend=detector,
enforce_detection=False,
)
# Delete uploaded images after processing
os.remove(img1_path)
os.remove(img2_path)
# Return verification results
return {
"verified": result["verified"],
"distance": result["distance"],
"threshold": result["threshold"],
"model": result["model"],
"detector_backend": result["detector_backend"],
"similarity_metric": result["similarity_metric"]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
def run_gradio_ui():
"""
Function to run Gradio in a separate thread
"""
# Create and set an event loop for this thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
def face_verification_ui(img1, img2, dist, model, detector):
result = face_verification_uii(img1, img2, dist, model, detector)
return result
with gr.Blocks() as demo:
img1 = gr.Image(type="filepath", label="Image 1")
img2 = gr.Image(type="filepath", label="Image 2")
dist = gr.Dropdown(choices=["cosine", "euclidean", "euclidean_l2"], label="Distance Metric", value="cosine")
model = gr.Dropdown(choices=["VGG-Face", "Facenet", "Facenet512", "ArcFace"], label="Model", value="Facenet")
detector = gr.Dropdown(choices=["opencv", "ssd", "mtcnn", "retinaface", "mediapipe"], label="Detector", value="ssd")
btn = gr.Button("Verify")
output = gr.Textbox()
btn.click(face_verification_ui, inputs=[img1, img2, dist, model, detector], outputs=output)
demo.launch(server_name="0.0.0.0", server_port=7861, show_api=False)
# FastAPI Startup Event
# FastAPI Startup Event
@app.on_event("startup")
def startup_event():
"""
Start Gradio UI in a separate thread
"""
thread = Thread(target=run_gradio_ui)
thread.start()
# Running Both Servers
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860, reload=True) |