Spaces:
Running
Running
test1
Browse files
app.py
CHANGED
|
@@ -31,6 +31,11 @@ def log_event(event_type: str, **fields):
|
|
| 31 |
logger.error(json.dumps(payload, default=str))
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def extract_root_cause(exc: Exception) -> str:
|
| 35 |
cause = getattr(exc, "__cause__", None)
|
| 36 |
if cause:
|
|
@@ -45,6 +50,17 @@ def safe_remove(path: str):
|
|
| 45 |
pass
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def prepare_image_for_deepface(source: str):
|
| 49 |
"""
|
| 50 |
Return a filesystem path DeepFace can consume reliably.
|
|
@@ -101,6 +117,7 @@ def verify(v: Verify):
|
|
| 101 |
print(f"Failed to load selfie image: {e}")
|
| 102 |
log_event("load_error", target="selfie", source=selfie, error=str(e))
|
| 103 |
return JSONResponse(content={"verified": False, "image": None, "error": "failed_to_load_selfie"})
|
|
|
|
| 104 |
|
| 105 |
try:
|
| 106 |
for image in gallery:
|
|
@@ -122,9 +139,17 @@ def verify(v: Verify):
|
|
| 122 |
detector_backend="opencv",
|
| 123 |
enforce_detection=False
|
| 124 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
if result.get("verified", False):
|
| 126 |
true_count += 1
|
|
|
|
| 127 |
if true_count >= 2:
|
|
|
|
| 128 |
return JSONResponse(content={"verified": True, "image": image})
|
| 129 |
except Exception as e:
|
| 130 |
msg = str(e)
|
|
@@ -144,9 +169,17 @@ def verify(v: Verify):
|
|
| 144 |
detector_backend="opencv",
|
| 145 |
enforce_detection=False
|
| 146 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
if result.get("verified", False):
|
| 148 |
true_count += 1
|
|
|
|
| 149 |
if true_count >= 2:
|
|
|
|
| 150 |
return JSONResponse(content={"verified": True, "image": image})
|
| 151 |
except Exception as e2:
|
| 152 |
print(f"DeepFace fallback error for {image}: {e2}")
|
|
@@ -154,6 +187,7 @@ def verify(v: Verify):
|
|
| 154 |
finally:
|
| 155 |
if gallery_is_temp and gallery_path:
|
| 156 |
safe_remove(gallery_path)
|
|
|
|
| 157 |
return JSONResponse(content={"verified": False, "image": None})
|
| 158 |
finally:
|
| 159 |
if selfie_is_temp and selfie_path:
|
|
|
|
| 31 |
logger.error(json.dumps(payload, default=str))
|
| 32 |
|
| 33 |
|
| 34 |
+
def log_info_event(event_type: str, **fields):
|
| 35 |
+
payload = {"event": event_type, **fields}
|
| 36 |
+
logger.info(json.dumps(payload, default=str))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
def extract_root_cause(exc: Exception) -> str:
|
| 40 |
cause = getattr(exc, "__cause__", None)
|
| 41 |
if cause:
|
|
|
|
| 50 |
pass
|
| 51 |
|
| 52 |
|
| 53 |
+
def summarize_result(result: dict):
|
| 54 |
+
return {
|
| 55 |
+
"verified": result.get("verified"),
|
| 56 |
+
"distance": result.get("distance"),
|
| 57 |
+
"threshold": result.get("threshold"),
|
| 58 |
+
"model": result.get("model"),
|
| 59 |
+
"detector_backend": result.get("detector_backend"),
|
| 60 |
+
"facial_areas": result.get("facial_areas"),
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
def prepare_image_for_deepface(source: str):
|
| 65 |
"""
|
| 66 |
Return a filesystem path DeepFace can consume reliably.
|
|
|
|
| 117 |
print(f"Failed to load selfie image: {e}")
|
| 118 |
log_event("load_error", target="selfie", source=selfie, error=str(e))
|
| 119 |
return JSONResponse(content={"verified": False, "image": None, "error": "failed_to_load_selfie"})
|
| 120 |
+
log_info_event("verify_started", selfie=selfie, gallery_count=len(gallery))
|
| 121 |
|
| 122 |
try:
|
| 123 |
for image in gallery:
|
|
|
|
| 139 |
detector_backend="opencv",
|
| 140 |
enforce_detection=False
|
| 141 |
)
|
| 142 |
+
log_info_event(
|
| 143 |
+
"verify_attempt",
|
| 144 |
+
stage="primary",
|
| 145 |
+
gallery_image=image,
|
| 146 |
+
result=summarize_result(result),
|
| 147 |
+
)
|
| 148 |
if result.get("verified", False):
|
| 149 |
true_count += 1
|
| 150 |
+
log_info_event("verify_match_count", gallery_image=image, true_count=true_count)
|
| 151 |
if true_count >= 2:
|
| 152 |
+
log_info_event("verify_response", verified=True, matched_image=image, true_count=true_count)
|
| 153 |
return JSONResponse(content={"verified": True, "image": image})
|
| 154 |
except Exception as e:
|
| 155 |
msg = str(e)
|
|
|
|
| 169 |
detector_backend="opencv",
|
| 170 |
enforce_detection=False
|
| 171 |
)
|
| 172 |
+
log_info_event(
|
| 173 |
+
"verify_attempt",
|
| 174 |
+
stage="fallback",
|
| 175 |
+
gallery_image=image,
|
| 176 |
+
result=summarize_result(result),
|
| 177 |
+
)
|
| 178 |
if result.get("verified", False):
|
| 179 |
true_count += 1
|
| 180 |
+
log_info_event("verify_match_count", gallery_image=image, true_count=true_count)
|
| 181 |
if true_count >= 2:
|
| 182 |
+
log_info_event("verify_response", verified=True, matched_image=image, true_count=true_count)
|
| 183 |
return JSONResponse(content={"verified": True, "image": image})
|
| 184 |
except Exception as e2:
|
| 185 |
print(f"DeepFace fallback error for {image}: {e2}")
|
|
|
|
| 187 |
finally:
|
| 188 |
if gallery_is_temp and gallery_path:
|
| 189 |
safe_remove(gallery_path)
|
| 190 |
+
log_info_event("verify_response", verified=False, matched_image=None, true_count=true_count)
|
| 191 |
return JSONResponse(content={"verified": False, "image": None})
|
| 192 |
finally:
|
| 193 |
if selfie_is_temp and selfie_path:
|