| | import gradio as gr |
| | import requests |
| | import os |
| |
|
| | |
| | |
| | BACKEND_URL = "https://am-om-backend.hf.space/track/" |
| | def track_video_via_api(input_video_path): |
| | if input_video_path is None: |
| | return None, "Please upload a video first." |
| | |
| | status = "Sending video to the backend for processing..." |
| | yield status, None, None |
| |
|
| | try: |
| | with open(input_video_path, "rb") as video_file: |
| | files = {'video': (os.path.basename(input_video_path), video_file, 'video/mp4')} |
| | |
| | |
| | with requests.post(BACKEND_URL, files=files, stream=True, timeout= 600 ) as response: |
| | response.raise_for_status() |
| | |
| | |
| | processed_video_path = "processed_video.mp4" |
| | with open(processed_video_path, "wb") as f: |
| | for chunk in response.iter_content(chunk_size=8192): |
| | f.write(chunk) |
| | |
| | |
| | |
| | status = "Processing complete!" |
| | yield status, processed_video_path, None |
| |
|
| | except requests.exceptions.RequestException as e: |
| | status = f"API Error: {e}" |
| | yield status, None, None |
| |
|
| | except requests.exceptions.ReadTimeout: |
| | gr.Error("The processing took too long (more than 10 minutes). Please try a shorter video.") |
| | return None |
| |
|
| | |
| |
|
| | |
| | with gr.Blocks() as demo: |
| | gr.Markdown("<h1 style='text-align:center; font-size:4em;'>Vehicle and Pedestrian Segmentation</h1>") |
| | gr.Markdown("Note: Processing on the free tier can take several minutes. Please be patient after clicking 'Start Tracking'.") |
| | input_video = gr.Video(label="Upload Video") |
| | start_btn = gr.Button("Start Tracking") |
| | status_display = gr.HTML("") |
| | output_video = gr.Video(label="Processed Video") |
| | output_json = gr.File(label="Download JSON Output") |
| |
|
| | start_btn.click( |
| | fn=track_video_via_api, |
| | inputs=input_video, |
| | outputs=[status_display, output_video, output_json] |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch(share = True) |
| |
|