karthikmn commited on
Commit
a29f239
·
verified ·
1 Parent(s): 0917168

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ def extract_frames(video_path, frame_skip=3, num_frames=30):
5
+ """
6
+ Extract frames from the video for analysis.
7
+
8
+ video_path: str, path to the video file
9
+ frame_skip: int, number of frames to skip between detections (e.g., skip every 3rd frame)
10
+ num_frames: int, number of frames to extract for analysis
11
+
12
+ Returns:
13
+ - frames_list: List of processed video frames
14
+ """
15
+ frames_list = []
16
+ cap = cv2.VideoCapture(video_path)
17
+
18
+ frame_counter = 0
19
+ while frame_counter < num_frames:
20
+ ret, frame = cap.read()
21
+ if not ret:
22
+ break
23
+
24
+ # Skip frames based on the frame_skip value
25
+ if frame_counter % frame_skip == 0:
26
+ # Preprocess frame (resize, normalize, etc.)
27
+ resized_frame = cv2.resize(frame, (224, 224)) # Resizing to 224x224
28
+ normalized_frame = resized_frame / 255.0 # Normalizing to [0, 1]
29
+ frames_list.append(normalized_frame)
30
+
31
+ frame_counter += 1
32
+
33
+ cap.release()
34
+ return np.array(frames_list)
35
+
36
+ # Example usage
37
+ video_path = 'path_to_video.mp4'
38
+ frames = extract_frames(video_path, frame_skip=3, num_frames=30)
39
+ import torch
40
+ import cv2
41
+
42
+ # Load YOLOv5 model (from Hugging Face or directly from Ultralytics)
43
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5m')
44
+
45
+ def detect_objects_in_frame(frame):
46
+ results = model(frame) # Running YOLOv5 on the frame
47
+ return results
48
+
49
+ def process_video_for_detection(video_path):
50
+ cap = cv2.VideoCapture(video_path)
51
+ while cap.isOpened():
52
+ ret, frame = cap.read()
53
+ if not ret:
54
+ break
55
+
56
+ # Detect objects using YOLOv5
57
+ results = detect_objects_in_frame(frame)
58
+
59
+ # Show or process the detection results
60
+ results.show() # Display the detected frame
61
+
62
+ # Break the loop on pressing 'q'
63
+ if cv2.waitKey(1) & 0xFF == ord('q'):
64
+ break
65
+
66
+ cap.release()
67
+ cv2.destroyAllWindows()
68
+
69
+ # Example usage
70
+ process_video_for_detection('path_to_video.mp4')
71
+ import tensorflow as tf
72
+ from tensorflow.keras.models import Model
73
+ from tensorflow.keras.layers import Input, Dense
74
+ from tensorflow.keras.optimizers import Adam
75
+
76
+ # Build an autoencoder model
77
+ def build_autoencoder(input_shape):
78
+ input_layer = Input(shape=input_shape)
79
+ encoded = Dense(128, activation='relu')(input_layer)
80
+ encoded = Dense(64, activation='relu')(encoded)
81
+ decoded = Dense(128, activation='relu')(encoded)
82
+ decoded = Dense(input_shape[0], activation='sigmoid')(decoded)
83
+
84
+ autoencoder = Model(input_layer, decoded)
85
+ autoencoder.compile(optimizer=Adam(), loss='mean_squared_error')
86
+
87
+ return autoencoder
88
+
89
+ # Train the autoencoder on normal (non-abnormal) frames
90
+ def train_autoencoder(frames):
91
+ model = build_autoencoder(frames.shape[1:])
92
+ model.fit(frames, frames, epochs=10, batch_size=32, shuffle=True)
93
+ return model
94
+
95
+ # Predict anomalies by comparing reconstruction error
96
+ def detect_anomalies(autoencoder, frames):
97
+ reconstructed = autoencoder.predict(frames)
98
+ reconstruction_error = np.mean(np.abs(reconstructed - frames), axis=(1,2,3)) # Mean error per frame
99
+ return reconstruction_error
100
+ import gradio as gr
101
+
102
+ def predict(video):
103
+ video_path = video.name
104
+ frames = extract_frames(video_path)
105
+ # Call your detection functions here
106
+ anomaly_detection_result = detect_anomalies(autoencoder, frames)
107
+
108
+ if anomaly_detection_result > threshold: # Set a threshold for anomaly
109
+ return "Abnormal behavior detected!"
110
+ else:
111
+ return "Normal behavior detected"
112
+
113
+ iface = gr.Interface(fn=predict, inputs=gr.inputs.Video(), outputs=gr.outputs.Textbox())
114
+ iface.launch()