scfive commited on
Commit
c599e3a
·
verified ·
1 Parent(s): 5176ae6

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from object_detection import ObjectDetector
5
+ import tempfile
6
+ import os
7
+ import torch
8
+ import time
9
+
10
+ # Initialize the detector
11
+ try:
12
+ detector = ObjectDetector()
13
+ print("Detector initialized successfully")
14
+ except Exception as e:
15
+ print(f"Error initializing detector: {str(e)}")
16
+ detector = None
17
+
18
+ def process_video(video_path):
19
+ if detector is None:
20
+ return None, "Error: Detector initialization failed"
21
+
22
+ try:
23
+ start_time = time.time()
24
+ # Create a temporary directory for processed frames
25
+ with tempfile.TemporaryDirectory() as temp_dir:
26
+ # Open the video
27
+ cap = cv2.VideoCapture(video_path)
28
+ if not cap.isOpened():
29
+ return None, "Error: Could not open video file"
30
+
31
+ # Get video properties
32
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
33
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
34
+ fps = cap.get(cv2.CAP_PROP_FPS)
35
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
36
+
37
+ # Limit processing to first 100 frames for demo purposes
38
+ max_frames = min(100, total_frames)
39
+
40
+ # Create output video writer
41
+ output_path = os.path.join(temp_dir, "output.mp4")
42
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
43
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
44
+
45
+ frame_count = 0
46
+ processed_frames = 0
47
+ while cap.isOpened() and frame_count < max_frames:
48
+ ret, frame = cap.read()
49
+ if not ret:
50
+ break
51
+
52
+ # Process frame
53
+ try:
54
+ results = detector.detect(frame)
55
+
56
+ # Draw detections
57
+ for det in results['detections']:
58
+ x1, y1, x2, y2 = det['bbox']
59
+ cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
60
+ cv2.putText(frame, f"{det['class']} {det['confidence']:.2f}",
61
+ (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
62
+
63
+ # Draw pose detections
64
+ for pose in results['pose_detections']:
65
+ keypoints = pose['keypoints']
66
+ for kp in keypoints:
67
+ x, y, conf = kp
68
+ if conf > 0.5:
69
+ cv2.circle(frame, (int(x), int(y)), 4, (0, 0, 255), -1)
70
+
71
+ # Draw analysis box
72
+ y_offset = 30
73
+ cv2.putText(frame, f"Total Objects: {results['stats']['total_objects']}",
74
+ (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
75
+ y_offset += 30
76
+
77
+ # Draw scene context
78
+ cv2.putText(frame, "Scene Context:", (10, y_offset),
79
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
80
+ y_offset += 30
81
+ cv2.putText(frame, f"Scene Type: {results['analysis']['scene_context']['scene_type']}",
82
+ (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
83
+ y_offset += 30
84
+
85
+ # Draw cognitive analysis
86
+ cv2.putText(frame, "Cognitive Analysis:", (10, y_offset),
87
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
88
+ y_offset += 30
89
+ cv2.putText(frame, f"Group Activity: {results['analysis']['cognitive']['group_activity']}",
90
+ (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
91
+
92
+ # Write frame
93
+ out.write(frame)
94
+ processed_frames += 1
95
+ except Exception as e:
96
+ print(f"Error processing frame {frame_count}: {str(e)}")
97
+
98
+ frame_count += 1
99
+
100
+ # Process every 5th frame to speed up processing
101
+ if frame_count % 5 != 0:
102
+ continue
103
+
104
+ # Release resources
105
+ cap.release()
106
+ out.release()
107
+
108
+ # Calculate processing time
109
+ processing_time = time.time() - start_time
110
+
111
+ # Return the processed video with detailed status
112
+ status = f"Processing complete!\nProcessed {processed_frames} frames in {processing_time:.2f} seconds"
113
+ return output_path, status
114
+ except Exception as e:
115
+ return None, f"Error processing video: {str(e)}"
116
+
117
+ # Create Gradio interface
118
+ iface = gr.Interface(
119
+ fn=process_video,
120
+ inputs=gr.Video(),
121
+ outputs=[
122
+ gr.Video(label="Processed Video"),
123
+ gr.Textbox(label="Status")
124
+ ],
125
+ title="Glad8tr Video Analysis",
126
+ description="Upload a video to analyze objects, poses, and cognitive states. Note: Processing is limited to first 100 frames for demo purposes.",
127
+ examples=[
128
+ ["teensonstreet.mp4"]
129
+ ],
130
+ allow_flagging="never"
131
+ )
132
+
133
+ # Launch the interface
134
+ if __name__ == "__main__":
135
+ iface.launch(share=True)