hb-setosys commited on
Commit
d0e540e
·
verified ·
1 Parent(s): 207e155

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ from deep_sort_realtime.deepsort_tracker import DeepSort
5
+ import tempfile
6
+
7
+ # Initialize YOLO model
8
+ model = YOLO("yolov8l.pt") # Load YOLOv8 model
9
+ tracker = DeepSort(max_age=30, n_init=3, nn_budget=100)
10
+
11
+ def count_people_in_video(video_file):
12
+ cap = cv2.VideoCapture(video_file) # Load video
13
+ total_ids = set() # Track unique IDs
14
+
15
+ while cap.isOpened():
16
+ ret, frame = cap.read()
17
+ if not ret:
18
+ break
19
+
20
+ # Run YOLO inference on the frame
21
+ results = model(frame)
22
+ detections = []
23
+
24
+ # Parse YOLO detections
25
+ for result in results:
26
+ for box, cls, conf in zip(result.boxes.xyxy, result.boxes.cls, result.boxes.conf):
27
+ if result.names[int(cls)] == "person" and conf > 0.5: # Detect "person" class
28
+ x1, y1, x2, y2 = map(int, box)
29
+ bbox = [x1, y1, x2 - x1, y2 - y1] # Convert to [x, y, width, height]
30
+ detections.append((bbox, conf, "person"))
31
+
32
+ # Update DeepSORT tracker with detections
33
+ tracks = tracker.update_tracks(detections, frame=frame)
34
+
35
+ # Add unique IDs from confirmed tracks
36
+ for track in tracks:
37
+ if track.is_confirmed():
38
+ total_ids.add(track.track_id)
39
+
40
+ cap.release()
41
+ return len(total_ids)
42
+
43
+ # Gradio Interface
44
+ def process_video(video_file):
45
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
46
+ temp_file.write(video_file.read())
47
+ temp_file.flush()
48
+ total_people = count_people_in_video(temp_file.name)
49
+ return f"Total unique people in the video: {total_people}"
50
+
51
+ interface = gr.Interface(
52
+ fn=process_video,
53
+ inputs=gr.Video(label="Upload a Video"),
54
+ outputs="text",
55
+ title="Person Counting with YOLOv8 and DeepSORT",
56
+ description="Upload a video to count the number of unique people using YOLOv8 and DeepSORT."
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ interface.launch()