| | from ultralytics import YOLO |
| | import cv2 |
| | import gradio as gr |
| | import torch |
| |
|
| | |
| | model = YOLO('yolov8n.pt') |
| |
|
| | |
| | stream_url = "https://edge01.london.nginx.hdontap.com/hosb5/ng_showcase-coke_bottle-street_fixed.stream/chunklist_w464099566.m3u8" |
| |
|
| | |
| | LOW_RES = (320, 180) |
| |
|
| | def detect_and_draw(frame): |
| | |
| | low_res_frame = cv2.resize(frame, LOW_RES) |
| |
|
| | |
| | results = model(low_res_frame) |
| |
|
| | |
| | scale_x = frame.shape[1] / LOW_RES[0] |
| | scale_y = frame.shape[0] / LOW_RES[1] |
| |
|
| | |
| | for detection in results[0].boxes.data: |
| | x1, y1, x2, y2, conf, cls = detection |
| | x1, y1, x2, y2 = int(x1*scale_x), int(y1*scale_y), int(x2*scale_x), int(y2*scale_y) |
| | label = f"{results[0].names[int(cls)]} {conf:.2f}" |
| | cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) |
| | cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) |
| |
|
| | return frame |
| |
|
| | def process_stream(): |
| | cap = cv2.VideoCapture(stream_url) |
| | frame_count = 0 |
| | while cap.isOpened(): |
| | ret, frame = cap.read() |
| | if not ret: |
| | break |
| |
|
| | frame_count += 3 |
| | if frame_count % 30 == 0: |
| | result = detect_and_draw(frame) |
| | result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB) |
| | yield result_rgb |
| |
|
| | cap.release() |
| |
|
| | |
| | iface = gr.Interface( |
| | fn=process_stream, |
| | inputs=None, |
| | outputs="image", |
| | live=True, |
| | title="YOLOv8 Real-Time Object Detection", |
| | description="Live stream processed with YOLOv8 for real-time object detection." |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | if torch.cuda.is_available(): |
| | model.to('cuda') |
| | iface.queue() |
| | iface.launch() |
| |
|
| |
|