|
|
|
|
|
import gradio as gr |
|
import os |
|
import tempfile |
|
from Fight_detec_func import fight_detec |
|
from objec_detect_yolo import detection |
|
import time |
|
|
|
def analyze_video(video_file_obj): |
|
if video_file_obj is None: |
|
return {"Error": "No video file uploaded."} |
|
|
|
temp_dir = tempfile.mkdtemp() |
|
|
|
input_filename = os.path.basename(video_file_obj.name) |
|
base, ext = os.path.splitext(input_filename) |
|
unique_suffix = str(int(time.time() * 1000)) |
|
safe_base = "".join(c if c.isalnum() or c in ('-', '_') else '_' for c in base) |
|
video_path = os.path.join(temp_dir, f"{safe_base}_{unique_suffix}{ext}") |
|
|
|
try: |
|
|
|
|
|
with open(video_path, 'wb') as f_dst, open(video_file_obj.name, 'rb') as f_src: |
|
f_dst.write(f_src.read()) |
|
|
|
print(f"Processing video: {video_path}") |
|
|
|
|
|
|
|
|
|
fight_status, _ = fight_detec(video_path, debug=False) |
|
detected_objects_set, annotated_video_path = detection(video_path) |
|
|
|
|
|
|
|
detected_objects_list = sorted(list(detected_objects_set)) |
|
|
|
print(f"Fight Status: {fight_status}") |
|
print(f"Detected Objects: {detected_objects_list}") |
|
|
|
|
|
|
|
results = { |
|
"Fight Detection": fight_status, |
|
"Detected Objects": detected_objects_list |
|
} |
|
|
|
except Exception as e: |
|
print(f"Error during processing: {e}") |
|
results = {"Error": f"Processing failed: {str(e)}"} |
|
finally: |
|
|
|
if 'video_path' in locals() and os.path.exists(video_path): |
|
try: |
|
os.remove(video_path) |
|
print(f"Removed temp video file: {video_path}") |
|
except OSError as e: |
|
print(f"Error removing temp file {video_path}: {e}") |
|
if 'temp_dir' in locals() and os.path.exists(temp_dir): |
|
try: |
|
|
|
results_dir_path = os.path.join(temp_dir, "results") |
|
if os.path.exists(results_dir_path) and os.path.isdir(results_dir_path): |
|
|
|
for item in os.listdir(results_dir_path): |
|
item_path = os.path.join(results_dir_path, item) |
|
if os.path.isfile(item_path): |
|
os.remove(item_path) |
|
os.rmdir(results_dir_path) |
|
print(f"Removed temp results directory: {results_dir_path}") |
|
|
|
os.rmdir(temp_dir) |
|
print(f"Removed temp directory: {temp_dir}") |
|
except OSError as e: |
|
|
|
print(f"Error removing temp directory {temp_dir} or its contents: {e}") |
|
|
|
|
|
return results |
|
|
|
|
|
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=[ |
|
|
|
|
|
] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|