зеркало из
https://github.com/ssciwr/AMMICO.git
synced 2025-10-29 05:04:14 +02:00
Include a face mask detection model
Этот коммит содержится в:
родитель
2d06cd290a
Коммит
81f8f91bc0
2
.gitignore
поставляемый
2
.gitignore
поставляемый
@ -1,3 +1,5 @@
|
|||||||
|
data
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
|
|||||||
3
.gitmodules
поставляемый
3
.gitmodules
поставляемый
@ -1,3 +0,0 @@
|
|||||||
[submodule "Face-Mask-Detection"]
|
|
||||||
path = Face-Mask-Detection
|
|
||||||
url = https://github.com/chandrikadeb7/Face-Mask-Detection.git
|
|
||||||
@ -1 +0,0 @@
|
|||||||
Subproject commit 7e500749401bad4bb338790fbdb89b58e41ef2d9
|
|
||||||
1
MANIFEST.in
Обычный файл
1
MANIFEST.in
Обычный файл
@ -0,0 +1 @@
|
|||||||
|
recursive-include misinformation/model *.model
|
||||||
@ -1,10 +1,20 @@
|
|||||||
|
import cv2
|
||||||
import ipywidgets
|
import ipywidgets
|
||||||
|
import numpy as np
|
||||||
|
import os
|
||||||
|
|
||||||
|
from tensorflow.keras.models import load_model
|
||||||
|
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
||||||
|
from tensorflow.keras.preprocessing.image import img_to_array
|
||||||
from IPython.display import display
|
from IPython.display import display
|
||||||
from deepface import DeepFace
|
from deepface import DeepFace
|
||||||
from retinaface import RetinaFace
|
from retinaface import RetinaFace
|
||||||
|
|
||||||
|
|
||||||
|
# Module-wide storage for lazily loaded mask detection model
|
||||||
|
mask_detection_model = None
|
||||||
|
|
||||||
|
|
||||||
def facial_expression_analysis(img_path):
|
def facial_expression_analysis(img_path):
|
||||||
result = {"filename": img_path}
|
result = {"filename": img_path}
|
||||||
|
|
||||||
@ -18,10 +28,19 @@ def facial_expression_analysis(img_path):
|
|||||||
# Find the biggest face image in the detected ones
|
# Find the biggest face image in the detected ones
|
||||||
maxface = max(faces, key=lambda f: f.shape[0] * f.shape[1])
|
maxface = max(faces, key=lambda f: f.shape[0] * f.shape[1])
|
||||||
|
|
||||||
|
# Determine whether the face wears a mask
|
||||||
|
result["wears_mask"] = wears_mask(maxface)
|
||||||
|
|
||||||
|
# Adapt the features we are looking for depending on whether a mask is
|
||||||
|
# worn. White masks screw race detection, emotion detection is useless.
|
||||||
|
actions = ["age", "gender"]
|
||||||
|
if not result["wears_mask"]:
|
||||||
|
actions = actions + ["race", "emotion"]
|
||||||
|
|
||||||
# Run the full DeepFace analysis
|
# Run the full DeepFace analysis
|
||||||
result["deepface_results"] = DeepFace.analyze(
|
result["deepface_results"] = DeepFace.analyze(
|
||||||
img_path=maxface,
|
img_path=maxface,
|
||||||
actions=["age", "gender", "race", "emotion"],
|
actions=actions,
|
||||||
prog_bar=False,
|
prog_bar=False,
|
||||||
detector_backend="skip",
|
detector_backend="skip",
|
||||||
)
|
)
|
||||||
@ -33,6 +52,31 @@ def facial_expression_analysis(img_path):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def wears_mask(face):
|
||||||
|
global mask_detection_model
|
||||||
|
|
||||||
|
# Preprocess the face to match the assumptions of the face mask
|
||||||
|
# detection model
|
||||||
|
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
|
||||||
|
face = cv2.resize(face, (224, 224))
|
||||||
|
face = img_to_array(face)
|
||||||
|
face = preprocess_input(face)
|
||||||
|
face = np.expand_dims(face, axis=0)
|
||||||
|
|
||||||
|
# Lazily load the model
|
||||||
|
if mask_detection_model is None:
|
||||||
|
mask_detection_model = load_model(
|
||||||
|
os.path.join(os.path.split(__file__)[0], "models", "mask_detector.model")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run the model (ignoring output)
|
||||||
|
with ipywidgets.Output():
|
||||||
|
mask, withoutMask = mask_detection_model.predict(face)[0]
|
||||||
|
|
||||||
|
# Convert from np.bool_ to bool to later be able to serialize the result
|
||||||
|
return bool(mask > withoutMask)
|
||||||
|
|
||||||
|
|
||||||
class JSONContainer:
|
class JSONContainer:
|
||||||
"""Expose a Python dictionary as a JSON document in JupyterLab
|
"""Expose a Python dictionary as a JSON document in JupyterLab
|
||||||
rich display rendering.
|
rich display rendering.
|
||||||
|
|||||||
Загрузка…
x
Ссылка в новой задаче
Block a user