skibi11 commited on
Commit
18f7e14
·
verified ·
1 Parent(s): bb3508c

fix mirrored picture problem

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -106,11 +106,32 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
106
  if not detect_faces_roboflow(temp_image_path):
107
  return JSONResponse(status_code=400, content={"error": "No face detected."})
108
 
 
 
109
  eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, raw_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  if error_msg or len(eye_crops) != 2:
111
- # This change returns a proper error that your frontend can catch.
112
  return JSONResponse(
113
- status_code=400,
114
  content={"error": "Could not detect exactly two eyes. Please try another photo."}
115
  )
116
 
 
106
  if not detect_faces_roboflow(temp_image_path):
107
  return JSONResponse(status_code=400, content={"error": "No face detected."})
108
 
109
+ # First, try to detect eyes on the original image
110
+ print("--- Attempting detection on original image... ---")
111
  eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, raw_image)
112
+
113
+ # If that fails, create a mirrored version and try again
114
+ if len(eye_crops) != 2:
115
+ print("--- Original failed. Attempting detection on mirrored image... ---")
116
+ mirrored_image = cv2.flip(raw_image, 1) # 1 means a horizontal flip
117
+
118
+ # We need a new temporary file for the mirrored image to send to Roboflow
119
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_mirrored:
120
+ cv2.imwrite(tmp_mirrored.name, mirrored_image)
121
+ temp_mirrored_image_path = tmp_mirrored.name
122
+
123
+ try:
124
+ # Run detection again on the new mirrored image
125
+ # NOTE: We use mirrored_image for cropping, not raw_image
126
+ eye_crops, error_msg = detect_eyes_roboflow(temp_mirrored_image_path, mirrored_image)
127
+ finally:
128
+ # Always clean up the second temporary file
129
+ os.remove(temp_mirrored_image_path)
130
+
131
+ # Finally, after trying both original and mirrored, check if we have a valid result
132
  if error_msg or len(eye_crops) != 2:
 
133
  return JSONResponse(
134
+ status_code=400,
135
  content={"error": "Could not detect exactly two eyes. Please try another photo."}
136
  )
137