Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -93,57 +93,6 @@ with gr.Blocks(css=css) as demo:
|
|
93 |
example_files = [os.path.join('examples', filename) for filename in example_files]
|
94 |
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider, raw_file], fn=on_submit, cache_examples=True)
|
95 |
|
96 |
-
with gr.Tab("Video Depth Prediction"):
|
97 |
-
with gr.Row():
|
98 |
-
input_video = gr.Video(label="Input Video")
|
99 |
-
submit = gr.Button("Submit")
|
100 |
-
processed_video = gr.Video(label="Processed Video")
|
101 |
-
|
102 |
-
def on_submit(filename):
|
103 |
-
raw_video = cv2.VideoCapture(filename)
|
104 |
-
frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
105 |
-
frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
|
106 |
-
output_width = frame_width * 2 + margin_width
|
107 |
-
|
108 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
|
109 |
-
output_path = tmpfile.name
|
110 |
-
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (output_width, frame_height))
|
111 |
-
|
112 |
-
while raw_video.isOpened():
|
113 |
-
ret, raw_frame = raw_video.read()
|
114 |
-
if not ret:
|
115 |
-
break
|
116 |
-
|
117 |
-
frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB) / 255.0
|
118 |
-
|
119 |
-
frame = transform({'image': frame})['image']
|
120 |
-
frame = torch.from_numpy(frame).unsqueeze(0).to(DEVICE)
|
121 |
-
|
122 |
-
depth = predict_depth(model, frame)
|
123 |
-
|
124 |
-
depth = F.interpolate(depth[None], (frame_height, frame_width), mode='bilinear', align_corners=False)[0, 0]
|
125 |
-
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
126 |
-
|
127 |
-
depth = depth.cpu().numpy().astype(np.uint8)
|
128 |
-
depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)
|
129 |
-
|
130 |
-
split_region = np.ones((frame_height, margin_width, 3), dtype=np.uint8) * 255
|
131 |
-
combined_frame = cv2.hconcat([raw_frame, split_region, depth_color])
|
132 |
-
|
133 |
-
out.write(combined_frame)
|
134 |
-
|
135 |
-
raw_video.release()
|
136 |
-
out.release()
|
137 |
-
|
138 |
-
return output_path
|
139 |
-
|
140 |
-
submit.click(on_submit, inputs=[input_video], outputs=processed_video)
|
141 |
-
|
142 |
-
example_files = os.listdir('examples_video')
|
143 |
-
example_files.sort()
|
144 |
-
example_files = [os.path.join('examples_video', filename) for filename in example_files]
|
145 |
-
examples = gr.Examples(examples=example_files, inputs=[input_video], outputs=processed_video, fn=on_submit, cache_examples=True)
|
146 |
-
|
147 |
|
148 |
if __name__ == '__main__':
|
149 |
demo.queue().launch()
|
|
|
93 |
example_files = [os.path.join('examples', filename) for filename in example_files]
|
94 |
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider, raw_file], fn=on_submit, cache_examples=True)
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
if __name__ == '__main__':
|
98 |
demo.queue().launch()
|