Skip to content

Commit 9d90fd5

Browse files
feat: add detection strategies
1 parent 208da51 commit 9d90fd5

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
The base strategy for detector strategies.
3+
This strategy is used to define the interface for all detector strategies.
4+
"""
5+
6+
from abc import abstractmethod, ABCMeta
7+
from dataclasses import dataclass
8+
9+
import numpy
10+
11+
12+
@dataclass
13+
class DetectorResult:
14+
"""This class represents the result of a detector strategy."""
15+
image: numpy.ndarray
16+
human_found: bool
17+
regions: list[tuple[int, int, int, int]] = None
18+
num_detections: float = None
19+
20+
21+
class BaseDetectorStrategy(metaclass=ABCMeta):
22+
"""
23+
The base strategy for detector strategies.
24+
"""
25+
@staticmethod
26+
@abstractmethod
27+
def detect_humans(frame: numpy.ndarray) -> DetectorResult:
28+
"""This method detects if there are any humans in the frame."""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
The HOG descriptor strategy for detector strategies.
3+
"""
4+
import cv2
5+
import numpy
6+
from .base_detector_strategy import BaseDetectorStrategy, DetectorResult
7+
8+
9+
class HogDescriptorStrategy(BaseDetectorStrategy):
10+
"""
11+
The HOG descriptor strategy for detector strategies.
12+
"""
13+
@staticmethod
14+
def detect_humans(frame: numpy.ndarray) -> DetectorResult:
15+
"""This method detects if there are any humans in the frame."""
16+
# Detect humans in the frame.
17+
hog_detector = cv2.HOGDescriptor()
18+
hog_detector.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
19+
regions, num_detections = hog_detector.detectMultiScale(
20+
frame,
21+
winStride=(2, 2),
22+
padding=(4, 4),
23+
scale=1.03,
24+
)
25+
26+
result = DetectorResult(
27+
image=frame,
28+
human_found=len(num_detections) > 0,
29+
regions=regions,
30+
num_detections=num_detections,
31+
)
32+
return result

0 commit comments

Comments
 (0)