Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,67 @@
|
|
1 |
-
import
|
2 |
-
from utils.video_processor import compare_videos
|
3 |
import os
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
with
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
os.makedirs("temp", exist_ok=True)
|
26 |
-
video1_path = os.path.join("temp", video1_file.name)
|
27 |
-
video2_path = os.path.join("temp", video2_file.name)
|
28 |
-
|
29 |
-
with open(video1_path, "wb") as f:
|
30 |
-
f.write(video1_file.getbuffer())
|
31 |
-
with open(video2_path, "wb") as f:
|
32 |
-
f.write(video2_file.getbuffer())
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# Clean up temporary files
|
48 |
os.remove(video1_path)
|
|
|
49 |
os.remove(video2_path)
|
50 |
-
os.remove(plot_path)
|
51 |
-
os.remove(diff_video_path)
|
52 |
-
|
53 |
-
except Exception as e:
|
54 |
-
st.error(f"Error during comparison: {str(e)}")
|
55 |
-
# Clean up temporary files if they exist
|
56 |
-
if os.path.exists(video1_path):
|
57 |
-
os.remove(video1_path)
|
58 |
-
if os.path.exists(video2_path):
|
59 |
-
os.remove(video2_path)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if __name__ == "__main__":
|
62 |
-
os.makedirs("utils/output", exist_ok=True)
|
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
from utils.video_processor import compare_videos
|
4 |
|
5 |
+
def compare_videos_interface(video1, video2):
|
6 |
+
if video1 is None or video2 is None:
|
7 |
+
return "Please upload both videos.", None, None
|
8 |
+
|
9 |
+
# Save uploaded videos temporarily
|
10 |
+
os.makedirs("temp", exist_ok=True)
|
11 |
+
video1_path = os.path.join("temp", os.path.basename(video1))
|
12 |
+
video2_path = os.path.join("temp", os.path.basename(video2))
|
13 |
+
|
14 |
+
# Copy uploaded files to temp directory
|
15 |
+
with open(video1_path, "wb") as f:
|
16 |
+
with open(video1, "rb") as v1:
|
17 |
+
f.write(v1.read())
|
18 |
+
with open(video2_path, "wb") as f:
|
19 |
+
with open(video2, "rb") as v2:
|
20 |
+
f.write(v2.read())
|
21 |
+
|
22 |
+
try:
|
23 |
+
# Compare videos
|
24 |
+
plot_path, diff_video_path = compare_videos(video1_path, video2_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Return results
|
27 |
+
return (
|
28 |
+
"Comparison complete!",
|
29 |
+
plot_path,
|
30 |
+
diff_video_path
|
31 |
+
)
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error during comparison: {str(e)}", None, None
|
35 |
+
|
36 |
+
finally:
|
37 |
+
# Clean up temporary files
|
38 |
+
if os.path.exists(video1_path):
|
|
|
39 |
os.remove(video1_path)
|
40 |
+
if os.path.exists(video2_path):
|
41 |
os.remove(video2_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# Define Gradio interface
|
44 |
+
with gr.Blocks(title="Video Comparison App") as demo:
|
45 |
+
gr.Markdown("# Video Comparison App")
|
46 |
+
gr.Markdown("Upload two videos of the same duration to compare frame-by-frame differences.")
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
video1_input = gr.Video(label="Upload First Video", format=["mp4", "avi", "mov"])
|
50 |
+
video2_input = gr.Video(label="Upload Second Video", format=["mp4", "avi", "mov"])
|
51 |
+
|
52 |
+
submit_button = gr.Button("Compare Videos")
|
53 |
+
|
54 |
+
output_text = gr.Textbox(label="Status")
|
55 |
+
output_plot = gr.Image(label="Difference Plot")
|
56 |
+
output_video = gr.Video(label="Difference Video")
|
57 |
+
|
58 |
+
submit_button.click(
|
59 |
+
fn=compare_videos_interface,
|
60 |
+
inputs=[video1_input, video2_input],
|
61 |
+
outputs=[output_text, output_plot, output_video]
|
62 |
+
)
|
63 |
+
|
64 |
+
# Launch the app
|
65 |
if __name__ == "__main__":
|
66 |
+
os.makedirs("utils/output", exist_ok=True)
|
67 |
+
demo.launch()
|