KRISH09bha commited on
Commit
eeb2e13
·
verified ·
1 Parent(s): 7dcc681

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -4
app.py CHANGED
@@ -26,13 +26,12 @@ detected_people_history = deque(maxlen=MAX_HISTORY)
26
 
27
  DISTANCE_THRESHOLD = 30 # cm
28
 
29
-
30
  @app.post("/detect")
31
  async def detect_faces(file: UploadFile = File(...)):
32
  # Read the image and decode it efficiently
33
  contents = await file.read()
34
  image_np = np.frombuffer(contents, np.uint8)
35
- frame = cv2.imdecode(image_np, cv2.IMREAD_REDUCED_COLOR_2) # Faster decoding
36
 
37
  # Resize frame for faster inference
38
  h, w, _ = frame.shape
@@ -45,7 +44,7 @@ async def detect_faces(file: UploadFile = File(...)):
45
  change_detected = False
46
  person_id = 1
47
 
48
- frame_width = resized_frame.shape[1]
49
 
50
  for result in results:
51
  for box in result.boxes.data.tolist(): # Convert tensor to list
@@ -56,7 +55,7 @@ async def detect_faces(file: UploadFile = File(...)):
56
  center_x = (x1 + x2) // 2
57
  face_width_pixels = x2 - x1
58
 
59
- # Determine position (Left, Center, Right)
60
  if center_x < frame_width // 3:
61
  position = "Left"
62
  elif center_x > 2 * frame_width // 3:
@@ -96,3 +95,5 @@ async def detect_faces(file: UploadFile = File(...)):
96
  else:
97
  return {"people": []} # No significant change detected
98
 
 
 
 
26
 
27
  DISTANCE_THRESHOLD = 30 # cm
28
 
 
29
  @app.post("/detect")
30
  async def detect_faces(file: UploadFile = File(...)):
31
  # Read the image and decode it efficiently
32
  contents = await file.read()
33
  image_np = np.frombuffer(contents, np.uint8)
34
+ frame = cv2.imdecode(image_np, cv2.IMREAD_COLOR) # Decode image correctly
35
 
36
  # Resize frame for faster inference
37
  h, w, _ = frame.shape
 
44
  change_detected = False
45
  person_id = 1
46
 
47
+ frame_width = frame.shape[1] # Use the original frame width
48
 
49
  for result in results:
50
  for box in result.boxes.data.tolist(): # Convert tensor to list
 
55
  center_x = (x1 + x2) // 2
56
  face_width_pixels = x2 - x1
57
 
58
+ # Determine position (Left, Center, Right) using original frame width
59
  if center_x < frame_width // 3:
60
  position = "Left"
61
  elif center_x > 2 * frame_width // 3:
 
95
  else:
96
  return {"people": []} # No significant change detected
97
 
98
+ if __name__ == "__main__":
99
+ uvicorn.run(app, host="0.0.0.0", port=8000)