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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -8
app.py CHANGED
@@ -18,11 +18,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):
@@ -48,14 +54,34 @@ def process_frame(image: np.ndarray) -> List[Dict]:
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(...)):
 
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
+ # Previous state storage
22
+ previous_people = {}
23
+ THRESHOLD_DISTANCE_CHANGE = 50 # cm
24
+
25
  # Function to Process Frame & Detect Faces
26
  def process_frame(image: np.ndarray) -> List[Dict]:
27
+ global previous_people
28
  results = model(image)
29
  frame_width = image.shape[1]
30
+ current_people = {}
31
+ detected_people = []
32
 
33
  for idx, result in enumerate(results):
34
  for i, box in enumerate(result.boxes):
 
54
  else:
55
  estimated_distance = -1 # Error case
56
 
57
+ person_id = f"person{i+1}"
58
+ current_people[person_id] = {
59
+ "distance": round(estimated_distance, 1),
60
+ "position": position
61
+ }
62
+
63
+ # Compare with previous state
64
+ if previous_people:
65
+ changes_detected = False
66
+ for person_id, data in current_people.items():
67
+ prev_data = previous_people.get(person_id)
68
+ if not prev_data or abs(prev_data["distance"] - data["distance"]) > THRESHOLD_DISTANCE_CHANGE or prev_data["position"] != data["position"]:
69
+ changes_detected = True
70
+ break
71
+
72
+ # Check if any person entered or left
73
+ if set(current_people.keys()) != set(previous_people.keys()):
74
+ changes_detected = True
75
+
76
+ if not changes_detected:
77
+ return []
78
+
79
+ previous_people = current_people.copy()
80
+
81
+ for person_id, data in current_people.items():
82
+ detected_people.append({person_id: data})
83
 
84
+ return detected_people
85
 
86
  @app.post("/detect")
87
  async def detect_faces(file: UploadFile = File(...)):