nagasurendra's picture
Update app.py
223eefb verified
raw
history blame
2.19 kB
import gradio as gr
import os
from utils.video_processor import compare_videos
def compare_videos_interface(video1, video2):
if video1 is None or video2 is None:
return "Please upload both videos.", None, None
# Save uploaded videos temporarily
os.makedirs("temp", exist_ok=True)
video1_path = os.path.join("temp", os.path.basename(video1))
video2_path = os.path.join("temp", os.path.basename(video2))
# Copy uploaded files to temp directory
with open(video1_path, "wb") as f:
with open(video1, "rb") as v1:
f.write(v1.read())
with open(video2_path, "wb") as f:
with open(video2, "rb") as v2:
f.write(v2.read())
try:
# Compare videos
plot_path, diff_video_path = compare_videos(video1_path, video2_path)
# Return results
return (
"Comparison complete!",
plot_path,
diff_video_path
)
except Exception as e:
return f"Error during comparison: {str(e)}", None, None
finally:
# Clean up temporary files
if os.path.exists(video1_path):
os.remove(video1_path)
if os.path.exists(video2_path):
os.remove(video2_path)
# Define Gradio interface
with gr.Blocks(title="Video Comparison App") as demo:
gr.Markdown("# Video Comparison App")
gr.Markdown("Upload two videos of the same duration to compare frame-by-frame differences.")
with gr.Row():
video1_input = gr.Video(label="Upload First Video", format=["mp4", "avi", "mov"])
video2_input = gr.Video(label="Upload Second Video", format=["mp4", "avi", "mov"])
submit_button = gr.Button("Compare Videos")
output_text = gr.Textbox(label="Status")
output_plot = gr.Image(label="Difference Plot")
output_video = gr.Video(label="Difference Video")
submit_button.click(
fn=compare_videos_interface,
inputs=[video1_input, video2_input],
outputs=[output_text, output_plot, output_video]
)
# Launch the app
if __name__ == "__main__":
os.makedirs("utils/output", exist_ok=True)
demo.launch()