Midnightar commited on
Commit
aece0c4
·
verified ·
1 Parent(s): e8ae471

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
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; adjust as needed
85
 
86
  return {
87
  "similarity": float(similarity),
88
  "match": matched
89
  }
90
 
91
- # ---------- Gradio UI ----------
92
- def gradio_ui(img1, img2):
93
- import base64
94
- import io
95
-
96
- def to_b64(pil_img):
97
- buf = io.BytesIO()
98
- pil_img.save(buf, format="JPEG")
99
- return base64.b64encode(buf.getvalue()).decode()
 
 
 
 
 
 
 
 
 
100
 
101
  if img1 is None or img2 is None:
102
- return "Upload both images."
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(img1_cv)
109
- emb2 = get_embedding(img2_cv)
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=[gr.Image(), gr.Image()],
 
 
 
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()