anupamdutta279 commited on
Commit
2c390eb
·
verified ·
1 Parent(s): 77f55d7

testing loggss update again

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -1,15 +1,19 @@
1
- from fastapi import FastAPI
2
- from deepface import DeepFace
3
- from pydantic import BaseModel
4
- from fastapi.responses import JSONResponse
5
  import base64
6
  import json
7
  import logging
8
  import os
9
  import tempfile
 
 
 
 
10
  import cv2
11
  import numpy as np
12
  import requests
 
 
 
 
13
 
14
 
15
  class Verify(BaseModel):
@@ -26,6 +30,14 @@ def log_event(event_type: str, **fields):
26
  payload = {"event": event_type, **fields}
27
  logger.error(json.dumps(payload, default=str))
28
 
 
 
 
 
 
 
 
 
29
  def safe_remove(path: str):
30
  try:
31
  os.remove(path)
@@ -106,7 +118,9 @@ def verify(v: Verify):
106
  result = DeepFace.verify(
107
  img1_path=selfie_path,
108
  img2_path=gallery_path,
109
- detector_backend="retinaface"
 
 
110
  )
111
  if result.get("verified", False):
112
  true_count += 1
@@ -114,17 +128,19 @@ def verify(v: Verify):
114
  return JSONResponse(content={"verified": True, "image": image})
115
  except Exception as e:
116
  msg = str(e)
 
117
  print(f"DeepFace verification error for {image}: {msg}")
118
  if "img1_path" in msg:
119
- log_event("img1_path_error", gallery_image=image, error=msg)
120
  if "Face could not be detected" in msg or "No face" in msg:
121
- log_event("face_not_detected", gallery_image=image, error=msg)
122
  # Fallback path on generic processing or face-detection errors.
123
  if "img1_path" in msg or "Face could not be detected" in msg or "No face" in msg:
124
  try:
125
  result = DeepFace.verify(
126
  img1_path=selfie_path,
127
  img2_path=gallery_path,
 
128
  detector_backend="opencv",
129
  enforce_detection=False
130
  )
@@ -134,6 +150,7 @@ def verify(v: Verify):
134
  return JSONResponse(content={"verified": True, "image": image})
135
  except Exception as e2:
136
  print(f"DeepFace fallback error for {image}: {e2}")
 
137
  finally:
138
  if gallery_is_temp and gallery_path:
139
  safe_remove(gallery_path)
 
 
 
 
 
1
  import base64
2
  import json
3
  import logging
4
  import os
5
  import tempfile
6
+
7
+ # DeepFace/RetinaFace can break on newer TF/Keras combinations without this flag.
8
+ os.environ.setdefault("TF_USE_LEGACY_KERAS", "1")
9
+
10
  import cv2
11
  import numpy as np
12
  import requests
13
+ from deepface import DeepFace
14
+ from fastapi import FastAPI
15
+ from fastapi.responses import JSONResponse
16
+ from pydantic import BaseModel
17
 
18
 
19
  class Verify(BaseModel):
 
30
  payload = {"event": event_type, **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:
37
+ return str(cause)
38
+ return str(exc)
39
+
40
+
41
  def safe_remove(path: str):
42
  try:
43
  os.remove(path)
 
118
  result = DeepFace.verify(
119
  img1_path=selfie_path,
120
  img2_path=gallery_path,
121
+ model_name="Facenet512",
122
+ detector_backend="opencv",
123
+ enforce_detection=False
124
  )
125
  if result.get("verified", False):
126
  true_count += 1
 
128
  return JSONResponse(content={"verified": True, "image": image})
129
  except Exception as e:
130
  msg = str(e)
131
+ root_cause = extract_root_cause(e)
132
  print(f"DeepFace verification error for {image}: {msg}")
133
  if "img1_path" in msg:
134
+ log_event("img1_path_error", gallery_image=image, error=msg, root_cause=root_cause)
135
  if "Face could not be detected" in msg or "No face" in msg:
136
+ log_event("face_not_detected", gallery_image=image, error=msg, root_cause=root_cause)
137
  # Fallback path on generic processing or face-detection errors.
138
  if "img1_path" in msg or "Face could not be detected" in msg or "No face" in msg:
139
  try:
140
  result = DeepFace.verify(
141
  img1_path=selfie_path,
142
  img2_path=gallery_path,
143
+ model_name="VGG-Face",
144
  detector_backend="opencv",
145
  enforce_detection=False
146
  )
 
150
  return JSONResponse(content={"verified": True, "image": image})
151
  except Exception as e2:
152
  print(f"DeepFace fallback error for {image}: {e2}")
153
+ log_event("fallback_error", gallery_image=image, error=str(e2), root_cause=extract_root_cause(e2))
154
  finally:
155
  if gallery_is_temp and gallery_path:
156
  safe_remove(gallery_path)