Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,27 +6,27 @@ from ultralytics import YOLO
|
|
6 |
from fastapi import FastAPI, File, UploadFile
|
7 |
import uvicorn
|
8 |
|
9 |
-
# Load YOLO model
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
model = YOLO("yolov11s-face.pt").to(device)
|
12 |
|
13 |
# Constants for distance estimation
|
14 |
KNOWN_DISTANCE = 50 # cm
|
15 |
KNOWN_FACE_WIDTH = 14 # cm
|
16 |
-
REF_IMAGE_FACE_WIDTH = 120 # Reference face width in pixels
|
17 |
FOCAL_LENGTH = (REF_IMAGE_FACE_WIDTH * KNOWN_DISTANCE) / KNOWN_FACE_WIDTH
|
18 |
SCALING_FACTOR = 2.0 # Adjust based on testing
|
19 |
|
20 |
# FastAPI initialization
|
21 |
app = FastAPI()
|
22 |
|
23 |
-
#
|
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(...)):
|
29 |
-
# Read the image and decode it
|
30 |
contents = await file.read()
|
31 |
image_np = np.frombuffer(contents, np.uint8)
|
32 |
frame = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
|
@@ -41,26 +41,29 @@ 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 |
-
|
|
|
47 |
|
48 |
for result in results:
|
49 |
-
for box in result.boxes
|
50 |
-
x1, y1, x2, y2
|
51 |
-
|
52 |
|
53 |
-
if conf > 0.5:
|
54 |
center_x = (x1 + x2) // 2
|
55 |
-
face_width_pixels = x2 - x1
|
56 |
|
57 |
-
# **
|
58 |
-
if center_x <
|
59 |
position = "Left"
|
60 |
-
|
|
|
61 |
position = "Right"
|
|
|
62 |
else:
|
63 |
-
position = "Center"
|
|
|
64 |
|
65 |
# Calculate distance
|
66 |
estimated_distance = (
|
@@ -77,6 +80,6 @@ async def detect_faces(file: UploadFile = File(...)):
|
|
77 |
# Return only if there's a change
|
78 |
if not detected_people_history or new_people_data != detected_people_history[-1]:
|
79 |
detected_people_history.append(new_people_data)
|
80 |
-
return {"people": new_people_data}
|
81 |
|
82 |
return {"people": []} # No change, return empty response
|
|
|
6 |
from fastapi import FastAPI, File, UploadFile
|
7 |
import uvicorn
|
8 |
|
9 |
+
# Load YOLO model
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
model = YOLO("yolov11s-face.pt").to(device)
|
12 |
|
13 |
# Constants for distance estimation
|
14 |
KNOWN_DISTANCE = 50 # cm
|
15 |
KNOWN_FACE_WIDTH = 14 # cm
|
16 |
+
REF_IMAGE_FACE_WIDTH = 120 # Reference face width in pixels
|
17 |
FOCAL_LENGTH = (REF_IMAGE_FACE_WIDTH * KNOWN_DISTANCE) / KNOWN_FACE_WIDTH
|
18 |
SCALING_FACTOR = 2.0 # Adjust based on testing
|
19 |
|
20 |
# FastAPI initialization
|
21 |
app = FastAPI()
|
22 |
|
23 |
+
# Tracking history of detections
|
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(...)):
|
29 |
+
# Read the image and decode it
|
30 |
contents = await file.read()
|
31 |
image_np = np.frombuffer(contents, np.uint8)
|
32 |
frame = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
|
|
|
41 |
new_people_data = {}
|
42 |
person_id = 1
|
43 |
frame_width = resized_frame.shape[1]
|
|
|
44 |
|
45 |
+
# Define Left, Center, Right based on frame width
|
46 |
+
left_count, center_count, right_count = 0, 0, 0
|
47 |
|
48 |
for result in results:
|
49 |
+
for box in result.boxes:
|
50 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
51 |
+
conf = box.conf[0].item()
|
52 |
|
53 |
+
if conf > 0.5: # Confidence threshold
|
54 |
center_x = (x1 + x2) // 2
|
55 |
+
face_width_pixels = x2 - x1 # Detected face width in pixels
|
56 |
|
57 |
+
# **Updated Position Logic**
|
58 |
+
if center_x < frame_width // 3:
|
59 |
position = "Left"
|
60 |
+
left_count += 1
|
61 |
+
elif center_x > 2 * frame_width // 3:
|
62 |
position = "Right"
|
63 |
+
right_count += 1
|
64 |
else:
|
65 |
+
position = "Center"
|
66 |
+
center_count += 1
|
67 |
|
68 |
# Calculate distance
|
69 |
estimated_distance = (
|
|
|
80 |
# Return only if there's a change
|
81 |
if not detected_people_history or new_people_data != detected_people_history[-1]:
|
82 |
detected_people_history.append(new_people_data)
|
83 |
+
return {"people": new_people_data, "counts": {"Left": left_count, "Center": center_count, "Right": right_count}}
|
84 |
|
85 |
return {"people": []} # No change, return empty response
|