hb-setosys commited on
Commit
97bc949
·
verified ·
1 Parent(s): 0303123

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -2
app.py CHANGED
@@ -3,11 +3,52 @@ from ultralytics import YOLO
3
  import cv2
4
  from deep_sort_realtime.deepsort_tracker import DeepSort
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Initialize YOLO model
7
  model = YOLO("setosys_ppl_in_video_small_v1.pt") # Load model
8
- tracker = DeepSort(max_age=30, n_init=3, nn_budget=100)
9
 
10
- def count_people_in_video(video_path):
11
  cap = cv2.VideoCapture(video_path) # Load video
12
  total_ids = set() # Track unique IDs
13
 
 
3
  import cv2
4
  from deep_sort_realtime.deepsort_tracker import DeepSort
5
 
6
+ def initialize_tracker(max_age=30, n_init=3, nn_budget=100):
7
+ return DeepSort(max_age=max_age, n_init=n_init, nn_budget=nn_budget)
8
+
9
+ def detect_people(model, frame, confidence_threshold=0.5):
10
+ results = model(frame, device="cpu") # Force CPU if CUDA is unavailable
11
+ detections = []
12
+ for result in results:
13
+ for box, cls, conf in zip(result.boxes.xyxy, result.boxes.cls, result.boxes.conf):
14
+ if result.names[int(cls)] == "person" and conf > confidence_threshold:
15
+ x1, y1, x2, y2 = map(int, box)
16
+ bbox = [x1, y1, x2 - x1, y2 - y1]
17
+ detections.append((bbox, conf, "person"))
18
+ return detections
19
+
20
+ def count_people_in_video(video_path, model_path="setosys_ppl_in_video_small_v1.pt", confidence_threshold=0.5):
21
+ logging.basicConfig(level=logging.INFO)
22
+ cap = cv2.VideoCapture(video_path)
23
+ model = YOLO(model_path) # Auto-detect device during model loading
24
+ tracker = initialize_tracker()
25
+
26
+ total_ids = set()
27
+ frame_count = 0
28
+
29
+ while cap.isOpened():
30
+ ret, frame = cap.read()
31
+ if not ret:
32
+ break
33
+
34
+ frame_count += 1
35
+ logging.info(f"Processing frame {frame_count}")
36
+ detections = detect_people(model, frame, confidence_threshold)
37
+ tracks = tracker.update_tracks(detections, frame=frame)
38
+
39
+ for track in tracks:
40
+ if track.is_confirmed():
41
+ total_ids.add(track.track_id)
42
+
43
+ cap.release()
44
+ return len(total_ids)
45
+
46
+
47
  # Initialize YOLO model
48
  model = YOLO("setosys_ppl_in_video_small_v1.pt") # Load model
49
+ #tracker = DeepSort(max_age=30, n_init=3, nn_budget=100)
50
 
51
+ def count_people_in_video_old(video_path):
52
  cap = cv2.VideoCapture(video_path) # Load video
53
  total_ids = set() # Track unique IDs
54