Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load Hugging Face models
|
6 |
+
pose_detection = pipeline("object-detection", model="./yolov8-pose") # Local YOLOv8 pose model
|
7 |
+
suspicious_activity_detection = pipeline("text-classification", model="./suspicious_activity_model") # Local LSTM model
|
8 |
+
|
9 |
+
# Function to process a single frame and predict suspicious activity
|
10 |
+
def process_frame(frame):
|
11 |
+
"""
|
12 |
+
Detect persons in the frame and determine if they exhibit suspicious behavior.
|
13 |
+
"""
|
14 |
+
results = pose_detection(frame)
|
15 |
+
|
16 |
+
for person in results:
|
17 |
+
if person['label'] == 'person' and 'box' in person:
|
18 |
+
x1, y1, x2, y2 = map(int, person['box'].values())
|
19 |
+
|
20 |
+
if 'keypoints' in person:
|
21 |
+
keypoints = person['keypoints']
|
22 |
+
keypoints_input = " ".join(map(str, [kp for point in keypoints for kp in point[:2]]))
|
23 |
+
|
24 |
+
# Predict suspicious activity
|
25 |
+
prediction = suspicious_activity_detection(keypoints_input)[0]['label']
|
26 |
+
color = (0, 0, 255) if prediction == "Suspicious" else (0, 255, 0)
|
27 |
+
label = 'Suspicious' if prediction == "Suspicious" else 'Normal'
|
28 |
+
|
29 |
+
# Annotate frame
|
30 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
31 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
32 |
+
else:
|
33 |
+
print("No keypoints found for detected person.")
|
34 |
+
return frame
|
35 |
+
|
36 |
+
# Gradio interface
|
37 |
+
def live_detection(frame):
|
38 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert BGR to RGB for Gradio
|
39 |
+
processed_frame = process_frame(frame)
|
40 |
+
return cv2.cvtColor(processed_frame, cv2.COLOR_RGB2BGR) # Convert back to BGR for display
|
41 |
+
|
42 |
+
# Gradio UI
|
43 |
+
interface = gr.Interface(
|
44 |
+
fn=live_detection,
|
45 |
+
inputs=gr.Image(source="webcam", tool="editor", type="numpy"),
|
46 |
+
outputs=gr.Image(type="numpy", label="Processed Video Stream"),
|
47 |
+
live=True,
|
48 |
+
description="Real-time Suspicious Activity Detection"
|
49 |
+
)
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|