Instructions to use HuangYiYang/Face-Mask-Detection-Model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use HuangYiYang/Face-Mask-Detection-Model with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://HuangYiYang/Face-Mask-Detection-Model") - Notebooks
- Google Colab
- Kaggle
| import cv2 | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.preprocessing.image import img_to_array | |
| from tensorflow.keras.applications.mobilenet_v2 import preprocess_input | |
| model = load_model("mask_detector_final_model.keras") | |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
| cap = cv2.VideoCapture(0) | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| faces = face_cascade.detectMultiScale(gray, 1.1, 4) | |
| for (x, y, w, h) in faces: | |
| face = frame[y:y + h, x:x + w] | |
| face = cv2.resize(face, (224, 224)) | |
| face = img_to_array(face) | |
| face = preprocess_input(face) | |
| face = np.expand_dims(face, axis=0) | |
| (mask, withoutMask) = model.predict(face)[0] | |
| confidence = max(mask, withoutMask) | |
| print(f"Probabilities: Mask={mask:.4f}, No Mask={withoutMask:.4f}") | |
| if confidence > 0.9: | |
| label = "Mask" if mask > withoutMask else "No Mask" | |
| color = (0, 255, 0) if label == "Mask" else (0, 0, 255) | |
| cv2.putText(frame, f"{label}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) | |
| cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) | |
| cv2.imshow("Face Mask Detector", frame) | |
| if cv2.waitKey(1) & 0xFF == ord('q') or cv2.getWindowProperty("Face Mask Detector", cv2.WND_PROP_VISIBLE) < 1: | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() |