Update app.py
Browse files
app.py
CHANGED
@@ -58,95 +58,43 @@ def write_flo(flow, filename):
|
|
58 |
flow.tofile(f)
|
59 |
f.close()
|
60 |
|
61 |
-
|
|
|
62 |
"""
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
- img: tensor of shape (B, H, W, C)
|
68 |
-
- x: flattened tensor of shape (B*H*W, )
|
69 |
-
- y: flattened tensor of shape (B*H*W, )
|
70 |
-
Returns
|
71 |
-
-------
|
72 |
-
- output: tensor of shape (B, H, W, C)
|
73 |
"""
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
max_y = tf.cast(H - 1, tf.int32)
|
104 |
-
max_x = tf.cast(W - 1, tf.int32)
|
105 |
-
zero = tf.zeros([], dtype=tf.int32)
|
106 |
-
|
107 |
-
x = flows[:,0,:,:]
|
108 |
-
y = flows[:,1,:,:]
|
109 |
-
x0 = x
|
110 |
-
y0 = y
|
111 |
-
x0 = tf.cast(x0, tf.int32)
|
112 |
-
x1 = x0 + 1
|
113 |
-
y0 = tf.cast(y0, tf.int32)
|
114 |
-
y1 = y0 + 1
|
115 |
-
|
116 |
-
# clip to range [0, H/W] to not violate img boundaries
|
117 |
-
x0 = tf.clip_by_value(x0, zero, max_x)
|
118 |
-
x1 = tf.clip_by_value(x1, zero, max_x)
|
119 |
-
y0 = tf.clip_by_value(y0, zero, max_y)
|
120 |
-
y1 = tf.clip_by_value(y1, zero, max_y)
|
121 |
-
|
122 |
-
# get pixel value at corner coords
|
123 |
-
Ia = get_pixel_value(img, x0, y0)
|
124 |
-
Ib = get_pixel_value(img, x0, y1)
|
125 |
-
Ic = get_pixel_value(img, x1, y0)
|
126 |
-
Id = get_pixel_value(img, x1, y1)
|
127 |
-
|
128 |
-
# recast as float for delta calculation
|
129 |
-
x0 = tf.cast(x0, tf.float32)
|
130 |
-
x1 = tf.cast(x1, tf.float32)
|
131 |
-
y0 = tf.cast(y0, tf.float32)
|
132 |
-
y1 = tf.cast(y1, tf.float32)
|
133 |
-
|
134 |
-
|
135 |
-
# calculate deltas
|
136 |
-
wa = (x1-x) * (y1-y)
|
137 |
-
wb = (x1-x) * (y-y0)
|
138 |
-
wc = (x-x0) * (y1-y)
|
139 |
-
wd = (x-x0) * (y-y0)
|
140 |
-
|
141 |
-
# add dimension for addition
|
142 |
-
wa = tf.expand_dims(wa, axis=3)
|
143 |
-
wb = tf.expand_dims(wb, axis=3)
|
144 |
-
wc = tf.expand_dims(wc, axis=3)
|
145 |
-
wd = tf.expand_dims(wd, axis=3)
|
146 |
-
|
147 |
-
# compute output
|
148 |
-
out = tf.add_n([wa*Ia, wb*Ib, wc*Ic, wd*Id])
|
149 |
-
return out
|
150 |
|
151 |
def infer():
|
152 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
@@ -242,18 +190,9 @@ def infer():
|
|
242 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
243 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
244 |
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
init = tf.global_variables_initializer()
|
249 |
-
sess.run(init)
|
250 |
-
|
251 |
-
output = tf_warp(a, predicted_flow, 520, 960)
|
252 |
-
out = sess.run(output, feed_dict = {a:img, flow_vec:flow})
|
253 |
-
out = np.clip(out,0,255).astype('uint8')
|
254 |
-
# print out.shape
|
255 |
-
im = Image.fromarray(out[0].astype('uint8'))
|
256 |
-
im.save('output.jpg')
|
257 |
return "done", "predicted_flow.jpg", ["flofile.flo"]
|
258 |
####################################
|
259 |
# Bonus: Creating GIFs of predicted flows
|
|
|
58 |
flow.tofile(f)
|
59 |
f.close()
|
60 |
|
61 |
+
#warp using scipy
|
62 |
+
def warp_image(im, flow):
|
63 |
"""
|
64 |
+
Use optical flow to warp image to the next
|
65 |
+
:param im: image to warp
|
66 |
+
:param flow: optical flow
|
67 |
+
:return: warped image
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
"""
|
69 |
+
from scipy import interpolate
|
70 |
+
image_height = im.shape[0]
|
71 |
+
image_width = im.shape[1]
|
72 |
+
flow_height = flow.shape[0]
|
73 |
+
flow_width = flow.shape[1]
|
74 |
+
n = image_height * image_width
|
75 |
+
(iy, ix) = np.mgrid[0:image_height, 0:image_width]
|
76 |
+
(fy, fx) = np.mgrid[0:flow_height, 0:flow_width]
|
77 |
+
fx = fx.astype(np.float64)
|
78 |
+
fy = fy.astype(np.float64)
|
79 |
+
fx += flow[:,:,0]
|
80 |
+
fy += flow[:,:,1]
|
81 |
+
mask = np.logical_or(fx <0 , fx > flow_width)
|
82 |
+
mask = np.logical_or(mask, fy < 0)
|
83 |
+
mask = np.logical_or(mask, fy > flow_height)
|
84 |
+
fx = np.minimum(np.maximum(fx, 0), flow_width)
|
85 |
+
fy = np.minimum(np.maximum(fy, 0), flow_height)
|
86 |
+
points = np.concatenate((ix.reshape(n,1), iy.reshape(n,1)), axis=1)
|
87 |
+
xi = np.concatenate((fx.reshape(n, 1), fy.reshape(n,1)), axis=1)
|
88 |
+
warp = np.zeros((image_height, image_width, im.shape[2]))
|
89 |
+
for i in range(im.shape[2]):
|
90 |
+
channel = im[:, :, i]
|
91 |
+
values = channel.reshape(n, 1)
|
92 |
+
new_channel = interpolate.griddata(points, values, xi, method='cubic')
|
93 |
+
new_channel = np.reshape(new_channel, [flow_height, flow_width])
|
94 |
+
new_channel[mask] = 1
|
95 |
+
warp[:, :, i] = new_channel.astype(np.uint8)
|
96 |
+
|
97 |
+
return warp.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
def infer():
|
100 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
|
|
190 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
191 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
192 |
|
193 |
+
res = warp_image(img1_batch, predicted_flow)
|
194 |
+
im = Image.fromarray(res)
|
195 |
+
im.save('output.jpg')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
return "done", "predicted_flow.jpg", ["flofile.flo"]
|
197 |
####################################
|
198 |
# Bonus: Creating GIFs of predicted flows
|