Update app.py
Browse files
app.py
CHANGED
@@ -33,6 +33,10 @@ import tempfile
|
|
33 |
from pathlib import Path
|
34 |
from urllib.request import urlretrieve
|
35 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
36 |
def write_flo(flow, filename):
|
37 |
"""
|
38 |
Write optical flow in Middlebury .flo format
|
@@ -58,43 +62,57 @@ def write_flo(flow, filename):
|
|
58 |
flow.tofile(f)
|
59 |
f.close()
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
print(f"IMAGE AP: {im}")
|
72 |
-
print(f"FLOW AV: {flow}")
|
73 |
-
#flow = flow.cpu().data.numpy()
|
74 |
-
#flow = flow.astype(np.float32)
|
75 |
-
flow = torch.FloatTensor(flow)
|
76 |
-
|
77 |
-
print(f"FLOW AP: {flow}")
|
78 |
-
h, w = flow.shape[:2]
|
79 |
-
flow[:,:,0] += np.arange(w)
|
80 |
-
flow[:,:,1] += np.arange(h)[:,np.newaxis]
|
81 |
-
prevImg = cv2.remap(im, flow, None, cv2.INTER_LINEAR)
|
82 |
-
|
83 |
def infer():
|
84 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
85 |
video_path = Path(tempfile.mkdtemp()) / "basketball.mp4"
|
86 |
_ = urlretrieve(video_url, video_path)
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
frames, _, _ = read_video(str(video_path), output_format="TCHW")
|
92 |
|
93 |
img1_batch = torch.stack([frames[100]])
|
94 |
img2_batch = torch.stack([frames[101]])
|
95 |
|
96 |
-
|
97 |
-
|
98 |
weights = Raft_Large_Weights.DEFAULT
|
99 |
transforms = weights.transforms()
|
100 |
|
@@ -173,10 +191,10 @@ def infer():
|
|
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 |
-
res = warp_image(img1_batch, predicted_flow)
|
178 |
-
|
179 |
-
|
180 |
return "done", "predicted_flow.jpg", ["flofile.flo"]
|
181 |
####################################
|
182 |
# Bonus: Creating GIFs of predicted flows
|
|
|
33 |
from pathlib import Path
|
34 |
from urllib.request import urlretrieve
|
35 |
import tensorflow as tf
|
36 |
+
from scipy.interpolate import interp2d
|
37 |
+
from imageio import imread, imwrite
|
38 |
+
from flowio import readFlowFile
|
39 |
+
|
40 |
def write_flo(flow, filename):
|
41 |
"""
|
42 |
Write optical flow in Middlebury .flo format
|
|
|
62 |
flow.tofile(f)
|
63 |
f.close()
|
64 |
|
65 |
+
def warpImage(im, vx, vy, cast_uint8=True):
|
66 |
+
'''
|
67 |
+
function to warp images with different dimensions
|
68 |
+
'''
|
69 |
+
|
70 |
+
height2, width2, nChannels = im.shape
|
71 |
+
height1, width1 = vx.shape
|
72 |
+
|
73 |
+
x = np.linspace(1, width2, width2)
|
74 |
+
y = np.linspace(1, height2, height2)
|
75 |
+
X = np.linspace(1, width1, width1)
|
76 |
+
Y = np.linspace(1, height1, height1)
|
77 |
+
xx, yy = np.meshgrid(x, y)
|
78 |
+
XX, YY = np.meshgrid(X, Y)
|
79 |
+
XX = XX + vx
|
80 |
+
YY = YY + vy
|
81 |
+
mask = (XX < 1) | (XX > width2) | (YY < 1) | (YY > height2)
|
82 |
+
XX = np.clip(XX, 1, width2)
|
83 |
+
YY = np.clip(XX, 1, height2)
|
84 |
+
|
85 |
+
warpI2 = np.zeros((height1, width1, nChannels))
|
86 |
+
for i in range(nChannels):
|
87 |
+
f = interp2d(x, y, im[:, :, i], 'cubic')
|
88 |
+
foo = f(X, Y)
|
89 |
+
foo[mask] = 0.6
|
90 |
+
warpI2[:, :, i] = foo
|
91 |
+
|
92 |
+
mask = 1 - mask
|
93 |
+
|
94 |
+
if cast_uint8:
|
95 |
+
warpI2 = warpI2.astype(np.uint8)
|
96 |
+
|
97 |
+
return warpI2, mask
|
98 |
+
|
99 |
+
|
100 |
+
def get_warp_res(fname_image, fname_flow, fname_output='warped.png'):
|
101 |
+
im2 = imread(fname_image)
|
102 |
+
flow = readFlowFile(fname_flow)
|
103 |
+
im_warped, _ = warpImage(im2, flow[:, :, 0], flow[:, :, 1])
|
104 |
+
imwrite(fname_output, im_warped)
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
def infer():
|
107 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
108 |
video_path = Path(tempfile.mkdtemp()) / "basketball.mp4"
|
109 |
_ = urlretrieve(video_url, video_path)
|
110 |
|
|
|
|
|
|
|
111 |
frames, _, _ = read_video(str(video_path), output_format="TCHW")
|
112 |
|
113 |
img1_batch = torch.stack([frames[100]])
|
114 |
img2_batch = torch.stack([frames[101]])
|
115 |
|
|
|
|
|
116 |
weights = Raft_Large_Weights.DEFAULT
|
117 |
transforms = weights.transforms()
|
118 |
|
|
|
191 |
# output_folder = "/tmp/" # Update this to the folder of your choice
|
192 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
193 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
194 |
+
write_jpeg(img1_batch, f"input_image.jpg")
|
195 |
+
#res = warp_image(img1_batch, predicted_flow)
|
196 |
+
res = get_warp_res("input_image.jpg", "flofile.flo", fname_output='warped.png'):
|
197 |
+
print(res)
|
198 |
return "done", "predicted_flow.jpg", ["flofile.flo"]
|
199 |
####################################
|
200 |
# Bonus: Creating GIFs of predicted flows
|