Skip to content

Commit 8ac04dc

Browse files
feat: working version with ov cameras
1 parent 3f3f3ad commit 8ac04dc

5 files changed

Lines changed: 106 additions & 80 deletions

File tree

core/strategies/eye/base_eye_strategy.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ def get_detector(self) -> BaseDetectorStrategy:
2121
"""This method returns the detector strategy."""
2222

2323
@abstractmethod
24-
def check_if_detected(self) -> EyeStrategyResult:
25-
"""This method checks if there are any protectors around."""
24+
def get_frame(self) -> ndarray:
25+
"""This method returns the frame from the camera."""
2626

2727
def _detect_humans(self, frame: ndarray) -> DetectorResult:
2828
"""This method checks if there is a person in front of the camera."""
2929
return self.get_detector().detect_humans(frame)
30+
31+
def check_if_detected(self) -> EyeStrategyResult:
32+
"""This method checks if there are any protectors around."""
33+
# Get the frame from the camera.
34+
frame = self.get_frame()
35+
# Detect humans in the frame.
36+
result = self._detect_humans(frame)
37+
38+
if result.human_found:
39+
return EyeStrategyResult(image=frame, result=True)
40+
return EyeStrategyResult(image=frame, result=False)
3041

core/strategies/eye/camera_strategy.py

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
The Camera strategy for eye strategies.
3+
"""
4+
import logging
5+
6+
import cv2
7+
import numpy
8+
from picamera2 import Picamera2
9+
10+
from core.strategies.detectors.base_detector_strategy import BaseDetectorStrategy
11+
from core.strategies.eye.base_eye_strategy import BaseEyeStrategy
12+
13+
# Add logging support.
14+
logger = logging.getLogger(__name__)
15+
16+
17+
class PiCameraStrategy(BaseEyeStrategy):
18+
"""
19+
The camera strategy for eye strategies.
20+
"""
21+
def __init__(self):
22+
self._detector = None
23+
24+
# Interface methods.
25+
def set_detector(self, detector: BaseDetectorStrategy) -> None:
26+
"""This method sets the detector strategy."""
27+
self._detector = detector
28+
29+
def get_detector(self) -> BaseDetectorStrategy:
30+
"""This method returns the detector strategy."""
31+
return self._detector
32+
33+
def get_frame(self) -> numpy.ndarray:
34+
"""This method returns the frame from the camera."""
35+
# Internal attributes
36+
picam2 = Picamera2()
37+
picam2.configure(picam2.create_preview_configuration(
38+
main={"format": 'XRGB8888', "size": (640, 480)}
39+
))
40+
picam2.start()
41+
frame = picam2.capture_array()
42+
picam2.close()
43+
del picam2
44+
return frame
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
The Camera strategy for eye strategies.
3+
"""
4+
import logging
5+
6+
import cv2
7+
import numpy
8+
9+
from core.strategies.detectors.base_detector_strategy import BaseDetectorStrategy
10+
from core.strategies.eye.base_eye_strategy import BaseEyeStrategy
11+
12+
# Add logging support.
13+
logger = logging.getLogger(__name__)
14+
15+
16+
class UsbCameraStrategy(BaseEyeStrategy):
17+
"""
18+
The camera strategy for eye strategies.
19+
"""
20+
def __init__(self, camera_id: int = 0):
21+
self._camera_id = camera_id
22+
self._detector = None
23+
24+
# Interface methods.
25+
def set_detector(self, detector: BaseDetectorStrategy) -> None:
26+
"""This method sets the detector strategy."""
27+
self._detector = detector
28+
29+
def get_detector(self) -> BaseDetectorStrategy:
30+
"""This method returns the detector strategy."""
31+
return self._detector
32+
33+
def get_frame(self) -> numpy.ndarray:
34+
"""This method returns the frame from the camera."""
35+
# Create a camera object.
36+
camera = cv2.VideoCapture(self._camera_id)
37+
# Set the camera resolution.
38+
camera.set(3, 640)
39+
camera.set(4, 480)
40+
# Read the frame from the camera.
41+
_, frame = camera.read()
42+
# Release the camera.
43+
camera.release()
44+
return frame

main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from core.observers.subject.wifi_subject import WiFiSubject
77
from core.observers.observer.hss_observer import HomeSecuritySystemObserver
88
from core.strategies.wifi.ipaddress_strategy import IpAddressStrategy
9-
from core.strategies.eye.camera_strategy import CameraStrategy
9+
from core.strategies.eye.usbcamera_strategy import UsbCameraStrategy
10+
from core.strategies.eye.picamera_strategy import PiCameraStrategy
1011
from core.strategies.notifier.whatsapp_strategy import WhatsappStrategy
1112
from core.strategies.detectors.hog_descriptor_strategy import HogDescriptorStrategy
1213
from core.utils.datatypes import WhatsappReciever, Protector
@@ -28,11 +29,11 @@ def main():
2829
"""
2930
# Create a WhatsApp notifier.
3031
whatsapp_notifier = WhatsappStrategy()
31-
whatsapp_notifier.add_reciever(WhatsappReciever("Gokhan", "+90555555555", "xxxxxx"))
32+
whatsapp_notifier.add_reciever(WhatsappReciever("Gokhan", "tel_no", "api_key"))
3233

3334
# Create a Protector within IpAddressStrategy.
3435
ip_address_strategy = IpAddressStrategy()
35-
ip_address_strategy.add_protector(Protector("Gokhan_iPhone", "192.168.1.65"))
36+
ip_address_strategy.add_protector(Protector("Gokhan_iPhone", "tel_ip"))
3637

3738
# Create observer.
3839
hss_observer = HomeSecuritySystemObserver()
@@ -46,7 +47,7 @@ def main():
4647

4748
# Run subjects.
4849
wifi_subject.run(ip_address_strategy)
49-
camera = CameraStrategy(0)
50+
camera = PiCameraStrategy()
5051
camera.set_detector(HogDescriptorStrategy())
5152
eye_subject.run(camera)
5253

0 commit comments

Comments
 (0)