Upload 3 files
Browse files- app.py +73 -0
- requirements.txt +4 -0
- yolov5n6.pt +3 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load YOLOv5n6 model
|
| 8 |
+
model = YOLO('yolov5n6.pt')
|
| 9 |
+
|
| 10 |
+
# Set the confidence threshold and IOU
|
| 11 |
+
model.conf = 0.25 # confidence threshold
|
| 12 |
+
model.iou = 0.45 # IOU threshold
|
| 13 |
+
model.agnostic = False
|
| 14 |
+
model.multi_label = False
|
| 15 |
+
model.max_det = 100 # max number of detections
|
| 16 |
+
|
| 17 |
+
# Low-resolution for inference
|
| 18 |
+
LOW_RES = (320, 180)
|
| 19 |
+
|
| 20 |
+
def detect_and_draw(frame):
|
| 21 |
+
# Create low-res copy
|
| 22 |
+
low_res_frame = cv2.resize(frame, LOW_RES)
|
| 23 |
+
|
| 24 |
+
# Perform detection
|
| 25 |
+
results = model(low_res_frame, verbose=False)
|
| 26 |
+
|
| 27 |
+
# Scale bounding boxes
|
| 28 |
+
scale_x = frame.shape[1] / LOW_RES[0]
|
| 29 |
+
scale_y = frame.shape[0] / LOW_RES[1]
|
| 30 |
+
|
| 31 |
+
# Draw bounding boxes on high-res frame
|
| 32 |
+
for detection in results[0].boxes.data:
|
| 33 |
+
x1, y1, x2, y2, conf, cls = detection
|
| 34 |
+
x1, y1, x2, y2 = int(x1*scale_x), int(y1*scale_y), int(x2*scale_x), int(y2*scale_y)
|
| 35 |
+
label = f"{results[0].names[int(cls)]} {conf:.2f}"
|
| 36 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 37 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 38 |
+
|
| 39 |
+
return frame
|
| 40 |
+
|
| 41 |
+
# Define your stream URL
|
| 42 |
+
stream_url = "https://edge01.london.nginx.hdontap.com/hosb5/ng_showcase-coke_bottle-street_fixed.stream/chunklist_w464099566.m3u8"
|
| 43 |
+
|
| 44 |
+
def process_stream():
|
| 45 |
+
cap = cv2.VideoCapture(stream_url)
|
| 46 |
+
frame_count = 0
|
| 47 |
+
while cap.isOpened():
|
| 48 |
+
ret, frame = cap.read()
|
| 49 |
+
if not ret:
|
| 50 |
+
break
|
| 51 |
+
|
| 52 |
+
frame_count += 3
|
| 53 |
+
if frame_count % 30 == 0:
|
| 54 |
+
result = detect_and_draw(frame)
|
| 55 |
+
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 56 |
+
yield result_rgb
|
| 57 |
+
|
| 58 |
+
cap.release()
|
| 59 |
+
|
| 60 |
+
iface = gr.Interface(
|
| 61 |
+
fn=process_stream,
|
| 62 |
+
inputs=None,
|
| 63 |
+
outputs="image",
|
| 64 |
+
live=True,
|
| 65 |
+
title="Fast Real-time Object Detection with High-Res Output",
|
| 66 |
+
description="Live stream processed with YOLOv5n6 on low-res frames, results drawn on high-res frames."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
if torch.cuda.is_available():
|
| 71 |
+
model.to('cuda')
|
| 72 |
+
#iface.queue()
|
| 73 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
opencv-python
|
| 3 |
+
gradio
|
| 4 |
+
torch
|
yolov5n6.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:496c05a2b991da15acc6c4408c63da287f2513a46c6756618776d4dd71170781
|
| 3 |
+
size 7190229
|