Update app.py
Browse files
app.py
CHANGED
@@ -65,32 +65,6 @@ def write_flo(flow, filename):
|
|
65 |
f.close()
|
66 |
|
67 |
|
68 |
-
def warp_flow(img, flow, mul=1.):
|
69 |
-
#img = np.array(img.convert('RGB'))
|
70 |
-
img = cv2.imread(img)
|
71 |
-
flow = cv2.imread(flow)
|
72 |
-
#flow = np.load(flow)
|
73 |
-
h, w = flow.shape[:2]
|
74 |
-
flow = flow.copy()
|
75 |
-
flow[:, :, 0] + np.arange(w)
|
76 |
-
flow[:, :, 1] + np.arange(h)[:, np.newaxis]
|
77 |
-
# print('flow stats', flow.max(), flow.min(), flow.mean())
|
78 |
-
# print(flow)
|
79 |
-
flow*mul
|
80 |
-
# print('flow stats mul', flow.max(), flow.min(), flow.mean())
|
81 |
-
# res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
|
82 |
-
res = cv2.remap(img, flow, None, cv2.INTER_LANCZOS4)
|
83 |
-
|
84 |
-
print(res)
|
85 |
-
|
86 |
-
def get_warp_res(fname_image, fname_flow, fname_output='warped.png'):
|
87 |
-
print(f"FNAME IMAGE: {fname_image}")
|
88 |
-
#im2 = imread(fname_image)
|
89 |
-
#print(f"FNAME IMAGE READED: {im2.shape}")
|
90 |
-
#flow = fname_flow.cpu().detach().numpy()
|
91 |
-
flow = fname_flow
|
92 |
-
#print(f"FNAME FLOW READED: {flow.shape}")
|
93 |
-
res = warp_flow(fname_image, flow, 1.)
|
94 |
|
95 |
def infer():
|
96 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
@@ -183,17 +157,10 @@ def infer():
|
|
183 |
print(f"predicted flow shape = {predicted_flow.shape}")
|
184 |
|
185 |
flow_img = flow_to_image(predicted_flow).to("cpu")
|
186 |
-
# output_folder = "/tmp/" # Update this to the folder of your choice
|
187 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
188 |
|
189 |
-
#input_image = flow_to_image(frames[100]).to("cpu")
|
190 |
-
#write_jpeg(input_image, f"frame_input.jpg")
|
191 |
-
|
192 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
193 |
-
|
194 |
-
#res = warp_image(img1_batch, predicted_flow)
|
195 |
-
|
196 |
-
|
197 |
# define a transform to convert a tensor to PIL image
|
198 |
transform = T.ToPILImage()
|
199 |
|
@@ -202,15 +169,12 @@ def infer():
|
|
202 |
img = img.resize((960, 520))
|
203 |
# display the PIL image
|
204 |
#img.show()
|
205 |
-
|
206 |
-
print(f"frame1pil: {
|
207 |
-
print(f"frame1pil shape: {
|
208 |
-
print(f"frame1pil dtype: {
|
209 |
-
img.save('
|
210 |
-
|
211 |
-
print(f"img1_batch shape: {img1_batch.shape}")
|
212 |
-
print(f"img1_batch dtype: {img1_batch.dtype}")
|
213 |
-
#res = get_warp_res(img1_batch, "predicted_flow.jpg", 'warped.png')
|
214 |
|
215 |
numpy_array_flow = predicted_flow.permute(1, 2, 0).detach().cpu().numpy()
|
216 |
print(f"numpy_array_flow: {numpy_array_flow}")
|
@@ -226,17 +190,17 @@ def infer():
|
|
226 |
numpy_array_flow*1.
|
227 |
# print('flow stats mul', flow.max(), flow.min(), flow.mean())
|
228 |
# res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
|
229 |
-
res = cv2.remap(
|
230 |
print(res)
|
231 |
|
232 |
res = Image.fromarray(res)
|
233 |
res.save('wraped.jpg')
|
234 |
|
235 |
blend2 = Image.open('frame_input.jpg')
|
236 |
-
|
237 |
-
|
238 |
|
239 |
-
return "done", "predicted_flow.jpg", ["flofile.flo"], '
|
240 |
####################################
|
241 |
# Bonus: Creating GIFs of predicted flows
|
242 |
# ---------------------------------------
|
|
|
65 |
f.close()
|
66 |
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def infer():
|
70 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
|
|
157 |
print(f"predicted flow shape = {predicted_flow.shape}")
|
158 |
|
159 |
flow_img = flow_to_image(predicted_flow).to("cpu")
|
|
|
160 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
161 |
|
|
|
|
|
|
|
162 |
flo_file = write_flo(predicted_flow, "flofile.flo")
|
163 |
+
|
|
|
|
|
|
|
164 |
# define a transform to convert a tensor to PIL image
|
165 |
transform = T.ToPILImage()
|
166 |
|
|
|
169 |
img = img.resize((960, 520))
|
170 |
# display the PIL image
|
171 |
#img.show()
|
172 |
+
frame2pil = np.array(img.convert('RGB'))
|
173 |
+
print(f"frame1pil: {frame2pil}")
|
174 |
+
print(f"frame1pil shape: {frame2pil.shape}")
|
175 |
+
print(f"frame1pil dtype: {frame2pil.dtype}")
|
176 |
+
img.save('raw_frame2.jpg')
|
177 |
+
|
|
|
|
|
|
|
178 |
|
179 |
numpy_array_flow = predicted_flow.permute(1, 2, 0).detach().cpu().numpy()
|
180 |
print(f"numpy_array_flow: {numpy_array_flow}")
|
|
|
190 |
numpy_array_flow*1.
|
191 |
# print('flow stats mul', flow.max(), flow.min(), flow.mean())
|
192 |
# res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
|
193 |
+
res = cv2.remap(frame2pil, numpy_array_flow, None, cv2.INTER_LANCZOS4)
|
194 |
print(res)
|
195 |
|
196 |
res = Image.fromarray(res)
|
197 |
res.save('wraped.jpg')
|
198 |
|
199 |
blend2 = Image.open('frame_input.jpg')
|
200 |
+
blend2 = Image.blend(res,blend2,0.5)
|
201 |
+
blend2.save("blended2.jpg")
|
202 |
|
203 |
+
return "done", "predicted_flow.jpg", ["flofile.flo"], 'raw_frame2.jpg', 'wraped.jpg', "blended2.jpg"
|
204 |
####################################
|
205 |
# Bonus: Creating GIFs of predicted flows
|
206 |
# ---------------------------------------
|