Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
-
import
|
|
|
5 |
|
6 |
-
# Load
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
#
|
11 |
-
#net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
|
12 |
-
|
13 |
-
# Set backend to CPU
|
14 |
-
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
|
15 |
-
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
|
16 |
-
|
17 |
-
# Load class names
|
18 |
-
with open('coco.names', 'r') as f:
|
19 |
-
classes = [line.strip() for line in f.readlines()]
|
20 |
-
|
21 |
-
# Get YOLO output layer names
|
22 |
-
output_layers_names = net.getUnconnectedOutLayersNames()
|
23 |
-
|
24 |
-
def count_people(video_path):
|
25 |
-
if not os.path.exists(video_path):
|
26 |
-
return "Error: Video file not found."
|
27 |
|
|
|
28 |
cap = cv2.VideoCapture(video_path)
|
29 |
if not cap.isOpened():
|
30 |
return "Error: Unable to open video file."
|
31 |
|
32 |
frame_count = 0
|
33 |
-
|
|
|
34 |
|
35 |
while True:
|
36 |
ret, frame = cap.read()
|
37 |
if not ret:
|
38 |
-
break
|
39 |
-
|
40 |
-
height, width, _ = frame.shape
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# Forward pass
|
47 |
-
layer_outputs = net.forward(output_layers_names)
|
48 |
-
|
49 |
-
# Process detections
|
50 |
-
boxes, confidences = [], []
|
51 |
-
for output in layer_outputs:
|
52 |
-
for detection in output:
|
53 |
-
scores = detection[5:]
|
54 |
-
class_id = np.argmax(scores)
|
55 |
-
confidence = scores[class_id]
|
56 |
-
|
57 |
-
if classes[class_id] == 'person' and confidence > 0.5:
|
58 |
-
center_x, center_y = int(detection[0] * width), int(detection[1] * height)
|
59 |
-
w, h = int(detection[2] * width), int(detection[3] * height)
|
60 |
-
x, y = int(center_x - w / 2), int(center_y - h / 2)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
|
73 |
cap.release()
|
74 |
|
75 |
-
# Generate analytics
|
76 |
return {
|
77 |
-
|
78 |
-
"
|
79 |
-
#"Avg People Per Frame": round(np.mean(people_per_frame), 2) if people_per_frame else 0
|
80 |
}
|
81 |
|
82 |
# Gradio UI function
|
83 |
def analyze_video(video_file):
|
84 |
-
result =
|
85 |
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
86 |
|
87 |
# Gradio Interface
|
88 |
interface = gr.Interface(
|
89 |
fn=analyze_video,
|
90 |
inputs=gr.Video(label="Upload Video"),
|
91 |
-
outputs=gr.Textbox(label="
|
92 |
-
title="
|
93 |
-
description="Upload a video to detect and count
|
94 |
)
|
95 |
|
96 |
# Launch app
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
+
import torch
|
5 |
+
from ultralytics import YOLO
|
6 |
|
7 |
+
# Load YOLOv8 model (pre-trained on COCO dataset)
|
8 |
+
model = YOLO("yolov8x.pt") # Use "yolov8x.pt" for highest accuracy
|
9 |
|
10 |
+
# Class label for trucks (COCO dataset)
|
11 |
+
TRUCK_CLASS_ID = 7 # "truck" in COCO dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
def count_trucks(video_path):
|
14 |
cap = cv2.VideoCapture(video_path)
|
15 |
if not cap.isOpened():
|
16 |
return "Error: Unable to open video file."
|
17 |
|
18 |
frame_count = 0
|
19 |
+
truck_count_per_frame = []
|
20 |
+
frame_skip = 5 # Process every 5th frame for efficiency
|
21 |
|
22 |
while True:
|
23 |
ret, frame = cap.read()
|
24 |
if not ret:
|
25 |
+
break # End of video
|
|
|
|
|
26 |
|
27 |
+
frame_count += 1
|
28 |
+
if frame_count % frame_skip != 0:
|
29 |
+
continue # Skip frames to improve efficiency
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Run YOLOv8 inference
|
32 |
+
results = model(frame, verbose=False)
|
33 |
|
34 |
+
truck_count = 0
|
35 |
+
for result in results:
|
36 |
+
for box in result.boxes:
|
37 |
+
class_id = int(box.cls.item()) # Get class ID
|
38 |
+
confidence = float(box.conf.item()) # Get confidence score
|
39 |
|
40 |
+
if class_id == TRUCK_CLASS_ID and confidence > 0.6:
|
41 |
+
truck_count += 1 # Count trucks with confidence > 0.6
|
42 |
|
43 |
+
truck_count_per_frame.append(truck_count)
|
44 |
|
45 |
cap.release()
|
46 |
|
|
|
47 |
return {
|
48 |
+
"Total Trucks in Video": int(np.max(truck_count_per_frame)) if truck_count_per_frame else 0,
|
49 |
+
"Avg Trucks Per Frame": round(np.mean(truck_count_per_frame), 2) if truck_count_per_frame else 0
|
|
|
50 |
}
|
51 |
|
52 |
# Gradio UI function
|
53 |
def analyze_video(video_file):
|
54 |
+
result = count_trucks(video_file)
|
55 |
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
56 |
|
57 |
# Gradio Interface
|
58 |
interface = gr.Interface(
|
59 |
fn=analyze_video,
|
60 |
inputs=gr.Video(label="Upload Video"),
|
61 |
+
outputs=gr.Textbox(label="Truck Counting Results"),
|
62 |
+
title="YOLOv8-based Truck Counter",
|
63 |
+
description="Upload a video to detect and count trucks using YOLOv8."
|
64 |
)
|
65 |
|
66 |
# Launch app
|