Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,38 +3,56 @@ import numpy as np
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
-
# TensorFlow / Keras
|
| 7 |
# =========================
|
| 8 |
import tensorflow as tf
|
| 9 |
from tensorflow.keras.applications.resnet50 import preprocess_input as resnet_preprocess
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# =========================
|
| 27 |
-
#
|
| 28 |
# =========================
|
| 29 |
-
import cv2
|
| 30 |
import torch
|
| 31 |
from facenet_pytorch import MTCNN
|
|
|
|
| 32 |
import dlib
|
| 33 |
from imutils import face_utils
|
| 34 |
from scipy.spatial import distance as dist
|
| 35 |
|
| 36 |
warnings.filterwarnings("ignore")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# =========================
|
| 39 |
# Paths / Config
|
| 40 |
# =========================
|
|
@@ -42,33 +60,18 @@ VIDEO_MODEL_PATH = "models/video_model.h5"
|
|
| 42 |
DLIB_LANDMARK_MODEL = "shape_predictor_68_face_landmarks.dat"
|
| 43 |
|
| 44 |
IMG_SIZE = (224, 224)
|
| 45 |
-
FRAME_STEP = 5
|
| 46 |
-
NUM_MAX_FACES = 300
|
| 47 |
-
|
| 48 |
-
# Blink (EAR) features
|
| 49 |
EAR_THRESHOLD = 0.25
|
| 50 |
EAR_CONSEC_FRAMES = 3
|
| 51 |
-
|
| 52 |
-
# Prediction threshold
|
| 53 |
PRED_THRESHOLD = 0.5
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
# Lazy-loaded state
|
| 57 |
-
# =========================
|
| 58 |
_video_model = None
|
| 59 |
_mtcnn = None
|
| 60 |
_dlib_detector = None
|
| 61 |
_dlib_predictor = None
|
| 62 |
|
| 63 |
-
# Torch / MTCNN device selection
|
| 64 |
-
_torch_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 65 |
-
if torch.cuda.is_available():
|
| 66 |
-
torch.backends.cudnn.benchmark = True
|
| 67 |
-
try:
|
| 68 |
-
torch.set_float32_matmul_precision("medium")
|
| 69 |
-
except Exception:
|
| 70 |
-
pass
|
| 71 |
-
print(f"[INFO] PyTorch device: {_torch_device}")
|
| 72 |
|
| 73 |
def lazy_load():
|
| 74 |
global _video_model, _mtcnn, _dlib_detector, _dlib_predictor
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
+
# TensorFlow / Keras Setup
|
| 7 |
# =========================
|
| 8 |
import tensorflow as tf
|
| 9 |
from tensorflow.keras.applications.resnet50 import preprocess_input as resnet_preprocess
|
| 10 |
|
| 11 |
+
# Limit TensorFlow thread usage (prevents OOM)
|
| 12 |
+
os.environ["OMP_NUM_THREADS"] = "2"
|
| 13 |
+
os.environ["MKL_NUM_THREADS"] = "2"
|
| 14 |
+
os.environ["OPENBLAS_NUM_THREADS"] = "2"
|
| 15 |
+
os.environ["NUMEXPR_NUM_THREADS"] = "2"
|
| 16 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
| 17 |
+
|
| 18 |
+
# Try to enable GPU safely for TensorFlow
|
| 19 |
+
try:
|
| 20 |
+
gpus = tf.config.experimental.list_physical_devices("GPU")
|
| 21 |
+
if gpus:
|
| 22 |
+
for gpu in gpus:
|
| 23 |
+
tf.config.experimental.set_memory_growth(gpu, True)
|
| 24 |
+
tf.keras.mixed_precision.set_global_policy("mixed_float16")
|
| 25 |
+
print("[INFO] ✅ TensorFlow GPU detected & configured (mixed precision ON).")
|
| 26 |
+
else:
|
| 27 |
+
print("[WARN] ⚠️ No TensorFlow GPU detected, running on CPU.")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"[WARN] TensorFlow GPU init skipped: {e}")
|
| 30 |
|
| 31 |
# =========================
|
| 32 |
+
# PyTorch / MTCNN Setup
|
| 33 |
# =========================
|
|
|
|
| 34 |
import torch
|
| 35 |
from facenet_pytorch import MTCNN
|
| 36 |
+
import cv2
|
| 37 |
import dlib
|
| 38 |
from imutils import face_utils
|
| 39 |
from scipy.spatial import distance as dist
|
| 40 |
|
| 41 |
warnings.filterwarnings("ignore")
|
| 42 |
|
| 43 |
+
# Use GPU for MTCNN if available
|
| 44 |
+
if torch.cuda.is_available():
|
| 45 |
+
_torch_device = torch.device("cuda")
|
| 46 |
+
torch.backends.cudnn.benchmark = True
|
| 47 |
+
try:
|
| 48 |
+
torch.set_float32_matmul_precision("medium")
|
| 49 |
+
except Exception:
|
| 50 |
+
pass
|
| 51 |
+
print("[INFO] ✅ PyTorch GPU detected — MTCNN will use CUDA.")
|
| 52 |
+
else:
|
| 53 |
+
_torch_device = torch.device("cpu")
|
| 54 |
+
print("[WARN] ⚠️ No GPU detected — MTCNN running on CPU.")
|
| 55 |
+
|
| 56 |
# =========================
|
| 57 |
# Paths / Config
|
| 58 |
# =========================
|
|
|
|
| 60 |
DLIB_LANDMARK_MODEL = "shape_predictor_68_face_landmarks.dat"
|
| 61 |
|
| 62 |
IMG_SIZE = (224, 224)
|
| 63 |
+
FRAME_STEP = 5
|
| 64 |
+
NUM_MAX_FACES = 300
|
|
|
|
|
|
|
| 65 |
EAR_THRESHOLD = 0.25
|
| 66 |
EAR_CONSEC_FRAMES = 3
|
|
|
|
|
|
|
| 67 |
PRED_THRESHOLD = 0.5
|
| 68 |
|
| 69 |
+
# Lazy-loaded globals
|
|
|
|
|
|
|
| 70 |
_video_model = None
|
| 71 |
_mtcnn = None
|
| 72 |
_dlib_detector = None
|
| 73 |
_dlib_predictor = None
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def lazy_load():
|
| 77 |
global _video_model, _mtcnn, _dlib_detector, _dlib_predictor
|