Spaces:
Running
Running
verification flow fix
Browse files- .gitignore +2 -1
- __pycache__/app.cpython-312.pyc +0 -0
- app.py +70 -7
- x.jpg +0 -0
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
docker-compose.yml
|
|
|
|
|
|
| 1 |
+
docker-compose.yml
|
| 2 |
+
env
|
__pycache__/app.cpython-312.pyc
ADDED
|
Binary file (4.9 kB). View file
|
|
|
app.py
CHANGED
|
@@ -2,6 +2,10 @@ from fastapi import FastAPI
|
|
| 2 |
from deepface import DeepFace
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class Verify(BaseModel):
|
|
@@ -10,6 +14,36 @@ class Verify(BaseModel):
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@app.get("/")
|
| 14 |
def greet_json():
|
| 15 |
return {"Hello": "World!"}
|
|
@@ -20,19 +54,48 @@ def verify(v: Verify):
|
|
| 20 |
selfie = data["image"]
|
| 21 |
gallery = data["images"]
|
| 22 |
|
| 23 |
-
import requests
|
| 24 |
-
res = requests.get(gallery[0], stream=True)
|
| 25 |
-
with open("x.jpg", "wb") as out_file:
|
| 26 |
-
out_file.write(res.content)
|
| 27 |
-
|
| 28 |
true_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
for image in gallery:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if result.get("verified", False):
|
| 33 |
true_count += 1
|
| 34 |
if true_count >= 2:
|
| 35 |
return JSONResponse(content={"verified": True, "image": image})
|
| 36 |
except Exception as e:
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return JSONResponse(content={"verified": False, "image": None})
|
|
|
|
| 2 |
from deepface import DeepFace
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
+
import base64
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
import requests
|
| 9 |
|
| 10 |
|
| 11 |
class Verify(BaseModel):
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
def load_image(source: str):
|
| 18 |
+
"""
|
| 19 |
+
Load an image from a URL, base64 data URI, or local file path into a numpy array (BGR).
|
| 20 |
+
"""
|
| 21 |
+
if isinstance(source, str):
|
| 22 |
+
if source.startswith("http://") or source.startswith("https://"):
|
| 23 |
+
# Download and decode
|
| 24 |
+
resp = requests.get(source, headers={"User-Agent": "Mozilla/5.0"}, timeout=20)
|
| 25 |
+
resp.raise_for_status()
|
| 26 |
+
data = np.frombuffer(resp.content, dtype=np.uint8)
|
| 27 |
+
img = cv2.imdecode(data, cv2.IMREAD_COLOR)
|
| 28 |
+
if img is None:
|
| 29 |
+
raise ValueError(f"Failed to decode image from URL: {source}")
|
| 30 |
+
return img
|
| 31 |
+
if source.startswith("data:image"):
|
| 32 |
+
# data URI: data:image/<type>;base64,<payload>
|
| 33 |
+
b64_payload = source.split(",", 1)[1] if "," in source else source
|
| 34 |
+
binary = base64.b64decode(b64_payload)
|
| 35 |
+
data = np.frombuffer(binary, dtype=np.uint8)
|
| 36 |
+
img = cv2.imdecode(data, cv2.IMREAD_COLOR)
|
| 37 |
+
if img is None:
|
| 38 |
+
raise ValueError("Failed to decode image from base64 data")
|
| 39 |
+
return img
|
| 40 |
+
# Fallback: treat as local path
|
| 41 |
+
img = cv2.imread(source)
|
| 42 |
+
if img is None:
|
| 43 |
+
raise ValueError(f"Failed to load local image: {source}")
|
| 44 |
+
return img
|
| 45 |
+
raise TypeError("Unsupported image source type")
|
| 46 |
+
|
| 47 |
@app.get("/")
|
| 48 |
def greet_json():
|
| 49 |
return {"Hello": "World!"}
|
|
|
|
| 54 |
selfie = data["image"]
|
| 55 |
gallery = data["images"]
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
true_count = 0
|
| 58 |
+
print(selfie)
|
| 59 |
+
try:
|
| 60 |
+
selfie_img = load_image(selfie)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Failed to load selfie image: {e}")
|
| 63 |
+
return JSONResponse(content={"verified": False, "image": None, "error": "failed_to_load_selfie"})
|
| 64 |
+
|
| 65 |
for image in gallery:
|
| 66 |
+
print(image)
|
| 67 |
+
try:
|
| 68 |
+
gallery_img = load_image(image)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Failed to load gallery image {image}: {e}")
|
| 71 |
+
continue
|
| 72 |
try:
|
| 73 |
+
# Use a more robust detector; fall back if detection fails
|
| 74 |
+
result = DeepFace.verify(
|
| 75 |
+
img1_path=selfie_img,
|
| 76 |
+
img2_path=gallery_img,
|
| 77 |
+
detector_backend="retinaface"
|
| 78 |
+
)
|
| 79 |
if result.get("verified", False):
|
| 80 |
true_count += 1
|
| 81 |
if true_count >= 2:
|
| 82 |
return JSONResponse(content={"verified": True, "image": image})
|
| 83 |
except Exception as e:
|
| 84 |
+
msg = str(e)
|
| 85 |
+
print(f"DeepFace verification error for {image}: {msg}")
|
| 86 |
+
# Optional fallback without strict detection to avoid generic img2_path errors
|
| 87 |
+
if "Face could not be detected" in msg or "No face" in msg:
|
| 88 |
+
try:
|
| 89 |
+
result = DeepFace.verify(
|
| 90 |
+
img1_path=selfie_img,
|
| 91 |
+
img2_path=gallery_img,
|
| 92 |
+
detector_backend="retinaface",
|
| 93 |
+
enforce_detection=False
|
| 94 |
+
)
|
| 95 |
+
if result.get("verified", False):
|
| 96 |
+
true_count += 1
|
| 97 |
+
if true_count >= 2:
|
| 98 |
+
return JSONResponse(content={"verified": True, "image": image})
|
| 99 |
+
except Exception as e2:
|
| 100 |
+
print(f"DeepFace fallback error for {image}: {e2}")
|
| 101 |
return JSONResponse(content={"verified": False, "image": None})
|
x.jpg
ADDED
|