Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import shutil | |
| 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 | |
| # Validate file extensions | |
| valid_extensions = [".mp4", ".avi", ".mov"] | |
| if not any(video1.lower().endswith(ext) for ext in valid_extensions) or \ | |
| not any(video2.lower().endswith(ext) for ext in valid_extensions): | |
| return "Invalid video format. Please upload MP4, AVI, or MOV files.", 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 | |
| shutil.copy(video1, video1_path) | |
| shutil.copy(video2, video2_path) | |
| 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 (MP4, AVI, MOV) to compare frame-by-frame differences.") | |
| with gr.Row(): | |
| video1_input = gr.File(label="Upload First Video", file_types=[".mp4", ".avi", ".mov"]) | |
| video2_input = gr.File(label="Upload Second Video", file_types=[".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() |