Compare commits
13 Commits
feat/video
...
feat/face-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e129113c9e | ||
|
|
18a4bde4ca | ||
|
|
815fc7bcde | ||
|
|
3bbe97579d | ||
| ad58b16559 | |||
| fb0d7850cc | |||
|
|
4afceccf46 | ||
|
|
83099a2810 | ||
|
|
4e9afbaaf5 | ||
|
|
49386ef8cd | ||
|
|
3b470c8f29 | ||
|
|
b8f71f6bee | ||
| aad2044b6e |
@@ -7,4 +7,3 @@ sphinx
|
|||||||
sphinx_rtd_theme
|
sphinx_rtd_theme
|
||||||
pre-commit
|
pre-commit
|
||||||
python-dotenv
|
python-dotenv
|
||||||
opencv-python==4.1.2.30
|
|
||||||
@@ -17,6 +17,10 @@ class AgentSettings(object):
|
|||||||
:vartype video_sender_port: int
|
:vartype video_sender_port: int
|
||||||
:ivar audio_sender_port: Port used for sending audio data, defaults to 5558.
|
:ivar audio_sender_port: Port used for sending audio data, defaults to 5558.
|
||||||
:vartype audio_sender_port: int
|
:vartype audio_sender_port: int
|
||||||
|
:ivar face_detection_port: Port used for sending face detection events, defaults to 5559.
|
||||||
|
:vartype face_detection_port: int
|
||||||
|
:ivar face_detection_interval: Time between face detection events, defaults to 1000 ms.
|
||||||
|
:vartype face_detection_interval: int
|
||||||
"""
|
"""
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -25,12 +29,16 @@ class AgentSettings(object):
|
|||||||
main_receiver_port=None,
|
main_receiver_port=None,
|
||||||
video_sender_port=None,
|
video_sender_port=None,
|
||||||
audio_sender_port=None,
|
audio_sender_port=None,
|
||||||
|
face_detection_port=None,
|
||||||
|
face_detection_interval=None,
|
||||||
):
|
):
|
||||||
self.control_backend_host = get_config(control_backend_host, "AGENT__CONTROL_BACKEND_HOST", "localhost")
|
self.control_backend_host = get_config(control_backend_host, "AGENT__CONTROL_BACKEND_HOST", "localhost")
|
||||||
self.actuation_receiver_port = get_config(actuation_receiver_port, "AGENT__ACTUATION_RECEIVER_PORT", 5557, int)
|
self.actuation_receiver_port = get_config(actuation_receiver_port, "AGENT__ACTUATION_RECEIVER_PORT", 5557, int)
|
||||||
self.main_receiver_port = get_config(main_receiver_port, "AGENT__MAIN_RECEIVER_PORT", 5555, int)
|
self.main_receiver_port = get_config(main_receiver_port, "AGENT__MAIN_RECEIVER_PORT", 5555, int)
|
||||||
self.video_sender_port = get_config(video_sender_port, "AGENT__VIDEO_SENDER_PORT", 5556, int)
|
self.video_sender_port = get_config(video_sender_port, "AGENT__VIDEO_SENDER_PORT", 5556, int)
|
||||||
self.audio_sender_port = get_config(audio_sender_port, "AGENT__AUDIO_SENDER_PORT", 5558, int)
|
self.audio_sender_port = get_config(audio_sender_port, "AGENT__AUDIO_SENDER_PORT", 5558, int)
|
||||||
|
self.face_detection_port = get_config(face_detection_port, "AGENT__FACE_DETECTION_PORT", 5559, int)
|
||||||
|
self.face_detection_interval = get_config(face_detection_interval, "AGENT__FACE_DETECTION_INTERVAL", 1000, int)
|
||||||
|
|
||||||
|
|
||||||
class VideoConfig(object):
|
class VideoConfig(object):
|
||||||
|
|||||||
93
src/robot_interface/endpoints/face_detector.py
Normal file
93
src/robot_interface/endpoints/face_detector.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
This program has been developed by students from the bachelor Computer Science at Utrecht
|
||||||
|
University within the Software Project course.
|
||||||
|
© Copyright Utrecht University (Department of Information and Computing Sciences)
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
from robot_interface.endpoints.socket_base import SocketBase
|
||||||
|
from robot_interface.state import state
|
||||||
|
from robot_interface.core.config import settings
|
||||||
|
|
||||||
|
|
||||||
|
class FaceDetectionSender(SocketBase):
|
||||||
|
"""
|
||||||
|
Face detection endpoint.
|
||||||
|
|
||||||
|
Subscribes to and polls ALMemory["FaceDetected"], sends events to CB.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, zmq_context, port=settings.agent_settings.face_detection_port):
|
||||||
|
super(FaceDetectionSender, self).__init__("face")
|
||||||
|
|
||||||
|
self.create_socket(zmq_context, zmq.PUB, port)
|
||||||
|
|
||||||
|
self._face_service = None
|
||||||
|
self._memory_service = None
|
||||||
|
|
||||||
|
self._face_thread = None
|
||||||
|
|
||||||
|
def start_face_detection(self):
|
||||||
|
if not state.qi_session:
|
||||||
|
logging.warning("No Qi session available. Face detection not started.")
|
||||||
|
return
|
||||||
|
|
||||||
|
self._face_service = state.qi_session.service("ALFaceDetection")
|
||||||
|
self._memory_service = state.qi_session.service("ALMemory")
|
||||||
|
|
||||||
|
self._face_service.setTrackingEnabled(False)
|
||||||
|
self._face_service.setRecognitionEnabled(False)
|
||||||
|
|
||||||
|
self._face_service.subscribe(
|
||||||
|
"FaceDetectionSender",
|
||||||
|
settings.agent_settings.face_detection_interval,
|
||||||
|
0.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
self._face_thread = threading.Thread(target=self._face_loop)
|
||||||
|
self._face_thread.start()
|
||||||
|
|
||||||
|
logging.info("Face detection started.")
|
||||||
|
|
||||||
|
def _face_loop(self):
|
||||||
|
"""
|
||||||
|
Continuously send face detected to the CB, at the interval set in the
|
||||||
|
``start_face_detection`` method.
|
||||||
|
"""
|
||||||
|
while not state.exit_event.is_set():
|
||||||
|
try:
|
||||||
|
value = self._memory_service.getData("FaceDetected", 0)
|
||||||
|
|
||||||
|
face_present = (
|
||||||
|
value
|
||||||
|
and len(value) > 1
|
||||||
|
and value[1]
|
||||||
|
and value[1][0]
|
||||||
|
and len(value[1][0]) > 0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.socket.send(json.dumps({"face_detected": face_present}).encode("utf-8"))
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Error reading FaceDetected")
|
||||||
|
|
||||||
|
time.sleep(settings.agent_settings.face_detection_interval / 1000.0)
|
||||||
|
|
||||||
|
def stop_face_detection(self):
|
||||||
|
try:
|
||||||
|
if self._face_service:
|
||||||
|
self._face_service.unsubscribe("FaceDetectionSender")
|
||||||
|
self._face_service.setTrackingEnabled(False)
|
||||||
|
logging.info("Face detection stopped.")
|
||||||
|
except Exception:
|
||||||
|
logging.warning("Error during face detection cleanup.")
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
super(FaceDetectionSender, self).close()
|
||||||
|
self.stop_face_detection()
|
||||||
@@ -4,6 +4,7 @@ from robot_interface.endpoints.receiver_base import ReceiverBase
|
|||||||
from robot_interface.state import state
|
from robot_interface.state import state
|
||||||
|
|
||||||
from robot_interface.core.config import settings
|
from robot_interface.core.config import settings
|
||||||
|
from robot_interface.endpoints.face_detector import FaceDetectionSender
|
||||||
|
|
||||||
|
|
||||||
class MainReceiver(ReceiverBase):
|
class MainReceiver(ReceiverBase):
|
||||||
@@ -37,6 +38,7 @@ class MainReceiver(ReceiverBase):
|
|||||||
"""
|
"""
|
||||||
return {"endpoint": "ping", "data": message.get("data")}
|
return {"endpoint": "ping", "data": message.get("data")}
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _handle_port_negotiation(message):
|
def _handle_port_negotiation(message):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import zmq
|
import zmq
|
||||||
import threading
|
import threading
|
||||||
import logging
|
import logging
|
||||||
import cv2
|
|
||||||
|
|
||||||
from robot_interface.endpoints.socket_base import SocketBase
|
from robot_interface.endpoints.socket_base import SocketBase
|
||||||
from robot_interface.state import state
|
from robot_interface.state import state
|
||||||
@@ -29,9 +28,7 @@ class VideoSender(SocketBase):
|
|||||||
Will not start if no qi session is available.
|
Will not start if no qi session is available.
|
||||||
"""
|
"""
|
||||||
if not state.qi_session:
|
if not state.qi_session:
|
||||||
logging.info("No Qi session available. Starting video from webcam.")
|
logging.info("No Qi session available. Not starting video loop.")
|
||||||
thread = threading.Thread(target=self.test_video_stream)
|
|
||||||
thread.start()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
video = state.qi_session.service("ALVideoDevice")
|
video = state.qi_session.service("ALVideoDevice")
|
||||||
@@ -62,29 +59,3 @@ class VideoSender(SocketBase):
|
|||||||
self.socket.send(img[settings.video_config.image_buffer])
|
self.socket.send(img[settings.video_config.image_buffer])
|
||||||
except:
|
except:
|
||||||
logging.warn("Failed to retrieve video image from robot.")
|
logging.warn("Failed to retrieve video image from robot.")
|
||||||
|
|
||||||
def test_video_stream(self):
|
|
||||||
"""
|
|
||||||
Test function to send video from local webcam instead of the robot.
|
|
||||||
"""
|
|
||||||
cap = cv2.VideoCapture(0)
|
|
||||||
if not cap.isOpened():
|
|
||||||
logging.error("Could not open webcam for video stream test.")
|
|
||||||
return
|
|
||||||
|
|
||||||
while not state.exit_event.is_set():
|
|
||||||
|
|
||||||
ret, frame = cap.read()
|
|
||||||
if not ret:
|
|
||||||
logging.warning("Failed to read frame from webcam.")
|
|
||||||
continue
|
|
||||||
|
|
||||||
cv2.waitKey(1)
|
|
||||||
|
|
||||||
small_frame = cv2.resize(frame, (320, 240), interpolation=cv2.INTER_AREA)
|
|
||||||
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 70]
|
|
||||||
_, buffer = cv2.imencode('.jpg', small_frame, encode_param)
|
|
||||||
|
|
||||||
self.socket.send(buffer.tobytes())
|
|
||||||
|
|
||||||
cap.release()
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ from robot_interface.endpoints.video_sender import VideoSender
|
|||||||
from robot_interface.state import state
|
from robot_interface.state import state
|
||||||
from robot_interface.core.config import settings
|
from robot_interface.core.config import settings
|
||||||
from robot_interface.utils.timeblock import TimeBlock
|
from robot_interface.utils.timeblock import TimeBlock
|
||||||
|
from robot_interface.endpoints.face_detector import FaceDetectionSender
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main_loop(context):
|
def main_loop(context):
|
||||||
@@ -35,6 +37,12 @@ def main_loop(context):
|
|||||||
video_sender.start_video_rcv()
|
video_sender.start_video_rcv()
|
||||||
audio_sender.start()
|
audio_sender.start()
|
||||||
|
|
||||||
|
# --- Face detection sender ---
|
||||||
|
face_sender = FaceDetectionSender(context)
|
||||||
|
state.sockets.append(face_sender)
|
||||||
|
face_sender.start_face_detection()
|
||||||
|
|
||||||
|
|
||||||
# Sockets that can run on the main thread. These sockets' endpoints should not block for long (say 50 ms at most).
|
# Sockets that can run on the main thread. These sockets' endpoints should not block for long (say 50 ms at most).
|
||||||
receivers = [main_receiver, actuation_receiver]
|
receivers = [main_receiver, actuation_receiver]
|
||||||
|
|
||||||
|
|||||||
175
test/unit/test_face_detection_sender.py
Normal file
175
test/unit/test_face_detection_sender.py
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
This program has been developed by students from the bachelor Computer Science at Utrecht
|
||||||
|
University within the Software Project course.
|
||||||
|
© Copyright Utrecht University (Department of Information and Computing Sciences)
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import json
|
||||||
|
import mock
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from robot_interface.endpoints.face_detector import FaceDetectionSender
|
||||||
|
from robot_interface.state import state
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def initialized_state(monkeypatch):
|
||||||
|
"""
|
||||||
|
Fully initialize global state so __getattribute__ allows access.
|
||||||
|
"""
|
||||||
|
# Bypass the initialization guard
|
||||||
|
monkeypatch.setattr(state, "is_initialized", True, raising=False)
|
||||||
|
|
||||||
|
# Install a controllable exit_event
|
||||||
|
exit_event = mock.Mock()
|
||||||
|
exit_event.is_set = mock.Mock(return_value=True)
|
||||||
|
monkeypatch.setattr(state, "exit_event", exit_event, raising=False)
|
||||||
|
|
||||||
|
# Default qi_session is None unless overridden
|
||||||
|
monkeypatch.setattr(state, "qi_session", None, raising=False)
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_face_detection_no_qi_session():
|
||||||
|
"""
|
||||||
|
Returns early when qi_session is None.
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
sender.start_face_detection()
|
||||||
|
|
||||||
|
assert sender._face_thread is None
|
||||||
|
assert sender._face_service is None
|
||||||
|
assert sender._memory_service is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_face_detection_happy_path(mocker):
|
||||||
|
"""
|
||||||
|
Initializes services and starts background thread.
|
||||||
|
"""
|
||||||
|
mock_face = mock.Mock()
|
||||||
|
mock_memory = mock.Mock()
|
||||||
|
|
||||||
|
mock_qi = mock.Mock()
|
||||||
|
mock_qi.service.side_effect = lambda name: {
|
||||||
|
"ALFaceDetection": mock_face,
|
||||||
|
"ALMemory": mock_memory,
|
||||||
|
}[name]
|
||||||
|
|
||||||
|
state.qi_session = mock_qi
|
||||||
|
|
||||||
|
fake_thread = mock.Mock()
|
||||||
|
mocker.patch("threading.Thread", return_value=fake_thread)
|
||||||
|
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
sender.start_face_detection()
|
||||||
|
|
||||||
|
mock_face.setTrackingEnabled.assert_called_with(False)
|
||||||
|
mock_face.setRecognitionEnabled.assert_called_with(False)
|
||||||
|
mock_face.subscribe.assert_called_once()
|
||||||
|
fake_thread.start.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_face_loop_face_detected_true(mocker):
|
||||||
|
"""
|
||||||
|
Sends face_detected=True when face data exists.
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
|
||||||
|
sender._memory_service = mock.Mock()
|
||||||
|
sender._memory_service.getData.return_value = [0, [[1]]]
|
||||||
|
sender.socket = mock.Mock()
|
||||||
|
|
||||||
|
mocker.patch("time.sleep")
|
||||||
|
state.exit_event.is_set.side_effect = [False, True]
|
||||||
|
|
||||||
|
sender._face_loop()
|
||||||
|
|
||||||
|
sent = sender.socket.send.call_args[0][0]
|
||||||
|
payload = json.loads(sent.decode("utf-8"))
|
||||||
|
|
||||||
|
assert payload["face_detected"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_face_loop_face_detected_false(mocker):
|
||||||
|
"""
|
||||||
|
Sends face_detected=False when no face data exists.
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
|
||||||
|
sender._memory_service = mock.Mock()
|
||||||
|
sender._memory_service.getData.return_value = []
|
||||||
|
sender.socket = mock.Mock()
|
||||||
|
|
||||||
|
mocker.patch("time.sleep")
|
||||||
|
state.exit_event.is_set.side_effect = [False, True]
|
||||||
|
|
||||||
|
sender._face_loop()
|
||||||
|
|
||||||
|
sent = sender.socket.send.call_args[0][0]
|
||||||
|
payload = json.loads(sent.decode("utf-8"))
|
||||||
|
|
||||||
|
assert not payload["face_detected"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_face_loop_handles_exception(mocker):
|
||||||
|
"""
|
||||||
|
Exceptions inside loop are swallowed.
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
|
||||||
|
sender._memory_service = mock.Mock()
|
||||||
|
sender._memory_service.getData.side_effect = Exception("boom")
|
||||||
|
sender.socket = mock.Mock()
|
||||||
|
|
||||||
|
mocker.patch("time.sleep")
|
||||||
|
state.exit_event.is_set.side_effect = [False, True]
|
||||||
|
|
||||||
|
# Must not raise
|
||||||
|
sender._face_loop()
|
||||||
|
|
||||||
|
|
||||||
|
def test_stop_face_detection_happy_path():
|
||||||
|
"""
|
||||||
|
Unsubscribes and disables tracking.
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
|
||||||
|
mock_face = mock.Mock()
|
||||||
|
sender._face_service = mock_face
|
||||||
|
|
||||||
|
sender.stop_face_detection()
|
||||||
|
|
||||||
|
mock_face.unsubscribe.assert_called_once()
|
||||||
|
mock_face.setTrackingEnabled.assert_called_with(False)
|
||||||
|
|
||||||
|
|
||||||
|
def test_stop_face_detection_exception():
|
||||||
|
"""
|
||||||
|
stop_face_detection swallows service exceptions.
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
|
||||||
|
mock_face = mock.Mock()
|
||||||
|
mock_face.unsubscribe.side_effect = Exception("fail")
|
||||||
|
sender._face_service = mock_face
|
||||||
|
|
||||||
|
sender.stop_face_detection()
|
||||||
|
|
||||||
|
|
||||||
|
def test_close_calls_stop_face_detection(mocker):
|
||||||
|
"""
|
||||||
|
close() calls parent close and stop_face_detection().
|
||||||
|
"""
|
||||||
|
sender = FaceDetectionSender(mock.Mock())
|
||||||
|
|
||||||
|
mocker.patch.object(sender, "stop_face_detection")
|
||||||
|
mocker.patch(
|
||||||
|
"robot_interface.endpoints.face_detector.SocketBase.close"
|
||||||
|
)
|
||||||
|
|
||||||
|
sender.close()
|
||||||
|
|
||||||
|
sender.stop_face_detection.assert_called_once()
|
||||||
@@ -55,6 +55,9 @@ class DummySender:
|
|||||||
def start(self):
|
def start(self):
|
||||||
self.called = True
|
self.called = True
|
||||||
|
|
||||||
|
def start_face_detection(self):
|
||||||
|
self.called = True
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -108,11 +111,13 @@ def patched_main_components(monkeypatch, fake_sockets, fake_poll):
|
|||||||
fake_act = FakeReceiver(act_sock)
|
fake_act = FakeReceiver(act_sock)
|
||||||
video_sender = DummySender()
|
video_sender = DummySender()
|
||||||
audio_sender = DummySender()
|
audio_sender = DummySender()
|
||||||
|
face_sender = DummySender()
|
||||||
|
|
||||||
monkeypatch.setattr(main_mod, "MainReceiver", lambda ctx: fake_main)
|
monkeypatch.setattr(main_mod, "MainReceiver", lambda ctx: fake_main)
|
||||||
monkeypatch.setattr(main_mod, "ActuationReceiver", lambda ctx: fake_act)
|
monkeypatch.setattr(main_mod, "ActuationReceiver", lambda ctx: fake_act)
|
||||||
monkeypatch.setattr(main_mod, "VideoSender", lambda ctx: video_sender)
|
monkeypatch.setattr(main_mod, "VideoSender", lambda ctx: video_sender)
|
||||||
monkeypatch.setattr(main_mod, "AudioSender", lambda ctx: audio_sender)
|
monkeypatch.setattr(main_mod, "AudioSender", lambda ctx: audio_sender)
|
||||||
|
monkeypatch.setattr(main_mod, "FaceDetectionSender", lambda ctx: face_sender)
|
||||||
|
|
||||||
# Register sockets for the fake poller
|
# Register sockets for the fake poller
|
||||||
fake_poll.registered = {main_sock: zmq.POLLIN, act_sock: zmq.POLLIN}
|
fake_poll.registered = {main_sock: zmq.POLLIN, act_sock: zmq.POLLIN}
|
||||||
|
|||||||
@@ -97,120 +97,3 @@ def test_video_receive_error(zmq_context, mocker):
|
|||||||
sender.video_rcv_loop(mock_video_service, "stream_name")
|
sender.video_rcv_loop(mock_video_service, "stream_name")
|
||||||
|
|
||||||
send_socket.assert_not_called()
|
send_socket.assert_not_called()
|
||||||
|
|
||||||
def test_video_stream_camera_fail(zmq_context, mocker):
|
|
||||||
"""
|
|
||||||
Test that the function logs an error and returns early if
|
|
||||||
the webcam cannot be opened.
|
|
||||||
"""
|
|
||||||
_patch_basics(mocker)
|
|
||||||
|
|
||||||
# Mock cv2 and logging
|
|
||||||
mock_cv2 = mocker.patch("robot_interface.endpoints.video_sender.cv2")
|
|
||||||
mock_logging = mocker.patch("robot_interface.endpoints.video_sender.logging")
|
|
||||||
|
|
||||||
# Setup the mock capture to fail isOpened()
|
|
||||||
mock_cap = mock.Mock()
|
|
||||||
mock_cap.isOpened.return_value = False
|
|
||||||
mock_cv2.VideoCapture.return_value = mock_cap
|
|
||||||
|
|
||||||
sender = VideoSender(zmq_context)
|
|
||||||
sender.test_video_stream()
|
|
||||||
|
|
||||||
# Assertions
|
|
||||||
mock_cv2.VideoCapture.assert_called_with(0)
|
|
||||||
|
|
||||||
# Ensure the loop was never entered and cleanup didn't happen
|
|
||||||
assert not mock_cap.read.called
|
|
||||||
assert not mock_cap.release.called
|
|
||||||
|
|
||||||
|
|
||||||
def test_video_stream_read_fail(zmq_context, mocker):
|
|
||||||
"""
|
|
||||||
Test that the function logs a warning and continues the loop
|
|
||||||
if a specific frame fails to read.
|
|
||||||
"""
|
|
||||||
_patch_basics(mocker)
|
|
||||||
_patch_exit_event(mocker) # Run loop exactly once
|
|
||||||
|
|
||||||
mock_cv2 = mocker.patch("robot_interface.endpoints.video_sender.cv2")
|
|
||||||
mock_logging = mocker.patch("robot_interface.endpoints.video_sender.logging")
|
|
||||||
|
|
||||||
# Setup capture to open successfully, but fail the read()
|
|
||||||
mock_cap = mock.Mock()
|
|
||||||
mock_cap.isOpened.return_value = True
|
|
||||||
# Return (False, None) simulating a failed frame read
|
|
||||||
mock_cap.read.return_value = (False, None)
|
|
||||||
mock_cv2.VideoCapture.return_value = mock_cap
|
|
||||||
|
|
||||||
sender = VideoSender(zmq_context)
|
|
||||||
# Mock the socket to ensure nothing is sent
|
|
||||||
sender.socket = mock.Mock()
|
|
||||||
|
|
||||||
sender.test_video_stream()
|
|
||||||
|
|
||||||
# Ensure we skipped the processing steps
|
|
||||||
assert not mock_cv2.resize.called
|
|
||||||
assert not sender.socket.send.called
|
|
||||||
|
|
||||||
# Ensure cleanup happened at the end
|
|
||||||
mock_cap.release.assert_called_once()
|
|
||||||
|
|
||||||
|
|
||||||
def test_video_stream_success(zmq_context, mocker):
|
|
||||||
"""
|
|
||||||
Test the happy path: Frame read -> Resize -> Encode -> Send.
|
|
||||||
"""
|
|
||||||
_patch_basics(mocker)
|
|
||||||
_patch_exit_event(mocker) # Run loop exactly once
|
|
||||||
|
|
||||||
mock_cv2 = mocker.patch("robot_interface.endpoints.video_sender.cv2")
|
|
||||||
|
|
||||||
# Setup constants usually found in cv2
|
|
||||||
mock_cv2.IMWRITE_JPEG_QUALITY = 1
|
|
||||||
mock_cv2.INTER_AREA = 2
|
|
||||||
|
|
||||||
# Setup capture to work perfectly
|
|
||||||
mock_cap = mock.Mock()
|
|
||||||
mock_cap.isOpened.return_value = True
|
|
||||||
fake_frame = "original_frame_data"
|
|
||||||
mock_cap.read.return_value = (True, fake_frame)
|
|
||||||
mock_cv2.VideoCapture.return_value = mock_cap
|
|
||||||
|
|
||||||
# Setup Resize and Encode
|
|
||||||
mock_cv2.resize.return_value = "small_frame_data"
|
|
||||||
|
|
||||||
# Mock buffer behavior
|
|
||||||
mock_buffer = mock.Mock()
|
|
||||||
mock_buffer.tobytes.return_value = b"encoded_bytes"
|
|
||||||
# imencode returns (retval, buffer)
|
|
||||||
mock_cv2.imencode.return_value = (True, mock_buffer)
|
|
||||||
|
|
||||||
sender = VideoSender(zmq_context)
|
|
||||||
sender.socket = mock.Mock()
|
|
||||||
|
|
||||||
sender.test_video_stream()
|
|
||||||
|
|
||||||
# Assertions
|
|
||||||
# 1. Check waitKey (the 1ms delay)
|
|
||||||
mock_cv2.waitKey.assert_called_with(1)
|
|
||||||
|
|
||||||
# 2. Check Resize logic
|
|
||||||
mock_cv2.resize.assert_called_with(
|
|
||||||
fake_frame,
|
|
||||||
(320, 240),
|
|
||||||
interpolation=mock_cv2.INTER_AREA
|
|
||||||
)
|
|
||||||
|
|
||||||
# 3. Check Encode logic
|
|
||||||
mock_cv2.imencode.assert_called_with(
|
|
||||||
'.jpg',
|
|
||||||
"small_frame_data",
|
|
||||||
[mock_cv2.IMWRITE_JPEG_QUALITY, 70]
|
|
||||||
)
|
|
||||||
|
|
||||||
# 4. Check Socket Send
|
|
||||||
sender.socket.send.assert_called_with(b"encoded_bytes")
|
|
||||||
|
|
||||||
# 5. Check Cleanup
|
|
||||||
mock_cap.release.assert_called_once()
|
|
||||||
Reference in New Issue
Block a user