Future-Tense commited on
Commit
e1f13a6
·
1 Parent(s): 615ad15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -118
app.py CHANGED
@@ -23,97 +23,6 @@ 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
- The function below identifies the device which is availabe to make the prediction and uses it to load and infer the frame. Once it has results it will extract the labels and cordinates(Along with scores) for each object detected in the frame.
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
- player = stream #Get your video stream.
96
- assert player.isOpened() # Make sure that their is a stream.
97
- #Below code creates a new video writer object to write our
98
- #output stream.
99
- out_vid = ("vid_tmp.avi")
100
- x_shape = int(player.get(cv2.CAP_PROP_FRAME_WIDTH))
101
- y_shape = int(player.get(cv2.CAP_PROP_FRAME_HEIGHT))
102
- four_cc = cv2.VideoWriter_fourcc(*"MJPG") #Using MJPEG codex
103
- out = cv2.VideoWriter(out_vid, four_cc, 20, \
104
- (x_shape, y_shape))
105
- ret, frame = player.read() # Read the first frame.
106
- while True: # Run until stream is out of frames
107
- start_time = time.time() # We would like to measure the FPS.
108
- results = score_frame(frame) # Score the Frame
109
- frame = plot_boxes(results, frame) # Plot the boxes.
110
- end_time = time.time()
111
- fps = 1/np.round(end_time - start_time, 3) #Measure the FPS.
112
- print(f"Frames Per Second : {fps}")
113
- out.write(frame) # Write the frame onto the output.
114
- ret, frame = player.read() # Read next frame.
115
- return out
116
-
117
  with gr.Blocks() as app:
118
  stream = gr.State()
119
  def load(URL):
@@ -128,44 +37,27 @@ with gr.Blocks() as app:
128
 
129
  def vid_play2(cap,frame_num):
130
  player = cv2.VideoCapture(cap)
131
-
132
- #player = stream #Get your video stream.
133
  assert player.isOpened() # Make sure that their is a stream.
134
- #Below code creates a new video writer object to write our
135
- #output stream.
136
- #out_vid = ("vid_tmp.avi")
137
- #x_shape = int(player.get(cv2.CAP_PROP_FRAME_WIDTH))
138
- #y_shape = int(player.get(cv2.CAP_PROP_FRAME_HEIGHT))
139
- #four_cc = cv2.VideoWriter_fourcc(*"MJPG") #Using MJPEG codex
140
- #out = cv2.VideoWriter(out_vid, four_cc, 20,(x_shape, y_shape))
141
- #stream.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num))
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
- total_frames = gr.Number(interactive=False)
165
- run_button = gr.Button()
166
- cur_frame = gr.Number()
 
 
 
 
 
 
167
  with gr.Row():
168
- output_win = gr.Video()
169
  det_win = gr.Image(source="webcam", streaming=True)
170
  load_button.click(load,youtube_url,[output_win,cur_frame,total_frames])
171
  run_button.click(vid_play2, [output_win,cur_frame], det_win)
 
23
  model.to(device)
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  with gr.Blocks() as app:
27
  stream = gr.State()
28
  def load(URL):
 
37
 
38
  def vid_play2(cap,frame_num):
39
  player = cv2.VideoCapture(cap)
 
 
40
  assert player.isOpened() # Make sure that their is a stream.
41
+ player.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num))
 
 
 
 
 
 
 
42
 
43
  ret, frame = player.read(int(frame_num))
44
  results = model.predict(frame)
 
45
  render = render_result(model=model, image=frame, result=results[0])
 
 
 
 
 
 
 
 
 
46
  return render
47
 
48
 
49
 
 
 
 
50
  with gr.Row():
51
+ with gr.Column():
52
+ youtube_url = gr.Textbox(label="YouTube URL",value=f"{URL}")
53
+ load_button = gr.Button("Load Video")
54
+ output_win = gr.Video()
55
+ with gr.Column():
56
+ with gr.Row():
57
+ cur_frame = gr.Number()
58
+ total_frames = gr.Number(interactive=False)
59
+ run_button = gr.Button()
60
  with gr.Row():
 
61
  det_win = gr.Image(source="webcam", streaming=True)
62
  load_button.click(load,youtube_url,[output_win,cur_frame,total_frames])
63
  run_button.click(vid_play2, [output_win,cur_frame], det_win)