Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,6 @@ app = FastAPI()
|
|
23 |
# Optimized tracking of previous detections using a deque
|
24 |
MAX_HISTORY = 10
|
25 |
detected_people_history = deque(maxlen=MAX_HISTORY)
|
26 |
-
DISTANCE_THRESHOLD = 30 # cm
|
27 |
|
28 |
@app.post("/detect")
|
29 |
async def detect_faces(file: UploadFile = File(...)):
|
@@ -42,6 +41,7 @@ async def detect_faces(file: UploadFile = File(...)):
|
|
42 |
new_people_data = {}
|
43 |
person_id = 1
|
44 |
frame_width = resized_frame.shape[1]
|
|
|
45 |
|
46 |
for result in results:
|
47 |
for box in result.boxes.data.tolist(): # Convert tensor to list
|
@@ -52,12 +52,13 @@ async def detect_faces(file: UploadFile = File(...)):
|
|
52 |
center_x = (x1 + x2) // 2
|
53 |
face_width_pixels = x2 - x1
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
# Calculate distance
|
63 |
estimated_distance = (
|
|
|
23 |
# Optimized tracking of previous detections using a deque
|
24 |
MAX_HISTORY = 10
|
25 |
detected_people_history = deque(maxlen=MAX_HISTORY)
|
|
|
26 |
|
27 |
@app.post("/detect")
|
28 |
async def detect_faces(file: UploadFile = File(...)):
|
|
|
41 |
new_people_data = {}
|
42 |
person_id = 1
|
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
|
|
|
52 |
center_x = (x1 + x2) // 2
|
53 |
face_width_pixels = x2 - x1
|
54 |
|
55 |
+
# Corrected position logic
|
56 |
+
if center_x < frame_center - (frame_width * 0.1): # 10% margin for accuracy
|
57 |
+
position = "Left"
|
58 |
+
elif center_x > frame_center + (frame_width * 0.1):
|
59 |
+
position = "Right"
|
60 |
+
else:
|
61 |
+
position = "Center"
|
62 |
|
63 |
# Calculate distance
|
64 |
estimated_distance = (
|