Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,51 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Function to
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
def video_processing(video_frame):
|
| 52 |
-
frame = cv2.cvtColor(video_frame, cv2.COLOR_BGR2RGB) # Convert to RGB
|
| 53 |
-
processed_frame = process_frame(frame)
|
| 54 |
-
return processed_frame
|
| 55 |
-
|
| 56 |
-
# Launch Gradio app
|
| 57 |
-
gr.Interface(
|
| 58 |
-
fn=video_processing,
|
| 59 |
-
inputs=gr.Video(streaming=True), # Correct the Video component
|
| 60 |
-
outputs="video",
|
| 61 |
-
live=True,
|
| 62 |
-
title="Suspicious Activity Detection"
|
| 63 |
-
).launch(debug=True)
|
| 64 |
-
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
from model import SuspiciousActivityModel # Import the model
|
| 6 |
+
|
| 7 |
+
# Initialize the model paths
|
| 8 |
+
lstm_model_path = 'suspicious_activity_model.h5' # Path to your LSTM model
|
| 9 |
+
yolo_model_path = 'yolov8n-pose.pt' # Path to your YOLO model
|
| 10 |
+
|
| 11 |
+
# Initialize the suspicious activity model
|
| 12 |
+
model = SuspiciousActivityModel(lstm_model_path, yolo_model_path)
|
| 13 |
+
|
| 14 |
+
# Function to process video frame
|
| 15 |
+
def process_video(video_frame):
|
| 16 |
+
# Check if the input frame is a valid NumPy array
|
| 17 |
+
if isinstance(video_frame, np.ndarray):
|
| 18 |
+
print(f"Frame shape: {video_frame.shape}") # Print the shape of the frame for debugging
|
| 19 |
+
|
| 20 |
+
# Convert frame from BGR to RGB (OpenCV uses BGR by default)
|
| 21 |
+
try:
|
| 22 |
+
frame_rgb = cv2.cvtColor(video_frame, cv2.COLOR_BGR2RGB)
|
| 23 |
+
except cv2.error as e:
|
| 24 |
+
print(f"Error in cvtColor: {e}")
|
| 25 |
+
return video_frame # Return the original frame if error occurs
|
| 26 |
+
|
| 27 |
+
# Call model to detect activity in the frame
|
| 28 |
+
label = model.detect_activity(frame_rgb)
|
| 29 |
+
|
| 30 |
+
# Add label to the frame (Optional: you can also draw bounding boxes)
|
| 31 |
+
cv2.putText(frame_rgb, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 32 |
+
|
| 33 |
+
# Convert back to BGR for Gradio (since it expects BGR format)
|
| 34 |
+
frame_bgr = cv2.cvtColor(frame_rgb, cv2.COLOR_RGB2BGR)
|
| 35 |
+
return frame_bgr
|
| 36 |
+
else:
|
| 37 |
+
print("Received invalid frame format")
|
| 38 |
+
return video_frame # Return the original frame if it's not valid
|
| 39 |
+
|
| 40 |
+
# Gradio interface
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=process_video, # Function that processes each frame
|
| 43 |
+
inputs=gr.Video(source="webcam", streaming=True), # Use webcam as input
|
| 44 |
+
outputs=gr.Video(), # Output is also a video
|
| 45 |
+
live=True, # Stream the video in real time
|
| 46 |
+
title="Suspicious Activity Detection" # Interface title
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Launch the app with public link
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
iface.launch(share=True) # Set share=True to create a public link
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|