File size: 2,685 Bytes
7a58ba4
8862f2a
f68e74f
 
4c13dea
f68e74f
8862f2a
4c13dea
8862f2a
4c13dea
 
8862f2a
 
4c13dea
 
8862f2a
 
4c13dea
 
 
 
8862f2a
 
 
 
 
 
4c13dea
 
 
8862f2a
 
 
 
 
 
 
4c13dea
 
 
 
 
 
8862f2a
4c13dea
 
 
8862f2a
 
f68e74f
4c13dea
f68e74f
 
4c13dea
f68e74f
8862f2a
 
 
 
 
 
 
f68e74f
 
8862f2a
 
 
4c13dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


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()