File size: 3,031 Bytes
9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 9e147b4 6ef8707 b0c01c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import gradio as gr
import cv2
import numpy as np
from deep_sort_realtime.deepsort_tracker import DeepSort
# Load YOLO model and configuration
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Initialize DeepSORT tracker
tracker = DeepSort(max_age=30, n_init=3, nn_budget=20)
def count_unique_people(video_path):
# Open video
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return "Error: Unable to open video file."
unique_people = set() # To store unique IDs
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
height, width, _ = frame.shape
# Detect people using YOLO
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
output_layers_names = net.getUnconnectedOutLayersNames()
layer_outputs = net.forward(output_layers_names)
boxes = []
confidences = []
for output in layer_outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
# If detected class is 'person'
if classes[class_id] == "person" and confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
# Apply non-maximum suppression
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
detections = []
if len(indexes) > 0:
for i in indexes.flatten():
x, y, w, h = boxes[i]
detections.append(([x, y, x + w, y + h], confidences[i]))
# Update tracker with detections
tracks = tracker.update_tracks(detections, frame=frame)
# Track unique IDs
for track in tracks:
if not track.is_confirmed():
continue
track_id = track.track_id
unique_people.add(track_id)
cap.release()
return {
"Total Unique People Detected": len(unique_people),
"Total Frames Processed": frame_count,
}
# Gradio Interface
description = """
Upload a video, and the app will count the total number of unique people detected in the video using YOLO and DeepSORT.
"""
interface = gr.Interface(
fn=count_unique_people,
inputs=gr.Video(label="Upload Video"),
outputs=gr.JSON(label="Unique People Count"),
title="Unique People Counter",
description=description,
)
if __name__ == "__main__":
interface.launch()
|