Update app.py
Browse files
app.py
CHANGED
@@ -30,6 +30,25 @@ import tempfile
|
|
30 |
from pathlib import Path
|
31 |
from urllib.request import urlretrieve
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def infer():
|
34 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
35 |
video_path = Path(tempfile.mkdtemp()) / "basketball.mp4"
|
@@ -122,8 +141,8 @@ def infer():
|
|
122 |
flow_img = flow_to_image(predicted_flow).to("cpu")
|
123 |
# output_folder = "/tmp/" # Update this to the folder of your choice
|
124 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
125 |
-
|
126 |
-
return "done", "predicted_flow.jpg"
|
127 |
####################################
|
128 |
# Bonus: Creating GIFs of predicted flows
|
129 |
# ---------------------------------------
|
@@ -151,22 +170,5 @@ def infer():
|
|
151 |
#
|
152 |
# ffmpeg -f image2 -framerate 30 -i predicted_flow_%d.jpg -loop -1 flow.gif
|
153 |
|
154 |
-
def write_flo(filename, flow):
|
155 |
-
"""
|
156 |
-
write optical flow in Middlebury .flo format
|
157 |
-
:param flow: optical flow map
|
158 |
-
:param filename: optical flow file path to be saved
|
159 |
-
:return: None
|
160 |
-
"""
|
161 |
-
f = open(filename, 'wb')
|
162 |
-
magic = np.array([202021.25], dtype=np.float32)
|
163 |
-
(height, width) = flow.shape[0:2]
|
164 |
-
w = np.array([width], dtype=np.int32)
|
165 |
-
h = np.array([height], dtype=np.int32)
|
166 |
-
magic.tofile(f)
|
167 |
-
w.tofile(f)
|
168 |
-
h.tofile(f)
|
169 |
-
flow.tofile(f)
|
170 |
-
f.close()
|
171 |
|
172 |
-
gr.Interface(fn=infer, inputs=[], outputs=[gr.Textbox(), gr.Image()]).launch()
|
|
|
30 |
from pathlib import Path
|
31 |
from urllib.request import urlretrieve
|
32 |
|
33 |
+
def write_flo(filename, flow):
|
34 |
+
"""
|
35 |
+
write optical flow in Middlebury .flo format
|
36 |
+
:param flow: optical flow map
|
37 |
+
:param filename: optical flow file path to be saved
|
38 |
+
:return: None
|
39 |
+
"""
|
40 |
+
f = open(filename, 'wb')
|
41 |
+
magic = np.array([202021.25], dtype=np.float32)
|
42 |
+
(height, width) = flow.shape[0:2]
|
43 |
+
w = np.array([width], dtype=np.int32)
|
44 |
+
h = np.array([height], dtype=np.int32)
|
45 |
+
magic.tofile(f)
|
46 |
+
w.tofile(f)
|
47 |
+
h.tofile(f)
|
48 |
+
flow.tofile(f)
|
49 |
+
f.close()
|
50 |
+
|
51 |
+
|
52 |
def infer():
|
53 |
video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
|
54 |
video_path = Path(tempfile.mkdtemp()) / "basketball.mp4"
|
|
|
141 |
flow_img = flow_to_image(predicted_flow).to("cpu")
|
142 |
# output_folder = "/tmp/" # Update this to the folder of your choice
|
143 |
write_jpeg(flow_img, f"predicted_flow.jpg")
|
144 |
+
flo_file = write_flo("flofile.flo", predicted_flow)
|
145 |
+
return "done", "predicted_flow.jpg", flo_file
|
146 |
####################################
|
147 |
# Bonus: Creating GIFs of predicted flows
|
148 |
# ---------------------------------------
|
|
|
170 |
#
|
171 |
# ffmpeg -f image2 -framerate 30 -i predicted_flow_%d.jpg -loop -1 flow.gif
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
+
gr.Interface(fn=infer, inputs=[], outputs=[gr.Textbox(), gr.Image(), gr.File()]).launch()
|