Future-Tense commited on
Commit
377c851
1 Parent(s): 4154b32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -9
app.py CHANGED
@@ -4,6 +4,11 @@ import cv2 # opencv2 package for python.
4
  import torch
5
  from pytube import YouTube
6
  from ultralyticsplus import YOLO, render_result
 
 
 
 
 
7
 
8
  #from torch import hub # Hub contains other models like FasterRCNN
9
  model = YOLO('ultralyticsplus/yolov8s')
@@ -109,15 +114,40 @@ def vid_play(vid_cap):
109
  ret, frame = player.read() # Read next frame.
110
  return out
111
 
112
-
113
-
114
- def load(URL):
115
- yt = YouTube(URL)
116
- vid_cap = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().last().download(filename="tmp.mp4")
117
-
118
- return vid_cap
119
-
120
  with gr.Blocks() as app:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
122
  load_button = gr.Button("Load Video")
123
  run_button = gr.Button()
@@ -125,6 +155,6 @@ with gr.Blocks() as app:
125
  output_win = gr.Video()
126
  det_win = gr.Video()
127
  load_button.click(load,youtube_url,output_win)
128
- run_button.click(vid_play, [output_win], det_win)
129
 
130
  app.launch(enable_queue=False)
 
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')
 
114
  ret, frame = player.read() # Read next frame.
115
  return out
116
 
 
 
 
 
 
 
 
 
117
  with gr.Blocks() as app:
118
+ def vid_play2(vid_cap):
119
+ stream = cv2.VideoCapture(vid_cap)
120
+
121
+ player = stream #Get your video stream.
122
+ assert player.isOpened() # Make sure that their is a stream.
123
+ #Below code creates a new video writer object to write our
124
+ #output stream.
125
+ out_vid = ("vid_tmp.avi")
126
+ x_shape = int(player.get(cv2.CAP_PROP_FRAME_WIDTH))
127
+ y_shape = int(player.get(cv2.CAP_PROP_FRAME_HEIGHT))
128
+ four_cc = cv2.VideoWriter_fourcc(*"MJPG") #Using MJPEG codex
129
+ out = cv2.VideoWriter(out_vid, four_cc, 20, \
130
+ (x_shape, y_shape))
131
+ ret, frame = player.read() # Read the first frame.
132
+
133
+ while True: # Run until stream is out of frames
134
+ start_time = time.time() # We would like to measure the FPS.
135
+ results = score_frame(frame) # Score the Frame
136
+ frame = plot_boxes(results, frame) # Plot the boxes.
137
+ end_time = time.time()
138
+ fps = 1/np.round(end_time - start_time, 3) #Measure the FPS.
139
+ print(f"Frames Per Second : {fps}")
140
+ out.write(frame) # Write the frame onto the output.
141
+ ret, frame = player.read() # Read next frame.
142
+ return gr.Video(out)
143
+
144
+
145
+ def load(URL):
146
+ yt = YouTube(URL)
147
+ vid_cap = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().last().download(filename="tmp.mp4")
148
+
149
+ return vid_cap
150
+
151
  youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
152
  load_button = gr.Button("Load Video")
153
  run_button = gr.Button()
 
155
  output_win = gr.Video()
156
  det_win = gr.Video()
157
  load_button.click(load,youtube_url,output_win)
158
+ run_button.click(vid_play2, [output_win], det_win)
159
 
160
  app.launch(enable_queue=False)