Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -81,32 +81,37 @@ async def compare_faces(req: CompareRequest):
|
|
| 81 |
# Cosine similarity
|
| 82 |
similarity = np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
|
| 83 |
|
| 84 |
-
matched = similarity > 0.55 # threshold
|
| 85 |
|
| 86 |
return {
|
| 87 |
"similarity": float(similarity),
|
| 88 |
"match": matched
|
| 89 |
}
|
| 90 |
|
| 91 |
-
# ---------- Gradio UI ----------
|
| 92 |
-
def gradio_ui(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
if img1 is None or img2 is None:
|
| 102 |
-
return "
|
| 103 |
-
|
| 104 |
-
# Convert to OpenCV
|
| 105 |
-
img1_cv = cv2.cvtColor(np.array(img1), cv2.COLOR_RGB2BGR)
|
| 106 |
-
img2_cv = cv2.cvtColor(np.array(img2), cv2.COLOR_RGB2BGR)
|
| 107 |
|
| 108 |
-
emb1 = get_embedding(
|
| 109 |
-
emb2 = get_embedding(
|
| 110 |
|
| 111 |
if emb1 is None or emb2 is None:
|
| 112 |
return "Face not detected."
|
|
@@ -118,7 +123,10 @@ def gradio_ui(img1, img2):
|
|
| 118 |
|
| 119 |
gr.Interface(
|
| 120 |
fn=gradio_ui,
|
| 121 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 122 |
outputs="text",
|
| 123 |
-
title="Face Match API"
|
| 124 |
).launch()
|
|
|
|
| 81 |
# Cosine similarity
|
| 82 |
similarity = np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
|
| 83 |
|
| 84 |
+
matched = similarity > 0.55 # threshold
|
| 85 |
|
| 86 |
return {
|
| 87 |
"similarity": float(similarity),
|
| 88 |
"match": matched
|
| 89 |
}
|
| 90 |
|
| 91 |
+
# ---------- Gradio UI (TEXT INPUT instead of upload) ----------
|
| 92 |
+
def gradio_ui(img1_text, img2_text):
|
| 93 |
+
"""
|
| 94 |
+
Accepts:
|
| 95 |
+
- base64 string
|
| 96 |
+
- or URL
|
| 97 |
+
Automatically detects which one.
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
# --- Determine type of input ---
|
| 101 |
+
def load_any(input_str):
|
| 102 |
+
if input_str.startswith("http://") or input_str.startswith("https://"):
|
| 103 |
+
return url_to_img(input_str)
|
| 104 |
+
else:
|
| 105 |
+
return b64_to_img(input_str)
|
| 106 |
+
|
| 107 |
+
img1 = load_any(img1_text)
|
| 108 |
+
img2 = load_any(img2_text)
|
| 109 |
|
| 110 |
if img1 is None or img2 is None:
|
| 111 |
+
return "Invalid image data or URL."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
emb1 = get_embedding(img1)
|
| 114 |
+
emb2 = get_embedding(img2)
|
| 115 |
|
| 116 |
if emb1 is None or emb2 is None:
|
| 117 |
return "Face not detected."
|
|
|
|
| 123 |
|
| 124 |
gr.Interface(
|
| 125 |
fn=gradio_ui,
|
| 126 |
+
inputs=[
|
| 127 |
+
gr.Textbox(label="Image 1 (base64 or URL)"),
|
| 128 |
+
gr.Textbox(label="Image 2 (base64 or URL)")
|
| 129 |
+
],
|
| 130 |
outputs="text",
|
| 131 |
+
title="Face Match API (Text Input)"
|
| 132 |
).launch()
|