|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
from deep_sort_realtime.deepsort_tracker import DeepSort |
|
|
|
|
|
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") |
|
with open("coco.names", "r") as f: |
|
classes = [line.strip() for line in f.readlines()] |
|
|
|
|
|
tracker = DeepSort(max_age=30, n_init=3, nn_budget=20) |
|
|
|
def count_unique_people(video_path): |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
if not cap.isOpened(): |
|
return "Error: Unable to open video file." |
|
|
|
unique_people = set() |
|
frame_count = 0 |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
frame_count += 1 |
|
height, width, _ = frame.shape |
|
|
|
|
|
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 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)) |
|
|
|
|
|
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])) |
|
|
|
|
|
tracks = tracker.update_tracks(detections, frame=frame) |
|
|
|
|
|
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, |
|
} |
|
|
|
|
|
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() |
|
|