fffiloni commited on
Commit
faf9142
·
1 Parent(s): 23b843d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -55,6 +55,30 @@ def write_flo(flow, filename):
55
  flow.tofile(f)
56
  f.close()
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def infer():
60
  video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
@@ -149,7 +173,9 @@ def infer():
149
  # output_folder = "/tmp/" # Update this to the folder of your choice
150
  write_jpeg(flow_img, f"predicted_flow.jpg")
151
  flo_file = write_flo(predicted_flow, "flofile.flo")
152
- return "done", "predicted_flow.jpg", ["flofile.flo"]
 
 
153
  ####################################
154
  # Bonus: Creating GIFs of predicted flows
155
  # ---------------------------------------
@@ -177,28 +203,5 @@ def infer():
177
  #
178
  # ffmpeg -f image2 -framerate 30 -i predicted_flow_%d.jpg -loop -1 flow.gif
179
 
180
- def warp_flow(img, flow):
181
- h, w = flow.shape[:2]
182
- flow = flow.copy()
183
- flow[:, :, 0] += np.arange(w)
184
- flow[:, :, 1] += np.arange(h)[:, np.newaxis]
185
- res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
186
- return res
187
-
188
-
189
-
190
- def warp(frame1, frame2, flo_path, blend=0.5, weights_path=None):
191
- flow21 = np.load(flo_path)
192
- frame1pil = np.array(frame1.convert('RGB').resize((flow21.shape[1],flow21.shape[0])))
193
- frame1_warped21 = warp_flow(frame1pil, flow21)
194
- # frame2pil = frame1pil
195
- frame2pil = np.array(frame2.convert('RGB').resize((flow21.shape[1],flow21.shape[0])))
196
-
197
- if weights_path:
198
- forward_weights = load_cc(weights_path)
199
- blended_w = frame2pil*(1-blend) + blend*(frame1_warped21*forward_weights+frame2pil*(1-forward_weights))
200
- else: blended_w = frame2pil*(1-blend) + frame1_warped21*(blend)
201
-
202
- return PIL.Image.fromarray(blended_w.astype('uint8'))
203
 
204
- gr.Interface(fn=infer, inputs=[], outputs=[gr.Textbox(), gr.Image(), gr.Files()]).launch()
 
55
  flow.tofile(f)
56
  f.close()
57
 
58
+ def warp_flow(img, flow):
59
+ h, w = flow.shape[:2]
60
+ flow = flow.copy()
61
+ flow[:, :, 0] += np.arange(w)
62
+ flow[:, :, 1] += np.arange(h)[:, np.newaxis]
63
+ res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
64
+ return res
65
+
66
+
67
+
68
+ def warp(frame1, frame2, flo_path, blend=0.5, weights_path=None):
69
+ flow21 = np.load(flo_path)
70
+ frame1pil = np.array(frame1.convert('RGB').resize((flow21.shape[1],flow21.shape[0])))
71
+ frame1_warped21 = warp_flow(frame1pil, flow21)
72
+ # frame2pil = frame1pil
73
+ frame2pil = np.array(frame2.convert('RGB').resize((flow21.shape[1],flow21.shape[0])))
74
+
75
+ if weights_path:
76
+ forward_weights = load_cc(weights_path)
77
+ blended_w = frame2pil*(1-blend) + blend*(frame1_warped21*forward_weights+frame2pil*(1-forward_weights))
78
+ else: blended_w = frame2pil*(1-blend) + frame1_warped21*(blend)
79
+
80
+ return PIL.Image.fromarray(blended_w.astype('uint8'))
81
+
82
 
83
  def infer():
84
  video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
 
173
  # output_folder = "/tmp/" # Update this to the folder of your choice
174
  write_jpeg(flow_img, f"predicted_flow.jpg")
175
  flo_file = write_flo(predicted_flow, "flofile.flo")
176
+
177
+ warp_res = warp(frames[100], frames[101], flo_file, blend=0.5, None)
178
+ return "done", "predicted_flow.jpg", ["flofile.flo"], warp_res
179
  ####################################
180
  # Bonus: Creating GIFs of predicted flows
181
  # ---------------------------------------
 
203
  #
204
  # ffmpeg -f image2 -framerate 30 -i predicted_flow_%d.jpg -loop -1 flow.gif
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
+ gr.Interface(fn=infer, inputs=[], outputs=[gr.Textbox(), gr.Image(), gr.Files(), gr.Image(type="pil")]).launch()