Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,8 @@ predicted flows to RGB images for visualization.
|
|
| 19 |
|
| 20 |
import cv2
|
| 21 |
import numpy as np
|
|
|
|
|
|
|
| 22 |
import torch
|
| 23 |
import matplotlib.pyplot as plt
|
| 24 |
import torchvision.transforms.functional as F
|
|
@@ -56,52 +58,20 @@ def write_flo(flow, filename):
|
|
| 56 |
flow.tofile(f)
|
| 57 |
f.close()
|
| 58 |
|
| 59 |
-
def
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
"""
|
| 74 |
-
f = open(filename, 'rb')
|
| 75 |
-
magic = np.fromfile(f, np.float32, count=1)
|
| 76 |
-
data2d = None
|
| 77 |
-
|
| 78 |
-
if 202021.25 != magic:
|
| 79 |
-
print('Magic number incorrect. Invalid .flo file')
|
| 80 |
-
else:
|
| 81 |
-
w = np.fromfile(f, np.int32, count=1)
|
| 82 |
-
h = np.fromfile(f, np.int32, count=1)
|
| 83 |
-
print("Reading %d x %d flow file in .flo format" % (h, w))
|
| 84 |
-
data2d = np.fromfile(f, np.float32, count=2 * w * h)
|
| 85 |
-
# reshape data into 3D array (columns, rows, channels)
|
| 86 |
-
data2d = np.resize(data2d, (h[0], w[0], 2))
|
| 87 |
-
f.close()
|
| 88 |
-
return data2d
|
| 89 |
-
|
| 90 |
-
def warp(frame1, frame2, flo_path, blend=0.5, weights_path=None):
|
| 91 |
-
flo_path = flo_path.cpu().data.numpy()
|
| 92 |
-
flow21 = np.array(flo_path)
|
| 93 |
-
frame1pil = np.array(frame1)
|
| 94 |
-
frame1_warped21 = warp_flow(frame1pil, flow21)
|
| 95 |
-
# frame2pil = frame1pil
|
| 96 |
-
frame2pil = np.array(frame2)
|
| 97 |
-
|
| 98 |
-
if weights_path:
|
| 99 |
-
forward_weights = load_cc(weights_path)
|
| 100 |
-
blended_w = frame2pil*(1-blend) + blend*(frame1_warped21*forward_weights+frame2pil*(1-forward_weights))
|
| 101 |
-
else: blended_w = frame2pil*(1-blend) + frame1_warped21*(blend)
|
| 102 |
-
|
| 103 |
-
return PIL.Image.fromarray(blended_w.astype('uint8'))
|
| 104 |
-
|
| 105 |
|
| 106 |
def infer():
|
| 107 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
|
@@ -197,8 +167,9 @@ def infer():
|
|
| 197 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
| 198 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
|
|
|
| 202 |
####################################
|
| 203 |
# Bonus: Creating GIFs of predicted flows
|
| 204 |
# ---------------------------------------
|
|
@@ -227,4 +198,4 @@ def infer():
|
|
| 227 |
# ffmpeg -f image2 -framerate 30 -i predicted_flow_%d.jpg -loop -1 flow.gif
|
| 228 |
|
| 229 |
|
| 230 |
-
gr.Interface(fn=infer, inputs=[], outputs=[gr.Textbox(), gr.Image(), gr.Files()
|
|
|
|
| 19 |
|
| 20 |
import cv2
|
| 21 |
import numpy as np
|
| 22 |
+
import os
|
| 23 |
+
import sys
|
| 24 |
import torch
|
| 25 |
import matplotlib.pyplot as plt
|
| 26 |
import torchvision.transforms.functional as F
|
|
|
|
| 58 |
flow.tofile(f)
|
| 59 |
f.close()
|
| 60 |
|
| 61 |
+
def get_flow(filename):
|
| 62 |
+
with open(filename, 'rb') as f:
|
| 63 |
+
magic = np.fromfile(f, np.float32, count=1)
|
| 64 |
+
if 202021.25 != magic:
|
| 65 |
+
print('Magic number incorrect. Invalid .flo file')
|
| 66 |
+
else:
|
| 67 |
+
w = np.fromfile(f, np.int32, count=1)
|
| 68 |
+
h = np.fromfile(f, np.int32, count=1)
|
| 69 |
+
print('Reading %d x %d flo file' % (w, h))
|
| 70 |
+
data = np.fromfile(f, np.float32, count=2*w*h)
|
| 71 |
+
# Reshape data into 3D array (columns, rows, bands)
|
| 72 |
+
data2D = np.resize(data, (1, h[0], w[0],2))
|
| 73 |
+
data2D = np.transpose(data2D,[0, 3,1,2])
|
| 74 |
+
return data2D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def infer():
|
| 77 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
|
|
|
| 167 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
| 168 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
| 169 |
|
| 170 |
+
test = get_flow("flofile.flo")
|
| 171 |
+
print(test)
|
| 172 |
+
return "done", "predicted_flow.jpg", ["flofile.flo"]
|
| 173 |
####################################
|
| 174 |
# Bonus: Creating GIFs of predicted flows
|
| 175 |
# ---------------------------------------
|
|
|
|
| 198 |
# ffmpeg -f image2 -framerate 30 -i predicted_flow_%d.jpg -loop -1 flow.gif
|
| 199 |
|
| 200 |
|
| 201 |
+
gr.Interface(fn=infer, inputs=[], outputs=[gr.Textbox(), gr.Image(), gr.Files()]).launch()
|