Update app.py
Browse files
app.py
CHANGED
|
@@ -23,10 +23,25 @@ def extract_keypoints(frame):
|
|
| 23 |
return flattened_keypoints
|
| 24 |
return None # Return None if no keypoints are detected
|
| 25 |
|
| 26 |
-
def
|
| 27 |
"""
|
| 28 |
-
Process
|
| 29 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Perform YOLO detection
|
| 31 |
results = yolo_model(frame, verbose=False)
|
| 32 |
for box in results[0].boxes:
|
|
@@ -57,15 +72,18 @@ def process_frame(frame):
|
|
| 57 |
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
| 58 |
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 59 |
else:
|
| 60 |
-
print("No valid keypoints detected for ROI. Skipping
|
| 61 |
else:
|
| 62 |
-
print("ROI size is zero. Skipping
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
def
|
| 67 |
"""
|
| 68 |
-
|
| 69 |
"""
|
| 70 |
# Open video capture
|
| 71 |
cap = cv2.VideoCapture(input_video)
|
|
@@ -85,11 +103,42 @@ def detect_suspicious_activity(input_video):
|
|
| 85 |
if not ret:
|
| 86 |
break
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Write processed frame to output video
|
| 92 |
-
out.write(
|
| 93 |
|
| 94 |
# Release resources
|
| 95 |
cap.release()
|
|
@@ -99,11 +148,17 @@ def detect_suspicious_activity(input_video):
|
|
| 99 |
|
| 100 |
# Create Gradio interface
|
| 101 |
iface = gr.Interface(
|
| 102 |
-
fn=
|
| 103 |
-
inputs=
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
title="Suspicious Activity Detection",
|
| 106 |
-
description="Upload
|
| 107 |
)
|
| 108 |
|
| 109 |
# Launch the interface
|
|
|
|
| 23 |
return flattened_keypoints
|
| 24 |
return None # Return None if no keypoints are detected
|
| 25 |
|
| 26 |
+
def process_input(input_media):
|
| 27 |
"""
|
| 28 |
+
Process either a video or an image for suspicious activity detection
|
| 29 |
"""
|
| 30 |
+
# Determine if input is a video or image path
|
| 31 |
+
is_video = input_media.lower().endswith(('.mp4', '.avi', '.mov'))
|
| 32 |
+
|
| 33 |
+
if is_video:
|
| 34 |
+
return process_video(input_media)
|
| 35 |
+
else:
|
| 36 |
+
return process_image(input_media)
|
| 37 |
+
|
| 38 |
+
def process_image(image_path):
|
| 39 |
+
"""
|
| 40 |
+
Process a single image for suspicious activity detection
|
| 41 |
+
"""
|
| 42 |
+
# Read the image
|
| 43 |
+
frame = cv2.imread(image_path)
|
| 44 |
+
|
| 45 |
# Perform YOLO detection
|
| 46 |
results = yolo_model(frame, verbose=False)
|
| 47 |
for box in results[0].boxes:
|
|
|
|
| 72 |
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
| 73 |
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 74 |
else:
|
| 75 |
+
print("No valid keypoints detected for ROI. Skipping.")
|
| 76 |
else:
|
| 77 |
+
print("ROI size is zero. Skipping.")
|
| 78 |
|
| 79 |
+
# Save the processed image
|
| 80 |
+
output_path = 'output_image.jpg'
|
| 81 |
+
cv2.imwrite(output_path, frame)
|
| 82 |
+
return output_path
|
| 83 |
|
| 84 |
+
def process_video(input_video):
|
| 85 |
"""
|
| 86 |
+
Process video for suspicious activity detection
|
| 87 |
"""
|
| 88 |
# Open video capture
|
| 89 |
cap = cv2.VideoCapture(input_video)
|
|
|
|
| 103 |
if not ret:
|
| 104 |
break
|
| 105 |
|
| 106 |
+
# Perform YOLO detection
|
| 107 |
+
results = yolo_model(frame, verbose=False)
|
| 108 |
+
for box in results[0].boxes:
|
| 109 |
+
cls = int(box.cls[0]) # Class ID
|
| 110 |
+
confidence = float(box.conf[0])
|
| 111 |
+
|
| 112 |
+
# Detect persons only (class_id 0 for 'person')
|
| 113 |
+
if cls == 0 and confidence > 0.5:
|
| 114 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
|
| 115 |
+
|
| 116 |
+
# Extract ROI for classification
|
| 117 |
+
roi = frame[y1:y2, x1:x2]
|
| 118 |
+
if roi.size > 0:
|
| 119 |
+
# Preprocess ROI to extract keypoints
|
| 120 |
+
keypoints = extract_keypoints(roi)
|
| 121 |
+
|
| 122 |
+
if keypoints is not None and len(keypoints) > 0:
|
| 123 |
+
# Standardize and reshape keypoints for LSTM input
|
| 124 |
+
keypoints_scaled = scaler.fit_transform([keypoints]) # Standardize features
|
| 125 |
+
keypoints_reshaped = keypoints_scaled.reshape((1, 1, len(keypoints))) # Reshape for LSTM
|
| 126 |
+
|
| 127 |
+
# Predict with LSTM model
|
| 128 |
+
prediction = (lstm_model.predict(keypoints_reshaped) > 0.5).astype(int)[0][0]
|
| 129 |
+
|
| 130 |
+
# Draw bounding box and label
|
| 131 |
+
color = (0, 0, 255) if prediction == 1 else (0, 255, 0)
|
| 132 |
+
label = 'Suspicious' if prediction == 1 else 'Normal'
|
| 133 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
| 134 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 135 |
+
else:
|
| 136 |
+
print("No valid keypoints detected for ROI. Skipping frame.")
|
| 137 |
+
else:
|
| 138 |
+
print("ROI size is zero. Skipping frame.")
|
| 139 |
|
| 140 |
# Write processed frame to output video
|
| 141 |
+
out.write(frame)
|
| 142 |
|
| 143 |
# Release resources
|
| 144 |
cap.release()
|
|
|
|
| 148 |
|
| 149 |
# Create Gradio interface
|
| 150 |
iface = gr.Interface(
|
| 151 |
+
fn=process_input,
|
| 152 |
+
inputs=[
|
| 153 |
+
gr.File(label="Upload Image or Video",
|
| 154 |
+
file_types=['image', 'video'],
|
| 155 |
+
type="filepath")
|
| 156 |
+
],
|
| 157 |
+
outputs=[
|
| 158 |
+
gr.File(label="Processed Media")
|
| 159 |
+
],
|
| 160 |
title="Suspicious Activity Detection",
|
| 161 |
+
description="Upload an image or video to detect suspicious activities using YOLO and LSTM models. Suspicious activities will be marked with red bounding boxes, normal activities with green."
|
| 162 |
)
|
| 163 |
|
| 164 |
# Launch the interface
|