Skip to content

Commit dbc0c21

Browse files
feat: add support for detector strategy selection
1 parent 9d90fd5 commit dbc0c21

2 files changed

Lines changed: 31 additions & 20 deletions

File tree

core/strategies/eye/base_eye_strategy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,28 @@
33
This strategy is used to define the interface for all eye strategies.
44
"""
55
from abc import abstractmethod, ABCMeta
6+
from numpy import ndarray
67
from core.utils.datatypes import EyeStrategyResult
8+
from core.strategies.detectors.base_detector_strategy import BaseDetectorStrategy, DetectorResult
79

810

911
class BaseEyeStrategy(metaclass=ABCMeta):
1012
"""
1113
The base strategy for eye strategies.
1214
"""
15+
@abstractmethod
16+
def set_detector(self, detector: BaseDetectorStrategy) -> None:
17+
"""This method sets the detector strategy."""
18+
19+
@abstractmethod
20+
def get_detector(self) -> BaseDetectorStrategy:
21+
"""This method returns the detector strategy."""
22+
1323
@abstractmethod
1424
def check_if_detected(self) -> EyeStrategyResult:
1525
"""This method checks if there are any protectors around."""
26+
27+
def _detect_humans(self, frame: ndarray) -> DetectorResult:
28+
"""This method checks if there is a person in front of the camera."""
29+
return self.get_detector().detect_humans(frame)
30+

core/strategies/eye/camera_strategy.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import cv2
88
import numpy
99

10+
from core.strategies.detectors.base_detector_strategy import BaseDetectorStrategy, DetectorResult
1011
from core.strategies.eye.base_eye_strategy import BaseEyeStrategy
1112
from core.utils.datatypes import EyeStrategyResult
1213

@@ -19,20 +20,30 @@ class CameraStrategy(BaseEyeStrategy):
1920
The camera strategy for eye strategies.
2021
"""
2122
def __init__(self, camera_id: int = 0):
22-
self._camera_id = camera_id
23+
self._camera_id = camera_id
24+
self._detector = None
25+
26+
# Interface methods.
27+
def set_detector(self, detector: BaseDetectorStrategy) -> None:
28+
"""This method sets the detector strategy."""
29+
self._detector = detector
30+
31+
def get_detector(self) -> BaseDetectorStrategy:
32+
"""This method returns the detector strategy."""
33+
return self._detector
2334

2435
def check_if_detected(self) -> EyeStrategyResult:
2536
"""This method checks if there are any protectors around."""
2637
# Get the frame from the camera.
2738
frame = self._get_frame()
2839
# Detect humans in the frame.
29-
regions, num_detections = self._detect_humans(frame)
40+
result = self._detect_humans(frame)
3041
# Outline the regions in the frame.
31-
self._outline_the_regions(frame, regions)
42+
# self._outline_the_regions(frame, regions)
3243
# Save the frame to the disk.
33-
self._save_frame(frame)
44+
# self._save_frame(frame)
3445
# If there is a human in the frame, return True.
35-
if len(num_detections) > 0:
46+
if result.human_found:
3647
return EyeStrategyResult(image=frame, result=True)
3748
return EyeStrategyResult(image=frame, result=False)
3849

@@ -49,21 +60,6 @@ def _get_frame(self) -> numpy.ndarray:
4960
# Release the camera.
5061
camera.release()
5162
return frame
52-
53-
def _detect_humans(self, frame: numpy.ndarray) -> tuple[list[tuple[int, int, int, int]], float]:
54-
"""This method checks if there is a person in front of the camera."""
55-
# Detect humans in the frame.
56-
hog_detector = cv2.HOGDescriptor()
57-
hog_detector.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
58-
regions, num_detections = hog_detector.detectMultiScale(
59-
frame,
60-
winStride=(4, 4),
61-
padding=(4, 4),
62-
scale=1.00
63-
)
64-
logger.debug("Number of detections: " + str(num_detections)
65-
+ " Regions: " + str(regions))
66-
return regions, num_detections
6763

6864
def _outline_the_regions(self, frame: numpy.ndarray, regions: list[tuple[int, int, int, int]]) -> None:
6965
"""This method outlines the regions in the frame."""

0 commit comments

Comments
 (0)