Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,32 @@
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
from Fight_detec_func import fight_detec
|
| 7 |
from objec_detect_yolo import detection
|
| 8 |
-
import
|
| 9 |
|
| 10 |
-
def analyze_video(
|
| 11 |
-
if
|
| 12 |
return {"Error": "No video file uploaded."}
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
input_filename = os.path.basename(video_file_obj.name)
|
| 17 |
-
base, ext = os.path.splitext(input_filename)
|
| 18 |
-
unique_suffix = str(int(time.time() * 1000)) # Add timestamp for uniqueness
|
| 19 |
-
safe_base = "".join(c if c.isalnum() or c in ('-', '_') else '_' for c in base) # Sanitize name
|
| 20 |
-
video_path = os.path.join(temp_dir, f"{safe_base}_{unique_suffix}{ext}")
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
#
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
print(f"Processing video: {video_path}")
|
| 29 |
-
|
| 30 |
-
# Run detection functions
|
| 31 |
-
# fight_detec returns (result_string, prediction_score)
|
| 32 |
-
# detection returns (set_of_labels, output_video_path)
|
| 33 |
-
fight_status, _ = fight_detec(video_path, debug=False)
|
| 34 |
-
detected_objects_set, annotated_video_path = detection(video_path)
|
| 35 |
|
| 36 |
# Format results
|
| 37 |
-
# Convert set to sorted list for consistent JSON output
|
| 38 |
detected_objects_list = sorted(list(detected_objects_set))
|
| 39 |
|
| 40 |
print(f"Fight Status: {fight_status}")
|
| 41 |
print(f"Detected Objects: {detected_objects_list}")
|
| 42 |
-
#
|
| 43 |
-
#
|
|
|
|
| 44 |
|
| 45 |
results = {
|
| 46 |
"Fight Detection": fight_status,
|
|
@@ -48,42 +35,23 @@ def analyze_video(video_file_obj):
|
|
| 48 |
}
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
print(f"Error during processing: {
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
os.remove(video_path)
|
| 58 |
-
print(f"Removed temp video file: {video_path}")
|
| 59 |
-
except OSError as e:
|
| 60 |
-
print(f"Error removing temp file {video_path}: {e}")
|
| 61 |
-
if 'temp_dir' in locals() and os.path.exists(temp_dir):
|
| 62 |
-
try:
|
| 63 |
-
# Clean up the results dir created by objec_detect_yolo if it's inside temp_dir
|
| 64 |
-
results_dir_path = os.path.join(temp_dir, "results")
|
| 65 |
-
if os.path.exists(results_dir_path) and os.path.isdir(results_dir_path):
|
| 66 |
-
# Remove files inside results dir first
|
| 67 |
-
for item in os.listdir(results_dir_path):
|
| 68 |
-
item_path = os.path.join(results_dir_path, item)
|
| 69 |
-
if os.path.isfile(item_path):
|
| 70 |
-
os.remove(item_path)
|
| 71 |
-
os.rmdir(results_dir_path) # Now remove empty results dir
|
| 72 |
-
print(f"Removed temp results directory: {results_dir_path}")
|
| 73 |
-
|
| 74 |
-
os.rmdir(temp_dir) # Attempt to remove the main temp dir
|
| 75 |
-
print(f"Removed temp directory: {temp_dir}")
|
| 76 |
-
except OSError as e:
|
| 77 |
-
# Might fail if other files are present or dir not empty
|
| 78 |
-
print(f"Error removing temp directory {temp_dir} or its contents: {e}")
|
| 79 |
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
return results
|
| 82 |
|
| 83 |
-
# Interface Definition
|
| 84 |
iface = gr.Interface(
|
| 85 |
fn=analyze_video,
|
| 86 |
-
inputs=gr.Video(label="Upload Video"),
|
| 87 |
outputs=gr.JSON(label="Detection Results"),
|
| 88 |
title="Fight and Object Detection Analysis",
|
| 89 |
description="Upload a video (< 1 min recommended) to detect potential fights and specific objects (Fire, Gun, Knife, Smoke, License_Plate). Results appear as JSON.",
|
|
@@ -97,3 +65,4 @@ iface = gr.Interface(
|
|
| 97 |
# Launch the interface
|
| 98 |
if __name__ == "__main__":
|
| 99 |
iface.launch()
|
|
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
+
# No longer need tempfile or time here, Gradio manages the input temp file
|
| 6 |
from Fight_detec_func import fight_detec
|
| 7 |
from objec_detect_yolo import detection
|
| 8 |
+
import traceback # For better error logging
|
| 9 |
|
| 10 |
+
def analyze_video(video_filepath): # RENAMED parameter to reflect it's a string path
|
| 11 |
+
if video_filepath is None:
|
| 12 |
return {"Error": "No video file uploaded."}
|
| 13 |
|
| 14 |
+
# video_filepath *is* the path to the temporary file created by Gradio
|
| 15 |
+
print(f"Processing video: {video_filepath}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
try:
|
| 18 |
+
# Directly use the filepath provided by Gradio for analysis
|
| 19 |
+
# No need to copy the file again.
|
| 20 |
+
fight_status, _ = fight_detec(video_filepath, debug=False)
|
| 21 |
+
detected_objects_set, annotated_video_path = detection(video_filepath) # This function saves its own output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Format results
|
|
|
|
| 24 |
detected_objects_list = sorted(list(detected_objects_set))
|
| 25 |
|
| 26 |
print(f"Fight Status: {fight_status}")
|
| 27 |
print(f"Detected Objects: {detected_objects_list}")
|
| 28 |
+
# annotated_video_path points to the video saved by detection(),
|
| 29 |
+
# but we are not returning it to the user via JSON here.
|
| 30 |
+
# It exists within the Space's filesystem in the 'results' folder.
|
| 31 |
|
| 32 |
results = {
|
| 33 |
"Fight Detection": fight_status,
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"Error during processing video: {video_filepath}")
|
| 39 |
+
print(f"Error type: {type(e).__name__}")
|
| 40 |
+
print(f"Error message: {e}")
|
| 41 |
+
print("Traceback:")
|
| 42 |
+
traceback.print_exc() # Print detailed traceback to Space logs
|
| 43 |
+
results = {"Error": f"Processing failed. Check Space logs for details. Error: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# No explicit cleanup needed for video_filepath, Gradio handles its temporary input file.
|
| 46 |
+
# Cleanup for files created by detection() (like annotated_video_path)
|
| 47 |
+
# would ideally happen within that function or rely on the Space's ephemeral nature.
|
| 48 |
|
| 49 |
return results
|
| 50 |
|
| 51 |
+
# Interface Definition (remains the same)
|
| 52 |
iface = gr.Interface(
|
| 53 |
fn=analyze_video,
|
| 54 |
+
inputs=gr.Video(label="Upload Video"),
|
| 55 |
outputs=gr.JSON(label="Detection Results"),
|
| 56 |
title="Fight and Object Detection Analysis",
|
| 57 |
description="Upload a video (< 1 min recommended) to detect potential fights and specific objects (Fire, Gun, Knife, Smoke, License_Plate). Results appear as JSON.",
|
|
|
|
| 65 |
# Launch the interface
|
| 66 |
if __name__ == "__main__":
|
| 67 |
iface.launch()
|
| 68 |
+
|