freealise commited on
Commit
8f1a95a
1 Parent(s): 6832791

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -93,7 +93,7 @@ def create_video(frames, fps, type):
93
  return type + "_result.mp4"
94
 
95
 
96
- def infer(url_in,interpolation,fps_output,resize_n,winsize):
97
 
98
  fps_output = logscale(fps_output)
99
  # 1. break video into frames and get FPS
@@ -137,19 +137,27 @@ def infer(url_in,interpolation,fps_output,resize_n,winsize):
137
  prvs = cv2.cvtColor(fr1, cv2.COLOR_RGBA2GRAY)
138
  hsv = np.zeros_like(fr1)
139
  hsv[..., 1] = 255
140
- res = np.zeros_like(prvs)
141
- flow = res
 
 
 
 
142
  while(depth_map.isOpened()):
143
  ret, fr2 = depth_map.read()
144
  if ret == False:
145
  break
146
  nxt = cv2.cvtColor(fr2, cv2.COLOR_RGBA2GRAY)
147
- fl = cv2.calcOpticalFlowFarneback(prvs, nxt, flow, 0.5, 3, winsize, 3, 5, 1.2, 0)
148
- mag, ang = cv2.cartToPolar(fl[..., 0], fl[..., 1])
149
- hsv[..., 0] = ang*180/np.pi/2
150
- hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
151
- rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
152
- rgb = cv2.cvtColor(rgb, cv2.COLOR_RGBA2GRAY)
 
 
 
 
153
  alpha = 1.0/len(frames_list)
154
  beta = (1.0 - alpha)
155
  res = cv2.addWeighted(rgb, alpha, res, beta, 0.0, res)
@@ -196,6 +204,7 @@ with gr.Blocks() as demo:
196
  with gr.Column():
197
  url_input = gr.Textbox(value="./examples/streetview.mp4", label="URL")
198
  resize_num = gr.Slider(minimum=1, maximum=4096, step=1, value=256, label="Resize to width: ")
 
199
  winsize_num = gr.Slider(minimum=1, maximum=256, step=1, value=15, label="Motion detection window size: ")
200
  with gr.Row():
201
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
@@ -213,13 +222,13 @@ with gr.Blocks() as demo:
213
  depth_output = gr.ImageEditor(image_mode="L", interactive=True, label="Depth map")
214
 
215
  gr.Examples(
216
- examples=[["./examples/streetview.mp4", 1, 0, 256, 15]],
217
  fn=infer,
218
- inputs=[url_input,interpolation_slider,fps_output_slider,resize_num,winsize_num],
219
  outputs=[video_output,file_output,depth_output],
220
  cache_examples=True
221
  )
222
 
223
- submit_btn.click(fn=infer, inputs=[url_input,interpolation_slider,fps_output_slider,resize_num,winsize_num], outputs=[video_output, file_output, depth_output])
224
 
225
  demo.launch()
 
93
  return type + "_result.mp4"
94
 
95
 
96
+ def infer(url_in,interpolation,fps_output,resize_n,winsize,o_flow):
97
 
98
  fps_output = logscale(fps_output)
99
  # 1. break video into frames and get FPS
 
137
  prvs = cv2.cvtColor(fr1, cv2.COLOR_RGBA2GRAY)
138
  hsv = np.zeros_like(fr1)
139
  hsv[..., 1] = 255
140
+ if o_flow == True:
141
+ res = np.zeros_like(prvs)
142
+ flow = res
143
+ else:
144
+ res = prvs
145
+
146
  while(depth_map.isOpened()):
147
  ret, fr2 = depth_map.read()
148
  if ret == False:
149
  break
150
  nxt = cv2.cvtColor(fr2, cv2.COLOR_RGBA2GRAY)
151
+ if o_flow == True:
152
+ fl = cv2.calcOpticalFlowFarneback(prvs, nxt, flow, 0.5, 3, winsize, 3, 5, 1.2, 0)
153
+ mag, ang = cv2.cartToPolar(fl[..., 0], fl[..., 1])
154
+ hsv[..., 0] = ang*180/np.pi/2
155
+ hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
156
+ rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
157
+ rgb = cv2.cvtColor(rgb, cv2.COLOR_RGBA2GRAY)
158
+ else:
159
+ rgb = nxt
160
+
161
  alpha = 1.0/len(frames_list)
162
  beta = (1.0 - alpha)
163
  res = cv2.addWeighted(rgb, alpha, res, beta, 0.0, res)
 
204
  with gr.Column():
205
  url_input = gr.Textbox(value="./examples/streetview.mp4", label="URL")
206
  resize_num = gr.Slider(minimum=1, maximum=4096, step=1, value=256, label="Resize to width: ")
207
+ of_check = gr.Checkbox(value=False, label="Detect motion for depth map: ")
208
  winsize_num = gr.Slider(minimum=1, maximum=256, step=1, value=15, label="Motion detection window size: ")
209
  with gr.Row():
210
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
 
222
  depth_output = gr.ImageEditor(image_mode="L", interactive=True, label="Depth map")
223
 
224
  gr.Examples(
225
+ examples=[["./examples/streetview.mp4", 1, 0, 256, 15, False]],
226
  fn=infer,
227
+ inputs=[url_input,interpolation_slider,fps_output_slider,resize_num,winsize_num,of_check],
228
  outputs=[video_output,file_output,depth_output],
229
  cache_examples=True
230
  )
231
 
232
+ submit_btn.click(fn=infer, inputs=[url_input,interpolation_slider,fps_output_slider,resize_num,winsize_num,of_check], outputs=[video_output, file_output, depth_output])
233
 
234
  demo.launch()