anupamdutta279 commited on
Commit
1c9c796
·
verified ·
1 Parent(s): 02aeeb5

advanced logging and image path fix implemented

Browse files
Files changed (1) hide show
  1. app.py +38 -5
app.py CHANGED
@@ -3,6 +3,8 @@ 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
@@ -13,6 +15,14 @@ class Verify(BaseModel):
13
  images: list[str]
14
 
15
  app = FastAPI()
 
 
 
 
 
 
 
 
16
 
17
  def load_image(source: str):
18
  """
@@ -60,6 +70,7 @@ def verify(v: Verify):
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:
@@ -68,12 +79,15 @@ def verify(v: Verify):
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):
@@ -83,12 +97,31 @@ def verify(v: Verify):
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
  )
@@ -98,4 +131,4 @@ def verify(v: Verify):
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})
 
3
  from pydantic import BaseModel
4
  from fastapi.responses import JSONResponse
5
  import base64
6
+ import json
7
+ import logging
8
  import cv2
9
  import numpy as np
10
  import requests
 
15
  images: list[str]
16
 
17
  app = FastAPI()
18
+ logger = logging.getLogger("plugg_verification")
19
+ if not logger.handlers:
20
+ logging.basicConfig(level=logging.INFO)
21
+
22
+
23
+ def log_event(event_type: str, **fields):
24
+ payload = {"event": event_type, **fields}
25
+ logger.error(json.dumps(payload, default=str))
26
 
27
  def load_image(source: str):
28
  """
 
70
  selfie_img = load_image(selfie)
71
  except Exception as e:
72
  print(f"Failed to load selfie image: {e}")
73
+ log_event("load_error", target="selfie", source=selfie, error=str(e))
74
  return JSONResponse(content={"verified": False, "image": None, "error": "failed_to_load_selfie"})
75
 
76
  for image in gallery:
 
79
  gallery_img = load_image(image)
80
  except Exception as e:
81
  print(f"Failed to load gallery image {image}: {e}")
82
+ log_event("load_error", target="gallery", source=image, error=str(e))
83
  continue
84
  try:
85
+ selfie_candidate = selfie_img.copy()
86
+ gallery_candidate = gallery_img.copy()
87
  # Use a more robust detector; fall back if detection fails
88
  result = DeepFace.verify(
89
+ img1_path=selfie_candidate,
90
+ img2_path=gallery_candidate,
91
  detector_backend="retinaface"
92
  )
93
  if result.get("verified", False):
 
97
  except Exception as e:
98
  msg = str(e)
99
  print(f"DeepFace verification error for {image}: {msg}")
100
+ # Retry once when DeepFace specifically fails while processing img1_path.
101
+ # Re-load selfie to avoid potential in-place mutation side effects.
102
+ if "img1_path" in msg:
103
+ try:
104
+ selfie_retry = load_image(selfie)
105
+ result = DeepFace.verify(
106
+ img1_path=selfie_retry,
107
+ img2_path=gallery_img.copy(),
108
+ detector_backend="retinaface"
109
+ )
110
+ if result.get("verified", False):
111
+ true_count += 1
112
+ if true_count >= 2:
113
+ return JSONResponse(content={"verified": True, "image": image})
114
+ continue
115
+ except Exception as e_retry:
116
+ print(f"DeepFace img1_path retry error for {image}: {e_retry}")
117
+ log_event("img1_path_error", gallery_image=image, error=str(e_retry))
118
  # Optional fallback without strict detection to avoid generic img2_path errors
119
  if "Face could not be detected" in msg or "No face" in msg:
120
+ log_event("face_not_detected", gallery_image=image, error=msg)
121
  try:
122
  result = DeepFace.verify(
123
+ img1_path=selfie_img.copy(),
124
+ img2_path=gallery_img.copy(),
125
  detector_backend="retinaface",
126
  enforce_detection=False
127
  )
 
131
  return JSONResponse(content={"verified": True, "image": image})
132
  except Exception as e2:
133
  print(f"DeepFace fallback error for {image}: {e2}")
134
+ return JSONResponse(content={"verified": False, "image": None})