Skip to content

Commit

Permalink
mesure for each cam (#221)
Browse files Browse the repository at this point in the history
* mesure for each cam

* fix test

* use not gray image
  • Loading branch information
MateoLostanlen committed Jul 27, 2024
1 parent 7474e75 commit bf01bab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 40 deletions.
14 changes: 1 addition & 13 deletions pyroengine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def capture_camera_image(camera: ReolinkCamera, image_queue: asyncio.Queue
# Move camera to the next pose to avoid waiting
next_pos_id = camera.cam_poses[(idx + 1) % len(camera.cam_poses)]
camera.move_camera("ToPos", idx=int(next_pos_id), speed=50)
if frame is not None:
if frame is not None and is_day_time(None, frame, "ir"):
await image_queue.put((cam_id, frame))
await asyncio.sleep(0) # Yield control
else:
Expand Down Expand Up @@ -137,17 +137,6 @@ async def analyze_stream(self, image_queue: asyncio.Queue) -> None:
finally:
image_queue.task_done() # Mark the task as done

def check_day_time(self) -> None:
"""
Checks and updates the day_time attribute based on the current frame.
"""
try:
frame = self.cameras[0].capture()
if frame is not None:
self.day_time = is_day_time(None, frame, "ir")
except Exception as e:
logging.exception(f"Exception during initial day time check: {e}")

async def run(self, period: int = 30, send_alerts: bool = True) -> None:
"""
Captures and analyzes all camera streams, then processes alerts.
Expand All @@ -157,7 +146,6 @@ async def run(self, period: int = 30, send_alerts: bool = True) -> None:
send_alerts (bool): Boolean to activate / deactivate alert sending
"""
try:
self.check_day_time()

if self.day_time:
image_queue: asyncio.Queue[Any] = asyncio.Queue()
Expand Down
35 changes: 8 additions & 27 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ def mock_engine():


@pytest.fixture
def mock_cameras():
def mock_cameras(mock_wildfire_image):
camera = MagicMock()
camera.capture.return_value = Image.new("RGB", (100, 100)) # Mock captured image
camera.capture.return_value = mock_wildfire_image # Mock captured image
camera.cam_type = "static"
camera.ip_address = "192.168.1.1"
return [camera]


@pytest.fixture
def mock_cameras_ptz():
def mock_cameras_ptz(mock_wildfire_image):
camera = MagicMock()
camera.capture.return_value = Image.new("RGB", (100, 100)) # Mock captured image
camera.capture.return_value = mock_wildfire_image # Mock captured image
camera.cam_type = "ptz"
camera.cam_poses = [1, 2]
camera.ip_address = "192.168.1.1"
Expand Down Expand Up @@ -94,9 +94,9 @@ async def test_capture_images_ptz(system_controller_ptz):


@pytest.mark.asyncio
async def test_analyze_stream(system_controller):
async def test_analyze_stream(system_controller, mock_wildfire_image):
queue = asyncio.Queue()
mock_frame = Image.new("RGB", (100, 100))
mock_frame = mock_wildfire_image
await queue.put(("192.168.1.1", mock_frame))

analyze_task = asyncio.create_task(system_controller.analyze_stream(queue))
Expand All @@ -118,9 +118,9 @@ async def test_capture_images_method(system_controller):


@pytest.mark.asyncio
async def test_analyze_stream_method(system_controller):
async def test_analyze_stream_method(system_controller, mock_wildfire_image):
queue = asyncio.Queue()
mock_frame = Image.new("RGB", (100, 100))
mock_frame = mock_wildfire_image
await queue.put(("192.168.1.1", mock_frame))
await queue.put(None) # Signal the end of the stream

Expand All @@ -129,25 +129,6 @@ async def test_analyze_stream_method(system_controller):
system_controller.engine.predict.assert_called_once_with(mock_frame, "192.168.1.1")


def test_check_day_time(system_controller):
with patch("pyroengine.core.is_day_time", return_value=True) as mock_is_day_time:
system_controller.check_day_time()
assert system_controller.day_time is True
mock_is_day_time.assert_called_once()

with patch("pyroengine.core.is_day_time", return_value=False) as mock_is_day_time:
system_controller.check_day_time()
assert system_controller.day_time is False
mock_is_day_time.assert_called_once()

with patch("pyroengine.core.is_day_time", side_effect=Exception("Error in is_day_time")) as mock_is_day_time, patch(
"pyroengine.core.logging.exception"
) as mock_logging_exception:
system_controller.check_day_time()
mock_is_day_time.assert_called_once()
mock_logging_exception.assert_called_once_with("Exception during initial day time check: Error in is_day_time")


def test_repr_method(system_controller):
repr_str = repr(system_controller)
# Check if the representation is a string
Expand Down

0 comments on commit bf01bab

Please sign in to comment.