Spaces:
Runtime error
Runtime error
File size: 4,060 Bytes
22a4da2 97ed606 32d34f8 2b463fa 5c4768f c57adfe 377c851 e165b9e 2a1d794 a750237 e165b9e 570e1aa e165b9e 2a1d794 097bdac bd00d70 a750237 bd00d70 70d24d0 640e331 a750237 bd00d70 2a2ae7e bd00d70 ea778b2 ee5dea8 ea778b2 8cd14af ea778b2 70da591 058d3f3 fe0193c 058d3f3 754b1cc 70da591 754b1cc bd00d70 476af94 8cd14af eb7cee4 097bdac e1f13a6 2a2ae7e 8778d00 ea778b2 8778d00 7c932a9 70da591 2a2ae7e eb7cee4 06ee771 034b367 2a2ae7e a750237 2a2ae7e f936f06 96da659 |
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 96 97 98 99 100 101 102 |
import gradio as gr
import time
import cv2 # opencv2 package for python.
import torch
from pytube import YouTube
from ultralyticsplus import YOLO, render_result
model = YOLO('ultralyticsplus/yolov8s')
device = 'cuda' if torch.cuda.is_available() else 'cpu'
URL = "https://www.youtube.com/watch?v=6NBwbKMyzEE" #URL to parse
# set model parameters
model.overrides['conf'] = 0.50 # NMS confidence threshold
model.overrides['iou'] = 0.45 # NMS IoU threshold
model.overrides['agnostic_nms'] = False # NMS class-agnostic
model.overrides['max_det'] = 1000 # maximum number of detections per image
model.to(device)
def load(URL):
yt = YouTube(URL)
vid_cap = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().last().download(filename="tmp.mp4")
global player
player = cv2.VideoCapture(vid_cap)
frame_num = int(player.get(cv2.CAP_PROP_POS_FRAMES))
frame_count = int(player.get(cv2.CAP_PROP_FRAME_COUNT))
frame_fps = (player.get(cv2.CAP_PROP_FPS))
tog = 0
return vid_cap,frame_num,frame_count,frame_fps,tog
def vid_play(cap,frame_num):
assert player.isOpened() # Make sure that their is a stream.
player.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num))
ret, frame_bgr = player.read(int(frame_num))
frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
results = model.predict(frame)
render = render_result(model=model, image=frame, result=results[0])
return render
def fw_fn(cur,last):
next = cur+1
if next > last:
next = last
return next
def bk_fn(cur):
next = cur-1
if next < 0:
next = 0
return next
def tog_on():
return 1,gr.Markdown.update("""<center><h7>Status: Playing 😁</h7></center>""")
def tog_off():
return 0,gr.Markdown.update("""<center><h7>Status: Stopped 💀</h7></center>""")
def pl_fn(cap,cur,last,fps,pl_tog):
player.set(cv2.CAP_PROP_POS_FRAMES, cur)
ret, frame_bgr = player.read(cur)
frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
results = model.predict(frame)
render = render_result(model=model, image=frame, result=results[0])
if pl_tog ==1:
cur+=1
else:
cur = cur
return render,cur
with gr.Blocks() as app:
gr.Markdown("""<center><h1>Slow Video Object Detection</h1><h4>Gradio and ultralyticsplus/yolov8s</h4><h4>Probably faster on GPU 🤷♂️</h4></center>""")
play_state = gr.Markdown("""<right><h7></h7></right>""")
with gr.Row():
with gr.Column():
youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
load_button = gr.Button("Load Video")
output_win = gr.Video()
with gr.Column():
with gr.Row():
cur_frame = gr.Number(label="Current Frame")
fps_frames = gr.Number(label="Video FPS",interactive=False)
total_frames = gr.Number(label="Total Frames",interactive=False)
#run_button = gr.Button()
with gr.Row():
bk = gr.Button("<")
pl = gr.Button("Play")
st = gr.Button("Stop")
fw = gr.Button(">")
det_win = gr.Image(source="webcam", streaming=True)
with gr.Row():
pl_tog=gr.Number(visible=False)
ins_cnt=gr.Number(visible=False)
pl.click(tog_on,None,[pl_tog,play_state],show_progress=False)
st.click(tog_off,None,[pl_tog,play_state],show_progress=False)
pl_tog.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
cur_frame.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
bk.click(bk_fn,[cur_frame],cur_frame,show_progress=False)
fw.click(fw_fn,[cur_frame,total_frames],cur_frame,show_progress=False)
load_button.click(load,youtube_url,[output_win,cur_frame,total_frames,fps_frames,pl_tog])
#run_button.click(vid_play, [output_win,cur_frame], det_win)
app.queue(concurrency_count=10).launch() |