Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| # No longer need tempfile or time here, Gradio manages the input temp file | |
| from Fight_detec_func import fight_detec | |
| from objec_detect_yolo import detection | |
| import traceback # For better error logging | |
| def analyze_video(video_filepath): # RENAMED parameter to reflect it's a string path | |
| if video_filepath is None: | |
| return {"Error": "No video file uploaded."} | |
| # video_filepath *is* the path to the temporary file created by Gradio | |
| print(f"Processing video: {video_filepath}") | |
| try: | |
| # Directly use the filepath provided by Gradio for analysis | |
| # No need to copy the file again. | |
| fight_status, _ = fight_detec(video_filepath, debug=False) | |
| detected_objects_set, annotated_video_path = detection(video_filepath) # This function saves its own output | |
| # Format results | |
| detected_objects_list = sorted(list(detected_objects_set)) | |
| print(f"Fight Status: {fight_status}") | |
| print(f"Detected Objects: {detected_objects_list}") | |
| # annotated_video_path points to the video saved by detection(), | |
| # but we are not returning it to the user via JSON here. | |
| # It exists within the Space's filesystem in the 'results' folder. | |
| results = { | |
| "Fight Detection": fight_status, | |
| "Detected Objects": detected_objects_list | |
| } | |
| except Exception as e: | |
| print(f"Error during processing video: {video_filepath}") | |
| print(f"Error type: {type(e).__name__}") | |
| print(f"Error message: {e}") | |
| print("Traceback:") | |
| traceback.print_exc() # Print detailed traceback to Space logs | |
| results = {"Error": f"Processing failed. Check Space logs for details. Error: {str(e)}"} | |
| # No explicit cleanup needed for video_filepath, Gradio handles its temporary input file. | |
| # Cleanup for files created by detection() (like annotated_video_path) | |
| # would ideally happen within that function or rely on the Space's ephemeral nature. | |
| return results | |
| # Interface Definition (remains the same) | |
| iface = gr.Interface( | |
| fn=analyze_video, | |
| inputs=gr.Video(label="Upload Video"), | |
| outputs=gr.JSON(label="Detection Results"), | |
| title="Fight and Object Detection Analysis", | |
| description="Upload a video (< 1 min recommended) to detect potential fights and specific objects (Fire, Gun, Knife, Smoke, License_Plate). Results appear as JSON.", | |
| allow_flagging='never', | |
| examples=[ | |
| # Add paths to example videos if you upload them to the HF repo | |
| # e.g., ["example_fight.mp4"], ["example_normal_gun.mp4"] | |
| ] | |
| ) | |
| # Launch the interface | |
| if __name__ == "__main__": | |
| iface.launch() | |