Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,9 +29,9 @@ matplotlib.rcParams['savefig.dpi'] = 400
|
|
| 29 |
# Initialize models and other global variables
|
| 30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
|
| 32 |
-
FIXED_FPS =
|
| 33 |
|
| 34 |
-
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.95, 0.95, 0.95], min_face_size=
|
| 35 |
model = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
| 36 |
|
| 37 |
mp_face_mesh = mp.solutions.face_mesh
|
|
@@ -215,7 +215,10 @@ def process_frames(frames_folder, aligned_faces_folder, frame_count, progress, b
|
|
| 215 |
if face.size > 0:
|
| 216 |
results = face_mesh.process(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
|
| 217 |
if results.multi_face_landmarks and is_frontal_face(results.multi_face_landmarks[0].landmark):
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
| 219 |
if aligned_face is not None:
|
| 220 |
aligned_face_resized = cv2.resize(aligned_face, (160, 160))
|
| 221 |
output_path = os.path.join(aligned_faces_folder, f"frame_{frame_num}_face.jpg")
|
|
@@ -290,22 +293,22 @@ class Autoencoder(nn.Module):
|
|
| 290 |
def __init__(self, input_size):
|
| 291 |
super(Autoencoder, self).__init__()
|
| 292 |
self.encoder = nn.Sequential(
|
| 293 |
-
nn.Linear(input_size,
|
| 294 |
-
nn.ReLU(),
|
| 295 |
-
nn.Linear(512, 256),
|
| 296 |
nn.ReLU(),
|
| 297 |
nn.Linear(256, 128),
|
| 298 |
nn.ReLU(),
|
| 299 |
-
nn.Linear(128, 64)
|
|
|
|
|
|
|
| 300 |
)
|
| 301 |
self.decoder = nn.Sequential(
|
|
|
|
|
|
|
| 302 |
nn.Linear(64, 128),
|
| 303 |
nn.ReLU(),
|
| 304 |
nn.Linear(128, 256),
|
| 305 |
nn.ReLU(),
|
| 306 |
-
nn.Linear(256,
|
| 307 |
-
nn.ReLU(),
|
| 308 |
-
nn.Linear(512, input_size)
|
| 309 |
)
|
| 310 |
|
| 311 |
def forward(self, x):
|
|
@@ -321,7 +324,7 @@ def determine_anomalies(mse_values, threshold):
|
|
| 321 |
anomalies = mse_values > (mean + threshold * std)
|
| 322 |
return anomalies
|
| 323 |
|
| 324 |
-
def anomaly_detection(X_embeddings, X_posture, epochs=200, batch_size=8, patience=
|
| 325 |
# Normalize posture
|
| 326 |
scaler_posture = MinMaxScaler()
|
| 327 |
X_posture_scaled = scaler_posture.fit_transform(X_posture.reshape(-1, 1))
|
|
@@ -519,8 +522,8 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=4):
|
|
| 519 |
|
| 520 |
|
| 521 |
def plot_mse_heatmap(mse_values, title, df):
|
| 522 |
-
plt.figure(figsize=(20,
|
| 523 |
-
fig, ax = plt.subplots(figsize=(20,
|
| 524 |
|
| 525 |
# Reshape MSE values to 2D array for heatmap
|
| 526 |
mse_2d = mse_values.reshape(1, -1)
|
|
@@ -626,7 +629,7 @@ def draw_pose_landmarks(frame, landmarks):
|
|
| 626 |
|
| 627 |
return annotated_frame
|
| 628 |
|
| 629 |
-
def get_all_face_samples(organized_faces_folder, output_folder, largest_cluster, max_samples=
|
| 630 |
face_samples = {"most_frequent": [], "others": []}
|
| 631 |
for cluster_folder in sorted(os.listdir(organized_faces_folder)):
|
| 632 |
if cluster_folder.startswith("person_"):
|
|
@@ -836,13 +839,13 @@ with gr.Blocks() as iface:
|
|
| 836 |
with gr.Row():
|
| 837 |
video_input = gr.Video()
|
| 838 |
|
| 839 |
-
anomaly_threshold = gr.Slider(minimum=1, maximum=5, step=0.1, value=3
|
| 840 |
process_btn = gr.Button("Process Video")
|
| 841 |
progress_bar = gr.Progress()
|
| 842 |
execution_time = gr.Number(label="Execution Time (seconds)")
|
| 843 |
|
| 844 |
with gr.Group(visible=False) as results_group:
|
| 845 |
-
results_text = gr.TextArea(label="Anomaly Detection Results", lines=
|
| 846 |
|
| 847 |
with gr.Tab("Facial Features"):
|
| 848 |
mse_features_plot = gr.Plot(label="MSE: Facial Features")
|
|
|
|
| 29 |
# Initialize models and other global variables
|
| 30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
|
| 32 |
+
FIXED_FPS = 5
|
| 33 |
|
| 34 |
+
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.95, 0.95, 0.95], min_face_size=80)
|
| 35 |
model = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
| 36 |
|
| 37 |
mp_face_mesh = mp.solutions.face_mesh
|
|
|
|
| 215 |
if face.size > 0:
|
| 216 |
results = face_mesh.process(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
|
| 217 |
if results.multi_face_landmarks and is_frontal_face(results.multi_face_landmarks[0].landmark):
|
| 218 |
+
|
| 219 |
+
#aligned_face = alignFace(face)
|
| 220 |
+
aligned_face = face
|
| 221 |
+
|
| 222 |
if aligned_face is not None:
|
| 223 |
aligned_face_resized = cv2.resize(aligned_face, (160, 160))
|
| 224 |
output_path = os.path.join(aligned_faces_folder, f"frame_{frame_num}_face.jpg")
|
|
|
|
| 293 |
def __init__(self, input_size):
|
| 294 |
super(Autoencoder, self).__init__()
|
| 295 |
self.encoder = nn.Sequential(
|
| 296 |
+
nn.Linear(input_size, 256),
|
|
|
|
|
|
|
| 297 |
nn.ReLU(),
|
| 298 |
nn.Linear(256, 128),
|
| 299 |
nn.ReLU(),
|
| 300 |
+
nn.Linear(128, 64),
|
| 301 |
+
nn.ReLU(),
|
| 302 |
+
nn.Linear(64, 32)
|
| 303 |
)
|
| 304 |
self.decoder = nn.Sequential(
|
| 305 |
+
nn.Linear(32, 64),
|
| 306 |
+
nn.ReLU(),
|
| 307 |
nn.Linear(64, 128),
|
| 308 |
nn.ReLU(),
|
| 309 |
nn.Linear(128, 256),
|
| 310 |
nn.ReLU(),
|
| 311 |
+
nn.Linear(256, input_size)
|
|
|
|
|
|
|
| 312 |
)
|
| 313 |
|
| 314 |
def forward(self, x):
|
|
|
|
| 324 |
anomalies = mse_values > (mean + threshold * std)
|
| 325 |
return anomalies
|
| 326 |
|
| 327 |
+
def anomaly_detection(X_embeddings, X_posture, epochs=200, batch_size=8, patience=5):
|
| 328 |
# Normalize posture
|
| 329 |
scaler_posture = MinMaxScaler()
|
| 330 |
X_posture_scaled = scaler_posture.fit_transform(X_posture.reshape(-1, 1))
|
|
|
|
| 522 |
|
| 523 |
|
| 524 |
def plot_mse_heatmap(mse_values, title, df):
|
| 525 |
+
plt.figure(figsize=(20, 5), dpi=400)
|
| 526 |
+
fig, ax = plt.subplots(figsize=(20, 5))
|
| 527 |
|
| 528 |
# Reshape MSE values to 2D array for heatmap
|
| 529 |
mse_2d = mse_values.reshape(1, -1)
|
|
|
|
| 629 |
|
| 630 |
return annotated_frame
|
| 631 |
|
| 632 |
+
def get_all_face_samples(organized_faces_folder, output_folder, largest_cluster, max_samples=500):
|
| 633 |
face_samples = {"most_frequent": [], "others": []}
|
| 634 |
for cluster_folder in sorted(os.listdir(organized_faces_folder)):
|
| 635 |
if cluster_folder.startswith("person_"):
|
|
|
|
| 839 |
with gr.Row():
|
| 840 |
video_input = gr.Video()
|
| 841 |
|
| 842 |
+
anomaly_threshold = gr.Slider(minimum=1, maximum=5, step=0.1, value=3, label="Anomaly Detection Threshold")
|
| 843 |
process_btn = gr.Button("Process Video")
|
| 844 |
progress_bar = gr.Progress()
|
| 845 |
execution_time = gr.Number(label="Execution Time (seconds)")
|
| 846 |
|
| 847 |
with gr.Group(visible=False) as results_group:
|
| 848 |
+
results_text = gr.TextArea(label="Anomaly Detection Results", lines=4)
|
| 849 |
|
| 850 |
with gr.Tab("Facial Features"):
|
| 851 |
mse_features_plot = gr.Plot(label="MSE: Facial Features")
|