KRISH09bha commited on
Commit
7e44a3d
·
verified ·
1 Parent(s): 44c12e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -33
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 uvicorn
8
 
9
  app = FastAPI()
10
 
11
- # Load YOLO model
12
- model = YOLO("yolov11s-face.pt") # Change model path as needed
13
 
14
- # Constants for Distance Estimation
15
  KNOWN_DISTANCE = 50 # cm
16
  KNOWN_FACE_WIDTH = 14 # cm
17
- REF_IMAGE_FACE_WIDTH = 120 # px (Change this based on calibration)
18
  FOCAL_LENGTH = (REF_IMAGE_FACE_WIDTH * KNOWN_DISTANCE) / KNOWN_FACE_WIDTH
19
- SCALING_FACTOR = 2.0 # Adjust based on testing
20
 
21
- @app.post("/detect")
22
- async def detect_faces(file: UploadFile = File(...)) -> Dict[str, List[Dict[str, str]]]:
23
- # Read image from file
24
- contents = await file.read()
25
- np_arr = np.frombuffer(contents, np.uint8)
26
- frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
27
-
28
- frame_width = frame.shape[1]
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 # Detected face width in pixels
43
-
44
- # Calculate distance
 
 
 
 
 
 
 
 
45
  if face_width_pixels > 0:
46
  estimated_distance = (FOCAL_LENGTH * KNOWN_FACE_WIDTH) / face_width_pixels
47
- estimated_distance *= SCALING_FACTOR # Apply scaling fix
48
  else:
49
  estimated_distance = -1 # Error case
 
 
 
 
 
 
 
 
 
50
 
51
- person_data = {
52
- f"person{j+1}": f"{estimated_distance:.1f} cm"
53
- }
54
- faces_detected.append(person_data)
55
-
56
- return {"faces": faces_detected}
 
 
 
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}