Spaces:
Sleeping
Sleeping
Commit
•
633b96b
1
Parent(s):
074d43b
Update app.py
Browse files
app.py
CHANGED
@@ -3,171 +3,84 @@ import time
|
|
3 |
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 |
-
|
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
|
17 |
-
|
18 |
-
# set model parameters
|
19 |
-
model.overrides['conf'] = 0.25 # NMS confidence threshold
|
20 |
-
model.overrides['iou'] = 0.45 # NMS IoU threshold
|
21 |
-
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 |
-
|
26 |
-
#play = pafy.new(_URL).streams[-1] #'-1' means read the lowest quality of video.
|
27 |
-
|
28 |
-
#assert play is not None # we want to make sure their is a input to read.
|
29 |
-
#stream = cv2.VideoCapture(play.url) #create a opencv video stream.
|
30 |
-
#stream = cv2.VideoCapture(0) # 0 means read from local camera.
|
31 |
-
#camera_ip = "rtsp://username:password@IP/port"
|
32 |
-
#stream = cv2.VideoCapture(camera_ip)
|
33 |
-
#class Capvid:
|
34 |
-
|
35 |
-
|
36 |
-
# load model
|
37 |
-
|
38 |
-
# set image
|
39 |
-
#image = 'https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg'
|
40 |
-
|
41 |
-
# perform inference
|
42 |
-
#def show(feed)
|
43 |
-
# return model.predict(feed)
|
44 |
-
|
45 |
-
# observe results
|
46 |
-
#print(results[0].boxes)
|
47 |
-
#render = render_result(model=model, image=image, result=results[0])
|
48 |
-
#render.show()
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
"""
|
53 |
-
def score_frame(frame):
|
54 |
-
#frame = [torch.tensor(frame)]
|
55 |
-
result = model(frame)
|
56 |
-
results = [torch.tensor(result)]
|
57 |
-
labels = results[0][:, -1].numpy()
|
58 |
-
cord = results[0][:, :-1].numpy()
|
59 |
-
return labels, cord
|
60 |
-
|
61 |
-
"""
|
62 |
-
The function below takes the results and the frame as input and plots boxes over all the objects which have a score higer than our threshold.
|
63 |
-
"""
|
64 |
-
def plot_boxes(results, frame):
|
65 |
-
labels, cord = results
|
66 |
-
n = len(labels)
|
67 |
-
x_shape, y_shape = frame.shape[1], frame.shape[0]
|
68 |
-
for i in range(n):
|
69 |
-
row = cord[i]
|
70 |
-
# If score is less than 0.2 we avoid making a prediction.
|
71 |
-
if row[4] < 0.2:
|
72 |
-
continue
|
73 |
-
x1 = int(row[0]*x_shape)
|
74 |
-
y1 = int(row[1]*y_shape)
|
75 |
-
x2 = int(row[2]*x_shape)
|
76 |
-
y2 = int(row[3]*y_shape)
|
77 |
-
bgr = (0, 255, 0) # color of the box
|
78 |
-
classes = model.names # Get the name of label index
|
79 |
-
label_font = cv2.FONT_HERSHEY_SIMPLEX #Font for the label.
|
80 |
-
cv2.rectangle(frame, \
|
81 |
-
(x1, y1), (x2, y2), \
|
82 |
-
bgr, 2) #Plot the boxes
|
83 |
-
cv2.putText(frame,\
|
84 |
-
classes[labels[i]], \
|
85 |
-
(x1, y1), \
|
86 |
-
label_font, 0.9, bgr, 2) #Put a label over box.
|
87 |
-
return frame
|
88 |
|
89 |
-
|
90 |
-
The Function below oracestrates the entire operation and performs the real-time parsing for video stream.
|
91 |
-
"""
|
92 |
-
def vid_play(vid_cap):
|
93 |
-
stream = cv2.VideoCapture(vid_cap)
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
def load(URL):
|
120 |
-
yt = YouTube(URL)
|
121 |
-
vid_cap = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().last().download(filename="tmp.mp4")
|
122 |
-
process = cv2.VideoCapture(vid_cap)
|
123 |
-
frame_num = int(process.get(cv2.CAP_PROP_POS_FRAMES))
|
124 |
-
frame_count = int(process.get(cv2.CAP_PROP_FRAME_COUNT))
|
125 |
-
process.release()
|
126 |
-
|
127 |
-
return vid_cap,frame_num,frame_count
|
128 |
-
|
129 |
-
def vid_play2(cap,frame_num):
|
130 |
-
player = cv2.VideoCapture(cap)
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
ret, frame = player.read(int(frame_num))
|
144 |
-
results = model.predict(frame)
|
145 |
-
|
146 |
-
render = render_result(model=model, image=frame, result=results[0])
|
147 |
-
#out = render.show()
|
148 |
-
#start_time = time.time() # We would like to measure the FPS.
|
149 |
-
#results = score_frame(frame) # Score the Frame
|
150 |
-
#frame = plot_boxes(results, frame) # Plot the boxes.
|
151 |
-
#end_time = time.time()
|
152 |
-
#fps = 1/np.round(end_time - start_time, 3) #Measure the FPS.
|
153 |
-
#print(f"Frames Per Second : {fps}")
|
154 |
-
#out.write(frame) # Write the frame onto the output.
|
155 |
-
#ret, frame = player.read() # Read next frame.
|
156 |
-
return render
|
157 |
-
|
158 |
-
|
159 |
|
|
|
|
|
|
|
160 |
|
161 |
-
youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
|
162 |
-
load_button = gr.Button("Load Video")
|
163 |
with gr.Row():
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
with gr.Row():
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import cv2 # opencv2 package for python.
|
4 |
import torch
|
5 |
from pytube import YouTube
|
|
|
|
|
6 |
|
7 |
|
8 |
+
segmentor = pipeline("image-segmentation", model="facebook/detr-resnet-50-panoptic")
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
+
URL = "https://www.youtube.com/watch?v=6NBwbKMyzEE" #URL to parse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
def load(URL):
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
yt = YouTube(URL)
|
17 |
+
vid_cap = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().last().download(filename="tmp.mp4")
|
18 |
+
global player
|
19 |
+
player = cv2.VideoCapture(vid_cap)
|
20 |
+
frame_num = int(player.get(cv2.CAP_PROP_POS_FRAMES))
|
21 |
+
frame_count = int(player.get(cv2.CAP_PROP_FRAME_COUNT))
|
22 |
+
frame_fps = (player.get(cv2.CAP_PROP_FPS))
|
23 |
+
tog = 0
|
24 |
+
return vid_cap,frame_num,frame_count,frame_fps,tog
|
25 |
+
|
26 |
+
def fw_fn(cur,last):
|
27 |
+
next = cur+1
|
28 |
+
if next > last:
|
29 |
+
next = last
|
30 |
+
return next
|
31 |
+
def bk_fn(cur):
|
32 |
+
next = cur-1
|
33 |
+
if next < 0:
|
34 |
+
next = 0
|
35 |
+
return next
|
36 |
+
def tog_on():
|
37 |
+
return 1,gr.Markdown.update("""<center><h7>Status: Playing 😁</h7></center>""")
|
38 |
+
def tog_off():
|
39 |
+
return 0,gr.Markdown.update("""<center><h7>Status: Stopped 💀</h7></center>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
def pl_fn(cap,cur,last,fps,pl_tog):
|
42 |
+
player.set(cv2.CAP_PROP_POS_FRAMES, cur)
|
43 |
+
ret, frame_bgr = player.read(cur)
|
44 |
+
frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
|
45 |
+
output = segmentor(frame)
|
46 |
+
|
47 |
+
if pl_tog ==1:
|
48 |
+
cur+=1
|
49 |
+
else:
|
50 |
+
cur = cur
|
51 |
+
return output,cur
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
with gr.Blocks() as app:
|
54 |
+
gr.Markdown("""<center><h1>Testing</h1><h4>stuff</h4><h4></h4></center>""")
|
55 |
+
play_state = gr.Markdown("""<right><h7></h7></right>""")
|
56 |
|
|
|
|
|
57 |
with gr.Row():
|
58 |
+
with gr.Column():
|
59 |
+
youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
|
60 |
+
load_button = gr.Button("Load Video")
|
61 |
+
output_win = gr.Video()
|
62 |
+
with gr.Column():
|
63 |
+
with gr.Row():
|
64 |
+
cur_frame = gr.Number(label="Current Frame")
|
65 |
+
fps_frames = gr.Number(label="Video FPS",interactive=False)
|
66 |
+
total_frames = gr.Number(label="Total Frames",interactive=False)
|
67 |
+
#run_button = gr.Button()
|
68 |
+
with gr.Row():
|
69 |
+
bk = gr.Button("<")
|
70 |
+
pl = gr.Button("Play")
|
71 |
+
st = gr.Button("Stop")
|
72 |
+
fw = gr.Button(">")
|
73 |
+
det_win = gr.Image(source="webcam", streaming=True)
|
74 |
with gr.Row():
|
75 |
+
pl_tog=gr.Number(visible=False)
|
76 |
+
ins_cnt=gr.Number(visible=False)
|
77 |
+
pl.click(tog_on,None,[pl_tog,play_state],show_progress=False)
|
78 |
+
st.click(tog_off,None,[pl_tog,play_state],show_progress=False)
|
79 |
+
pl_tog.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
|
80 |
+
cur_frame.change(pl_fn,[output_win,cur_frame,total_frames,fps_frames,pl_tog],[det_win,cur_frame],show_progress=False)
|
81 |
+
bk.click(bk_fn,[cur_frame],cur_frame,show_progress=False)
|
82 |
+
fw.click(fw_fn,[cur_frame,total_frames],cur_frame,show_progress=False)
|
83 |
+
load_button.click(load,youtube_url,[output_win,cur_frame,total_frames,fps_frames,pl_tog])
|
84 |
+
#run_button.click(vid_play, [output_win,cur_frame], det_win)
|
85 |
+
|
86 |
+
app.queue(concurrency_count=10).launch()
|