Future-Tense commited on
Commit
2a2ae7e
·
1 Parent(s): 754b1cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -32
app.py CHANGED
@@ -4,13 +4,8 @@ import cv2 # opencv2 package for python.
4
  import torch
5
  from pytube import YouTube
6
  from ultralyticsplus import YOLO, render_result
7
- #from imageai.Detection import ObjectDetection
8
 
9
 
10
- #obj_detect.setModelPath(r"C:/Datasets/yolo.h5")
11
- #obj_detect.loadModel()
12
-
13
- #from torch import hub # Hub contains other models like FasterRCNN
14
  model = YOLO('ultralyticsplus/yolov8s')
15
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
16
  URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" #URL to parse
@@ -22,10 +17,6 @@ model.overrides['agnostic_nms'] = False # NMS class-agnostic
22
  model.overrides['max_det'] = 1000 # maximum number of detections per image
23
  model.to(device)
24
 
25
- #class Player:
26
-
27
-
28
-
29
 
30
  def load(URL):
31
  yt = YouTube(URL)
@@ -36,19 +27,17 @@ def load(URL):
36
  frame_count = int(player.get(cv2.CAP_PROP_FRAME_COUNT))
37
  frame_fps = (player.get(cv2.CAP_PROP_FPS))
38
  #process.release()
39
-
40
  return vid_cap,frame_num,frame_count,frame_fps
41
 
42
  def vid_play(cap,frame_num):
43
- #player = cv2.VideoCapture(cap)
44
  assert player.isOpened() # Make sure that their is a stream.
45
  player.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num))
46
-
47
  ret, frame_bgr = player.read(int(frame_num))
48
  frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
49
  results = model.predict(frame)
50
  render = render_result(model=model, image=frame, result=results[0])
51
  return render
 
52
  def fw_fn(cur,last):
53
  next = cur+1
54
  if next > last:
@@ -65,25 +54,19 @@ def tog_off():
65
  return 0
66
 
67
  def pl_fn(cap,cur,last,fps,pl_tog):
68
- #player = cv2.VideoCapture(cap)
69
- #assert player.isOpened() # Make sure that their is a stream.
70
  player.set(cv2.CAP_PROP_POS_FRAMES, cur)
71
  ret, frame_bgr = player.read(cur)
72
  frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
73
  results = model.predict(frame)
74
  render = render_result(model=model, image=frame, result=results[0])
75
  if pl_tog ==1:
76
- #ins_cnt+=1
77
  cur+=1
78
  else:
79
  cur = cur
80
  return render,cur
81
 
82
-
83
-
84
-
85
  with gr.Blocks() as app:
86
- #player = gr.State()
87
  with gr.Row():
88
  with gr.Column():
89
  youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
@@ -91,10 +74,10 @@ with gr.Blocks() as app:
91
  output_win = gr.Video()
92
  with gr.Column():
93
  with gr.Row():
94
- cur_frame = gr.Number()
95
- fps_frames = gr.Number()
96
- total_frames = gr.Number(interactive=False)
97
- run_button = gr.Button()
98
  with gr.Row():
99
  bk = gr.Button("<")
100
  pl = gr.Button("Play")
@@ -102,17 +85,15 @@ with gr.Blocks() as app:
102
  fw = gr.Button(">")
103
  det_win = gr.Image(source="webcam", streaming=True)
104
  with gr.Row():
105
- pl_tog=gr.Number()
106
- ins_cnt=gr.Number()
107
- pl.click(tog_on,None,pl_tog)
108
- st.click(tog_off,None,pl_tog)
109
  pl_tog.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
110
- #cur_frame.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],every=0.1,show_progress=False)
111
-
112
  cur_frame.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
113
- bk.click(bk_fn,[cur_frame],cur_frame)
114
- fw.click(fw_fn,[cur_frame,total_frames],cur_frame)
115
  load_button.click(load,youtube_url,[output_win,cur_frame,total_frames,fps_frames])
116
- run_button.click(vid_play, [output_win,cur_frame], det_win)
117
 
118
  app.queue(concurrency_count=10).launch()
 
4
  import torch
5
  from pytube import YouTube
6
  from ultralyticsplus import YOLO, render_result
 
7
 
8
 
 
 
 
 
9
  model = YOLO('ultralyticsplus/yolov8s')
10
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
11
  URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" #URL to parse
 
17
  model.overrides['max_det'] = 1000 # maximum number of detections per image
18
  model.to(device)
19
 
 
 
 
 
20
 
21
  def load(URL):
22
  yt = YouTube(URL)
 
27
  frame_count = int(player.get(cv2.CAP_PROP_FRAME_COUNT))
28
  frame_fps = (player.get(cv2.CAP_PROP_FPS))
29
  #process.release()
 
30
  return vid_cap,frame_num,frame_count,frame_fps
31
 
32
  def vid_play(cap,frame_num):
 
33
  assert player.isOpened() # Make sure that their is a stream.
34
  player.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num))
 
35
  ret, frame_bgr = player.read(int(frame_num))
36
  frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
37
  results = model.predict(frame)
38
  render = render_result(model=model, image=frame, result=results[0])
39
  return render
40
+
41
  def fw_fn(cur,last):
42
  next = cur+1
43
  if next > last:
 
54
  return 0
55
 
56
  def pl_fn(cap,cur,last,fps,pl_tog):
 
 
57
  player.set(cv2.CAP_PROP_POS_FRAMES, cur)
58
  ret, frame_bgr = player.read(cur)
59
  frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
60
  results = model.predict(frame)
61
  render = render_result(model=model, image=frame, result=results[0])
62
  if pl_tog ==1:
 
63
  cur+=1
64
  else:
65
  cur = cur
66
  return render,cur
67
 
 
 
 
68
  with gr.Blocks() as app:
69
+ gr.Markdown("""<center><h1>Mediocre Video Object Detection</h1></center>""")
70
  with gr.Row():
71
  with gr.Column():
72
  youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
 
74
  output_win = gr.Video()
75
  with gr.Column():
76
  with gr.Row():
77
+ cur_frame = gr.Number(label="Current Frame")
78
+ fps_frames = gr.Number(label="Video FPS",interactive=False)
79
+ total_frames = gr.Number(label="Total Frames",interactive=False)
80
+ #run_button = gr.Button()
81
  with gr.Row():
82
  bk = gr.Button("<")
83
  pl = gr.Button("Play")
 
85
  fw = gr.Button(">")
86
  det_win = gr.Image(source="webcam", streaming=True)
87
  with gr.Row():
88
+ pl_tog=gr.Number(visible=False)
89
+ ins_cnt=gr.Number(visible=False)
90
+ pl.click(tog_on,None,pl_tog,show_progress=False)
91
+ st.click(tog_off,None,pl_tog,show_progress=False)
92
  pl_tog.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
 
 
93
  cur_frame.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
94
+ bk.click(bk_fn,[cur_frame],cur_frame,show_progress=False)
95
+ fw.click(fw_fn,[cur_frame,total_frames],cur_frame,show_progress=False)
96
  load_button.click(load,youtube_url,[output_win,cur_frame,total_frames,fps_frames])
97
+ #run_button.click(vid_play, [output_win,cur_frame], det_win)
98
 
99
  app.queue(concurrency_count=10).launch()