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