KillD00zer commited on
Commit
8862f2a
·
verified ·
1 Parent(s): e06363f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -29
app.py CHANGED
@@ -1,39 +1,100 @@
 
 
1
  import gradio as gr
2
  import os
3
  import tempfile
4
  from Fight_detec_func import fight_detec
5
- from objec_detect_yolo import detect_objects_in_video
6
-
7
- def analyze_video(video_path):
8
- temp_dir = "/tmp"
9
- output_path = os.path.join(temp_dir, "output.mp4")
10
-
11
- # Call your fight and object detection functions
12
- fight_result = fight_detec(video_path)
13
- crime_result, annotated_video_path = detect_objects_in_video(video_path)
14
-
15
- return {
16
- "Fight": fight_result,
17
- "Crime": crime_result,
18
- "Output Video": annotated_video_path
19
- }
20
-
21
-
22
- # Clean up
23
- os.remove(video_path)
24
- os.rmdir(temp_dir)
25
-
26
- return {
27
- "Fight Detection": fight_result[0],
28
- "YOLO Object Detection": yolo_result
29
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
31
  iface = gr.Interface(
32
  fn=analyze_video,
33
- inputs=gr.Video(label="Upload Video"),
34
  outputs=gr.JSON(label="Detection Results"),
35
- title="Fight and Object Detection System",
36
- description="Upload a video to detect fights and objects using our AI models"
 
 
 
 
 
37
  )
38
 
39
- iface.launch()
 
 
 
 
1
+ --- START OF FILE app.py ---
2
+
3
  import gradio as gr
4
  import os
5
  import tempfile
6
  from Fight_detec_func import fight_detec
7
+ from objec_detect_yolo import detection
8
+ import time # Added for unique temp file names
9
+
10
+ def analyze_video(video_file_obj):
11
+ if video_file_obj is None:
12
+ return {"Error": "No video file uploaded."}
13
+
14
+ temp_dir = tempfile.mkdtemp()
15
+ # Create a unique filename within the temp dir
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
+ # Gradio file object has '.name' attribute with the path to temp copy
24
+ # Copy it to ensure control over the path and name if needed downstream
25
+ with open(video_path, 'wb') as f_dst, open(video_file_obj.name, 'rb') as f_src:
26
+ f_dst.write(f_src.read())
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
+ # Note: annotated_video_path points to a file saved in the 'results' directory
43
+ # within the Space container, but we are not returning it via the UI here.
44
+
45
+ results = {
46
+ "Fight Detection": fight_status,
47
+ "Detected Objects": detected_objects_list
48
+ }
49
+
50
+ except Exception as e:
51
+ print(f"Error during processing: {e}")
52
+ results = {"Error": f"Processing failed: {str(e)}"}
53
+ finally:
54
+ # Clean up the specific temp file and directory
55
+ if 'video_path' in locals() and os.path.exists(video_path):
56
+ try:
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"), # Source can be 'upload' or 'webcam'
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.",
90
+ allow_flagging='never',
91
+ examples=[
92
+ # Add paths to example videos if you upload them to the HF repo
93
+ # e.g., ["example_fight.mp4"], ["example_normal_gun.mp4"]
94
+ ]
95
  )
96
 
97
+ # Launch the interface
98
+ if __name__ == "__main__":
99
+ iface.launch()
100
+ --- END OF FILE app.py ---