Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,11 @@ import numpy as np
|
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
| 6 |
import torch.optim as optim
|
| 7 |
-
import seaborn as sns
|
| 8 |
from facenet_pytorch import InceptionResnetV1, MTCNN
|
| 9 |
import mediapipe as mp
|
| 10 |
from fer import FER
|
| 11 |
-
from
|
| 12 |
-
from sklearn.
|
| 13 |
-
from sklearn.preprocessing import StandardScaler, MinMaxScaler
|
| 14 |
-
from sklearn.metrics import silhouette_score
|
| 15 |
from sklearn.decomposition import PCA
|
| 16 |
import umap
|
| 17 |
import pandas as pd
|
|
@@ -22,24 +19,18 @@ from PIL import Image
|
|
| 22 |
import gradio as gr
|
| 23 |
import tempfile
|
| 24 |
import shutil
|
| 25 |
-
import io
|
| 26 |
|
| 27 |
-
# Suppress TensorFlow warnings
|
| 28 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
| 29 |
-
import tensorflow as tf
|
| 30 |
-
|
| 31 |
-
tf.get_logger().setLevel('ERROR')
|
| 32 |
|
| 33 |
matplotlib.rcParams['figure.dpi'] = 400
|
| 34 |
matplotlib.rcParams['savefig.dpi'] = 400
|
| 35 |
|
| 36 |
# Initialize models and other global variables
|
| 37 |
-
device = 'cuda'
|
| 38 |
|
| 39 |
-
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.
|
| 40 |
model = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
| 41 |
mp_face_mesh = mp.solutions.face_mesh
|
| 42 |
-
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.
|
| 43 |
emotion_detector = FER(mtcnn=False)
|
| 44 |
|
| 45 |
|
|
@@ -188,7 +179,7 @@ def organize_faces_by_person(embeddings_by_frame, clusters, aligned_faces_folder
|
|
| 188 |
shutil.copy(src, dst)
|
| 189 |
|
| 190 |
|
| 191 |
-
def find_optimal_components(embeddings, max_components=
|
| 192 |
pca = PCA(n_components=max_components)
|
| 193 |
pca.fit(embeddings)
|
| 194 |
|
|
@@ -269,7 +260,7 @@ def save_person_data_to_csv(embeddings_by_frame, emotions_by_frame, clusters, de
|
|
| 269 |
|
| 270 |
|
| 271 |
class LSTMAutoencoder(nn.Module):
|
| 272 |
-
def __init__(self, input_size, hidden_size=
|
| 273 |
super(LSTMAutoencoder, self).__init__()
|
| 274 |
self.input_size = input_size
|
| 275 |
self.hidden_size = hidden_size
|
|
@@ -283,8 +274,8 @@ class LSTMAutoencoder(nn.Module):
|
|
| 283 |
return out
|
| 284 |
|
| 285 |
|
| 286 |
-
def lstm_anomaly_detection(X, feature_columns, raw_embedding_columns, epochs=100
|
| 287 |
-
device = 'cuda'
|
| 288 |
X = torch.FloatTensor(X).to(device)
|
| 289 |
if X.dim() == 2:
|
| 290 |
X = X.unsqueeze(0)
|
|
@@ -328,8 +319,8 @@ def lstm_anomaly_detection(X, feature_columns, raw_embedding_columns, epochs=100
|
|
| 328 |
|
| 329 |
return mse_all, mse_comp, mse_raw
|
| 330 |
|
| 331 |
-
def embedding_anomaly_detection(embeddings, epochs=100
|
| 332 |
-
device = '
|
| 333 |
X = torch.FloatTensor(embeddings).to(device)
|
| 334 |
if X.dim() == 2:
|
| 335 |
X = X.unsqueeze(0)
|
|
@@ -355,14 +346,14 @@ def embedding_anomaly_detection(embeddings, epochs=100, batch_size=64):
|
|
| 355 |
mse = np.mean(np.power(X.squeeze(0).cpu().numpy() - reconstructed, 2), axis=1)
|
| 356 |
return mse
|
| 357 |
|
| 358 |
-
def determine_anomalies(mse_values, threshold=
|
| 359 |
mean = np.mean(mse_values)
|
| 360 |
std = np.std(mse_values)
|
| 361 |
anomalies = mse_values > (mean + threshold * std)
|
| 362 |
return anomalies
|
| 363 |
|
| 364 |
|
| 365 |
-
def plot_mse(df, mse_values, title, color='blue', time_threshold=1, hide_first_n=
|
| 366 |
plt.figure(figsize=(16, 8), dpi=300)
|
| 367 |
fig, ax = plt.subplots(figsize=(16, 8))
|
| 368 |
|
|
@@ -520,13 +511,13 @@ def process_video(video_path, desired_fps, batch_size, progress=gr.Progress()):
|
|
| 520 |
X, feature_columns, raw_embedding_columns, batch_size=batch_size)
|
| 521 |
|
| 522 |
progress(0.95, "Generating plots")
|
| 523 |
-
mse_plot_all = plot_mse(df, mse_all, "Facial Features + Emotions", color='blue', hide_first_n=
|
| 524 |
-
mse_plot_comp = plot_mse(df, mse_comp, "Facial Features", color='deepskyblue', hide_first_n=
|
| 525 |
-
mse_plot_raw = plot_mse(df, mse_raw, "Facial Embeddings", color='steelblue', hide_first_n=
|
| 526 |
|
| 527 |
emotion_plots = [
|
| 528 |
plot_mse(df, embedding_anomaly_detection(df[emotion].values.reshape(-1, 1)),
|
| 529 |
-
f"MSE: {emotion.capitalize()}", color=color, hide_first_n=
|
| 530 |
for emotion, color in zip(['fear', 'sad', 'angry', 'happy', 'surprise', 'neutral'],
|
| 531 |
['purple', 'green', 'orange', 'darkblue', 'gold', 'grey'])
|
| 532 |
]
|
|
@@ -569,8 +560,8 @@ iface = gr.Interface(
|
|
| 569 |
outputs=[
|
| 570 |
gr.Textbox(label="Anomaly Detection Results"),
|
| 571 |
gr.Plot(label="MSE: Facial Features + Emotions"),
|
| 572 |
-
gr.Plot(label="MSE: Facial Features
|
| 573 |
-
gr.Plot(label="MSE:
|
| 574 |
gr.Plot(label="MSE: Fear"),
|
| 575 |
gr.Plot(label="MSE: Sad"),
|
| 576 |
gr.Plot(label="MSE: Angry"),
|
|
@@ -590,7 +581,7 @@ iface = gr.Interface(
|
|
| 590 |
|
| 591 |
Adjust the parameters as needed:
|
| 592 |
- Desired FPS: Frames per second to analyze (lower for faster processing)
|
| 593 |
-
- Batch Size: Affects processing speed and memory usage
|
| 594 |
""",
|
| 595 |
allow_flagging="never"
|
| 596 |
)
|
|
|
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
| 6 |
import torch.optim as optim
|
|
|
|
| 7 |
from facenet_pytorch import InceptionResnetV1, MTCNN
|
| 8 |
import mediapipe as mp
|
| 9 |
from fer import FER
|
| 10 |
+
from sklearn.cluster import DBSCAN
|
| 11 |
+
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
|
|
|
| 12 |
from sklearn.decomposition import PCA
|
| 13 |
import umap
|
| 14 |
import pandas as pd
|
|
|
|
| 19 |
import gradio as gr
|
| 20 |
import tempfile
|
| 21 |
import shutil
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
matplotlib.rcParams['figure.dpi'] = 400
|
| 25 |
matplotlib.rcParams['savefig.dpi'] = 400
|
| 26 |
|
| 27 |
# Initialize models and other global variables
|
| 28 |
+
device = 'cuda'
|
| 29 |
|
| 30 |
+
mtcnn = MTCNN(keep_all=False, device=device, thresholds=[0.98, 0.98, 0.98], min_face_size=100)
|
| 31 |
model = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
| 32 |
mp_face_mesh = mp.solutions.face_mesh
|
| 33 |
+
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.7)
|
| 34 |
emotion_detector = FER(mtcnn=False)
|
| 35 |
|
| 36 |
|
|
|
|
| 179 |
shutil.copy(src, dst)
|
| 180 |
|
| 181 |
|
| 182 |
+
def find_optimal_components(embeddings, max_components=20):
|
| 183 |
pca = PCA(n_components=max_components)
|
| 184 |
pca.fit(embeddings)
|
| 185 |
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
class LSTMAutoencoder(nn.Module):
|
| 263 |
+
def __init__(self, input_size, hidden_size=128, num_layers=2):
|
| 264 |
super(LSTMAutoencoder, self).__init__()
|
| 265 |
self.input_size = input_size
|
| 266 |
self.hidden_size = hidden_size
|
|
|
|
| 274 |
return out
|
| 275 |
|
| 276 |
|
| 277 |
+
def lstm_anomaly_detection(X, feature_columns, raw_embedding_columns, epochs=100):
|
| 278 |
+
device = 'cuda'
|
| 279 |
X = torch.FloatTensor(X).to(device)
|
| 280 |
if X.dim() == 2:
|
| 281 |
X = X.unsqueeze(0)
|
|
|
|
| 319 |
|
| 320 |
return mse_all, mse_comp, mse_raw
|
| 321 |
|
| 322 |
+
def embedding_anomaly_detection(embeddings, epochs=100):
|
| 323 |
+
device = 'cpu'
|
| 324 |
X = torch.FloatTensor(embeddings).to(device)
|
| 325 |
if X.dim() == 2:
|
| 326 |
X = X.unsqueeze(0)
|
|
|
|
| 346 |
mse = np.mean(np.power(X.squeeze(0).cpu().numpy() - reconstructed, 2), axis=1)
|
| 347 |
return mse
|
| 348 |
|
| 349 |
+
def determine_anomalies(mse_values, threshold=4):
|
| 350 |
mean = np.mean(mse_values)
|
| 351 |
std = np.std(mse_values)
|
| 352 |
anomalies = mse_values > (mean + threshold * std)
|
| 353 |
return anomalies
|
| 354 |
|
| 355 |
|
| 356 |
+
def plot_mse(df, mse_values, title, color='blue', time_threshold=1, hide_first_n=5):
|
| 357 |
plt.figure(figsize=(16, 8), dpi=300)
|
| 358 |
fig, ax = plt.subplots(figsize=(16, 8))
|
| 359 |
|
|
|
|
| 511 |
X, feature_columns, raw_embedding_columns, batch_size=batch_size)
|
| 512 |
|
| 513 |
progress(0.95, "Generating plots")
|
| 514 |
+
mse_plot_all = plot_mse(df, mse_all, "Facial Features + Emotions", color='blue', hide_first_n=5)
|
| 515 |
+
mse_plot_comp = plot_mse(df, mse_comp, "Facial Features", color='deepskyblue', hide_first_n=5)
|
| 516 |
+
mse_plot_raw = plot_mse(df, mse_raw, "Facial Embeddings", color='steelblue', hide_first_n=5)
|
| 517 |
|
| 518 |
emotion_plots = [
|
| 519 |
plot_mse(df, embedding_anomaly_detection(df[emotion].values.reshape(-1, 1)),
|
| 520 |
+
f"MSE: {emotion.capitalize()}", color=color, hide_first_n=5)
|
| 521 |
for emotion, color in zip(['fear', 'sad', 'angry', 'happy', 'surprise', 'neutral'],
|
| 522 |
['purple', 'green', 'orange', 'darkblue', 'gold', 'grey'])
|
| 523 |
]
|
|
|
|
| 560 |
outputs=[
|
| 561 |
gr.Textbox(label="Anomaly Detection Results"),
|
| 562 |
gr.Plot(label="MSE: Facial Features + Emotions"),
|
| 563 |
+
gr.Plot(label="MSE: Facial Features"),
|
| 564 |
+
gr.Plot(label="MSE: Facial Embeddings"),
|
| 565 |
gr.Plot(label="MSE: Fear"),
|
| 566 |
gr.Plot(label="MSE: Sad"),
|
| 567 |
gr.Plot(label="MSE: Angry"),
|
|
|
|
| 581 |
|
| 582 |
Adjust the parameters as needed:
|
| 583 |
- Desired FPS: Frames per second to analyze (lower for faster processing)
|
| 584 |
+
- Batch Size: Affects processing speed and GPU memory usage
|
| 585 |
""",
|
| 586 |
allow_flagging="never"
|
| 587 |
)
|