Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,6 +43,8 @@ async def detect_faces(file: UploadFile = File(...)):
|
|
43 |
frame_width = resized_frame.shape[1]
|
44 |
frame_center = frame_width // 2 # Calculate the frame's center
|
45 |
|
|
|
|
|
46 |
for result in results:
|
47 |
for box in result.boxes.data.tolist(): # Convert tensor to list
|
48 |
x1, y1, x2, y2, conf, _ = box[:6] # Extract values
|
@@ -52,13 +54,13 @@ async def detect_faces(file: UploadFile = File(...)):
|
|
52 |
center_x = (x1 + x2) // 2
|
53 |
face_width_pixels = x2 - x1
|
54 |
|
55 |
-
#
|
56 |
-
if center_x < frame_center -
|
57 |
position = "Left"
|
58 |
-
elif center_x > frame_center +
|
59 |
position = "Right"
|
60 |
else:
|
61 |
-
position = "Center"
|
62 |
|
63 |
# Calculate distance
|
64 |
estimated_distance = (
|
|
|
43 |
frame_width = resized_frame.shape[1]
|
44 |
frame_center = frame_width // 2 # Calculate the frame's center
|
45 |
|
46 |
+
center_threshold = frame_width * 0.15 # 15% margin for center detection
|
47 |
+
|
48 |
for result in results:
|
49 |
for box in result.boxes.data.tolist(): # Convert tensor to list
|
50 |
x1, y1, x2, y2, conf, _ = box[:6] # Extract values
|
|
|
54 |
center_x = (x1 + x2) // 2
|
55 |
face_width_pixels = x2 - x1
|
56 |
|
57 |
+
# **Improved Position Logic**
|
58 |
+
if center_x < frame_center - center_threshold:
|
59 |
position = "Left"
|
60 |
+
elif center_x > frame_center + center_threshold:
|
61 |
position = "Right"
|
62 |
else:
|
63 |
+
position = "Center" # Corrected condition for center
|
64 |
|
65 |
# Calculate distance
|
66 |
estimated_distance = (
|