Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,53 +4,64 @@ import numpy as np
|
|
4 |
from fastapi import FastAPI, File, UploadFile
|
5 |
from ultralytics import YOLO
|
6 |
from typing import List, Dict
|
7 |
-
import
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
# Load YOLO
|
12 |
-
model = YOLO("yolov11s-face.pt")
|
13 |
|
14 |
-
# Constants for Distance
|
15 |
KNOWN_DISTANCE = 50 # cm
|
16 |
KNOWN_FACE_WIDTH = 14 # cm
|
17 |
-
REF_IMAGE_FACE_WIDTH = 120 #
|
18 |
FOCAL_LENGTH = (REF_IMAGE_FACE_WIDTH * KNOWN_DISTANCE) / KNOWN_FACE_WIDTH
|
19 |
-
SCALING_FACTOR = 2.0 # Adjust based on testing
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
# Run YOLO detection
|
31 |
-
results = model(frame)
|
32 |
-
|
33 |
-
faces_detected = []
|
34 |
-
|
35 |
-
for i, result in enumerate(results):
|
36 |
-
for j, box in enumerate(result.boxes):
|
37 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
38 |
conf = box.conf[0].item()
|
39 |
-
|
40 |
if conf > 0.5: # Confidence threshold
|
41 |
center_x = (x1 + x2) // 2
|
42 |
-
face_width_pixels = x2 - x1
|
43 |
-
|
44 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
if face_width_pixels > 0:
|
46 |
estimated_distance = (FOCAL_LENGTH * KNOWN_FACE_WIDTH) / face_width_pixels
|
47 |
-
estimated_distance *= SCALING_FACTOR
|
48 |
else:
|
49 |
estimated_distance = -1 # Error case
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
4 |
from fastapi import FastAPI, File, UploadFile
|
5 |
from ultralytics import YOLO
|
6 |
from typing import List, Dict
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Load YOLO Model
|
12 |
+
model = YOLO("yolov11s-face.pt")
|
13 |
|
14 |
+
# Constants for Distance Calculation
|
15 |
KNOWN_DISTANCE = 50 # cm
|
16 |
KNOWN_FACE_WIDTH = 14 # cm
|
17 |
+
REF_IMAGE_FACE_WIDTH = 120 # pixels
|
18 |
FOCAL_LENGTH = (REF_IMAGE_FACE_WIDTH * KNOWN_DISTANCE) / KNOWN_FACE_WIDTH
|
19 |
+
SCALING_FACTOR = 2.0 # Adjust based on real-world testing
|
20 |
|
21 |
+
# Function to Process Frame & Detect Faces
|
22 |
+
def process_frame(image: np.ndarray) -> List[Dict]:
|
23 |
+
results = model(image)
|
24 |
+
frame_width = image.shape[1]
|
25 |
+
people = []
|
26 |
+
|
27 |
+
for idx, result in enumerate(results):
|
28 |
+
for i, box in enumerate(result.boxes):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
30 |
conf = box.conf[0].item()
|
31 |
+
|
32 |
if conf > 0.5: # Confidence threshold
|
33 |
center_x = (x1 + x2) // 2
|
34 |
+
face_width_pixels = x2 - x1
|
35 |
+
|
36 |
+
# Determine Position
|
37 |
+
if center_x < frame_width // 3:
|
38 |
+
position = "Left"
|
39 |
+
elif center_x > 2 * frame_width // 3:
|
40 |
+
position = "Right"
|
41 |
+
else:
|
42 |
+
position = "Center"
|
43 |
+
|
44 |
+
# Estimate Distance
|
45 |
if face_width_pixels > 0:
|
46 |
estimated_distance = (FOCAL_LENGTH * KNOWN_FACE_WIDTH) / face_width_pixels
|
47 |
+
estimated_distance *= SCALING_FACTOR
|
48 |
else:
|
49 |
estimated_distance = -1 # Error case
|
50 |
+
|
51 |
+
people.append({
|
52 |
+
f"person{i+1}": {
|
53 |
+
"distance": round(estimated_distance, 1),
|
54 |
+
"position": position
|
55 |
+
}
|
56 |
+
})
|
57 |
+
|
58 |
+
return people
|
59 |
|
60 |
+
@app.post("/detect")
|
61 |
+
async def detect_faces(file: UploadFile = File(...)):
|
62 |
+
image_data = await file.read()
|
63 |
+
nparr = np.frombuffer(image_data, np.uint8)
|
64 |
+
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
65 |
+
|
66 |
+
faces = process_frame(image)
|
67 |
+
return {"people": faces}
|