Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/added detect_remote_faces to face_recognition domain #605

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@
"optional": true,
"default": 5
},
{
"type": "boolean",
"name": "detect_remote_faces",
"description": "If true, faces trained using other methods will be detected.",
"optional": true,
"default": false
},
{
"type": "boolean",
"name": "train",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@
"optional": true,
"default": 5
},
{
"type": "boolean",
"name": "detect_remote_faces",
"description": "If true, faces trained using other methods will be detected.",
"optional": true,
"default": false
},
{
"type": "boolean",
"name": "train",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@
"optional": true,
"default": 5
},
{
"type": "boolean",
"name": "detect_remote_faces",
"description": "If true, faces trained using other methods will be detected.",
"optional": true,
"default": false
},
{
"type": "boolean",
"name": "train",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
"optional": true,
"default": 5
},
{
"type": "boolean",
"name": "detect_remote_faces",
"description": "If true, faces trained using other methods will be detected.",
"optional": true,
"default": false
},
{
"type": "select",
"options": [
Expand Down
3 changes: 2 additions & 1 deletion viseron/components/codeprojectai/face_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:
if detection["userid"] != "unknown":
self._logger.debug(f"Face found: {detection}")
self.known_face_found(
COMPONENT,
detection["userid"],
(
detection["x_min"],
Expand All @@ -96,7 +97,7 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:
confidence=detection["confidence"],
)
elif self._config[CONFIG_SAVE_UNKNOWN_FACES]:
self.unknown_face_found(cropped_frame)
self.unknown_face_found(COMPONENT, cropped_frame)

def process(self, post_processor_frame: PostProcessorFrame) -> None:
"""Process received frame."""
Expand Down
3 changes: 3 additions & 0 deletions viseron/components/compreface/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CONFIG_PREDICTION_COUNT = "prediction_count"
CONFIG_FACE_PLUGINS = "face_plugins"
CONFIG_STATUS = "status"
CONFIG_DETECT_REMOTE_FACES = "detect_remote_faces"

DEFAULT_TRAIN = False
DEFAULT_DET_PROB_THRESHOLD = 0.8
Expand All @@ -33,6 +34,7 @@
DEFAULT_PREDICTION_COUNT = 1
DEFAULT_FACE_PLUGINS: Final = None
DEFAULT_STATUS = False
DEFAULT_DETECT_REMOTE_FACES = False

DESC_TRAIN = (
"Train CompreFace to recognize faces on Viseron start. "
Expand Down Expand Up @@ -65,3 +67,4 @@
DESC_STATUS = (
"If true includes system information like execution_time and plugin_version fields."
)
DESC_DETECT_REMOTE_FACES = "If true faces trained using other methods will be detected."
3 changes: 2 additions & 1 deletion viseron/components/compreface/face_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:
if subject["similarity"] >= self._config[CONFIG_SIMILARITTY_THRESHOLD]:
self._logger.debug(f"Face found: {subject}")
self.known_face_found(
COMPONENT,
subject["subject"],
(
result["box"]["x_min"],
Expand All @@ -120,7 +121,7 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:
extra_attributes=result,
)
elif self._config[CONFIG_SAVE_UNKNOWN_FACES]:
self.unknown_face_found(cropped_frame)
self.unknown_face_found(COMPONENT, cropped_frame)

def process(self, post_processor_frame: PostProcessorFrame) -> None:
"""Process received frame."""
Expand Down
3 changes: 2 additions & 1 deletion viseron/components/deepstack/face_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:
if detection["userid"] != "unknown":
self._logger.debug(f"Face found: {detection}")
self.known_face_found(
COMPONENT,
detection["userid"],
(
detection["x_min"],
Expand All @@ -93,7 +94,7 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:
confidence=detection["confidence"],
)
elif self._config[CONFIG_SAVE_UNKNOWN_FACES]:
self.unknown_face_found(cropped_frame)
self.unknown_face_found(COMPONENT, cropped_frame)

def process(self, post_processor_frame: PostProcessorFrame) -> None:
"""Process received frame."""
Expand Down
4 changes: 2 additions & 2 deletions viseron/components/dlib/face_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def face_recognition(self, frame, detected_object: DetectedObject) -> None:

for face, coordinates in faces:
if face != "unknown":
self.known_face_found(face, coordinates)
self.known_face_found(COMPONENT, face, coordinates)
elif self._config[CONFIG_SAVE_UNKNOWN_FACES]:
self.unknown_face_found(cropped_frame)
self.unknown_face_found(COMPONENT, cropped_frame)

def process(self, post_processor_frame: PostProcessorFrame) -> None:
"""Process received frame."""
Expand Down
32 changes: 23 additions & 9 deletions viseron/domains/face_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

from .binary_sensor import FaceDetectionBinarySensor
from .const import (
CONFIG_DETECT_REMOTE_FACES,
CONFIG_EXPIRE_AFTER,
CONFIG_FACE_RECOGNITION_PATH,
CONFIG_SAVE_UNKNOWN_FACES,
CONFIG_UNKNOWN_FACES_PATH,
DEFAULT_DETECT_REMOTE_FACES,
DEFAULT_EXPIRE_AFTER,
DEFAULT_FACE_RECOGNITION_PATH,
DEFAULT_SAVE_UNKNOWN_FACES,
DEFAULT_UNKNOWN_FACES_PATH,
DESC_DETECT_REMOTE_FACES,
DESC_EXPIRE_AFTER,
DESC_FACE_RECOGNITION_PATH,
DESC_SAVE_UNKNOWN_FACES,
Expand Down Expand Up @@ -55,6 +58,11 @@
default=DEFAULT_EXPIRE_AFTER,
description=DESC_EXPIRE_AFTER,
): FLOAT_MIN_ZERO,
vol.Optional(
CONFIG_DETECT_REMOTE_FACES,
default=DEFAULT_DETECT_REMOTE_FACES,
description=DESC_DETECT_REMOTE_FACES,
): bool,
}
)

Expand Down Expand Up @@ -87,15 +95,17 @@ def __init__(self, vis, component, config, camera_identifier) -> None:
if config[CONFIG_SAVE_UNKNOWN_FACES]:
create_directory(config[CONFIG_UNKNOWN_FACES_PATH])

for face_dir in os.listdir(config[CONFIG_FACE_RECOGNITION_PATH]):
if face_dir == "unknown":
continue
vis.add_entity(
component, FaceDetectionBinarySensor(vis, self._camera, face_dir)
)
if os.path.isdir(config[CONFIG_FACE_RECOGNITION_PATH]):
for face_dir in os.listdir(config[CONFIG_FACE_RECOGNITION_PATH]):
if face_dir == "unknown":
continue
vis.add_entity(
component, FaceDetectionBinarySensor(vis, self._camera, face_dir)
)

def known_face_found(
self,
component: str,
face: str,
coordinates: tuple[int, int, int, int],
confidence=None,
Expand All @@ -106,6 +116,11 @@ def known_face_found(
if self._faces.get(face, None):
self._faces[face].timer.cancel()

if self._config[CONFIG_DETECT_REMOTE_FACES] and not self._faces.get(face, None):
self._vis.add_entity(
component, FaceDetectionBinarySensor(self._vis, self._camera, face)
)

# Adds a detected face and schedules an expiry timer
face_dict = FaceDict(
face,
Expand All @@ -127,11 +142,11 @@ def known_face_found(
)
self._faces[face] = face_dict

def unknown_face_found(self, frame) -> None:
def unknown_face_found(self, component: str, frame) -> None:
"""Save unknown faces."""
unique_id = (
f"{datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S-')}"
f"{str(uuid4())}.jpg"
f"{component}-{str(uuid4())}.jpg"
)
file_name = os.path.join(self._config[CONFIG_UNKNOWN_FACES_PATH], unique_id)
self._logger.debug(f"Unknown face found, saving to {file_name}")
Expand All @@ -151,4 +166,3 @@ def expire_face(self, face) -> None:
face=self._faces[face],
),
)
del self._faces[face]
5 changes: 5 additions & 0 deletions viseron/domains/face_recognition/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
CONFIG_SAVE_UNKNOWN_FACES = "save_unknown_faces"
CONFIG_UNKNOWN_FACES_PATH = "unknown_faces_path"
CONFIG_EXPIRE_AFTER = "expire_after"
CONFIG_DETECT_REMOTE_FACES = "detect_remote_faces"

DEFAULT_FACE_RECOGNITION_PATH = "/config/face_recognition/faces"
DEFAULT_SAVE_UNKNOWN_FACES = False
DEFAULT_UNKNOWN_FACES_PATH = f"{DEFAULT_FACE_RECOGNITION_PATH}/unknown"
DEFAULT_EXPIRE_AFTER = 5
DEFAULT_DETECT_REMOTE_FACES = False

DESC_FACE_RECOGNITION_PATH = (
"Path to folder which contains subdirectories with images for each face to track."
Expand All @@ -34,3 +36,6 @@
DESC_EXPIRE_AFTER = (
"Time in seconds before a detected face is no longer considered detected."
)
DESC_DETECT_REMOTE_FACES = (
"If true, faces trained using other methods will be detected."
)