1 Commits

Author SHA1 Message Date
Storm
19b7efec05 chore: implemented test video function
If there is no qi session, the webcam of the device is used to send video.
2026-01-22 11:36:34 +01:00
38 changed files with 156 additions and 300 deletions

View File

@@ -7,3 +7,4 @@ sphinx
sphinx_rtd_theme sphinx_rtd_theme
pre-commit pre-commit
python-dotenv python-dotenv
opencv-python==4.1.2.30

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,10 +1,3 @@
# -*- 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 from __future__ import unicode_literals
from robot_interface.utils.get_config import get_config from robot_interface.utils.get_config import get_config
@@ -68,7 +61,7 @@ class VideoConfig(object):
): ):
self.camera_index = get_config(camera_index, "VIDEO__CAMERA_INDEX", 0, int) self.camera_index = get_config(camera_index, "VIDEO__CAMERA_INDEX", 0, int)
self.resolution = get_config(resolution, "VIDEO__RESOLUTION", 2, int) self.resolution = get_config(resolution, "VIDEO__RESOLUTION", 2, int)
self.color_space = get_config(color_space, "VIDEO__COLOR_SPACE", 13, int) self.color_space = get_config(color_space, "VIDEO__COLOR_SPACE", 11, int)
self.fps = get_config(fps, "VIDEO__FPS", 15, int) self.fps = get_config(fps, "VIDEO__FPS", 15, int)
self.stream_name = get_config(stream_name, "VIDEO__STREAM_NAME", "Pepper Video") self.stream_name = get_config(stream_name, "VIDEO__STREAM_NAME", "Pepper Video")
self.image_buffer = get_config(image_buffer, "VIDEO__IMAGE_BUFFER", 6, int) self.image_buffer = get_config(image_buffer, "VIDEO__IMAGE_BUFFER", 6, int)

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,10 +1,3 @@
# -*- 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 # So that we can log texts with Unicode characters from __future__ import unicode_literals # So that we can log texts with Unicode characters
import logging import logging
from threading import Thread from threading import Thread

View File

@@ -1,10 +1,3 @@
# -*- 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 # So that `logging` can use Unicode characters in names from __future__ import unicode_literals # So that `logging` can use Unicode characters in names
import threading import threading
import logging import logging

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
class GestureTags: class GestureTags:
tags = ["above", "affirmative", "afford", "agitated", "all", "allright", "alright", "any", tags = ["above", "affirmative", "afford", "agitated", "all", "allright", "alright", "any",
"assuage", "assuage", "attemper", "back", "bashful", "beg", "beseech", "blank", "assuage", "assuage", "attemper", "back", "bashful", "beg", "beseech", "blank",

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import zmq import zmq
from robot_interface.endpoints.receiver_base import ReceiverBase from robot_interface.endpoints.receiver_base import ReceiverBase

View File

@@ -1,10 +1,3 @@
# -*- 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 abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from robot_interface.endpoints.socket_base import SocketBase from robot_interface.endpoints.socket_base import SocketBase

View File

@@ -1,10 +1,3 @@
# -*- 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 abc import ABCMeta from abc import ABCMeta
import zmq import zmq

View File

@@ -1,15 +1,7 @@
# -*- 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)
"""
import zmq import zmq
import threading import threading
import logging import logging
import cv2
import struct
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
@@ -28,7 +20,7 @@ class VideoSender(SocketBase):
""" """
def __init__(self, zmq_context, port=settings.agent_settings.video_sender_port): def __init__(self, zmq_context, port=settings.agent_settings.video_sender_port):
super(VideoSender, self).__init__("video") super(VideoSender, self).__init__("video")
self.create_socket(zmq_context, zmq.PUB, port, [(zmq.SNDHWM,3)]) self.create_socket(zmq_context, zmq.PUB, port, [(zmq.CONFLATE,1)])
def start_video_rcv(self): def start_video_rcv(self):
""" """
@@ -37,7 +29,9 @@ 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. Not starting video loop.") logging.info("No Qi session available. Starting video from webcam.")
thread = threading.Thread(target=self.test_video_stream)
thread.start()
return return
video = state.qi_session.service("ALVideoDevice") video = state.qi_session.service("ALVideoDevice")
@@ -61,23 +55,36 @@ class VideoSender(SocketBase):
:param vid_stream_name: The name of a camera subscription on the video service object vid_service :param vid_stream_name: The name of a camera subscription on the video service object vid_service
:type vid_stream_name: str :type vid_stream_name: str
""" """
try: while not state.exit_event.is_set():
while not state.exit_event.is_set(): try:
try: img = vid_service.getImageRemote(vid_stream_name)
img = vid_service.getImageRemote(vid_stream_name) #Possibly limit images sent if queuing issues arise
if img is not None: self.socket.send(img[settings.video_config.image_buffer])
raw_data = img[6] except:
width = img[0] logging.warn("Failed to retrieve video image from robot.")
height = img[1]
width_bytes = struct.pack('<I', width) def test_video_stream(self):
height_bytes = struct.pack('<I', height) """
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
self.socket.send_multipart([width_bytes, height_bytes, raw_data]) while not state.exit_event.is_set():
except KeyboardInterrupt:
logging.info("Video receiving loop interrupted by user.") ret, frame = cap.read()
except: if not ret:
logging.warn("Failed to retrieve video image from robot.") logging.warning("Failed to read frame from webcam.")
finally: continue
vid_service.unsubscribe(vid_stream_name)
logging.info("Unsubscribed from video stream.") 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()

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import logging import logging
from robot_interface.endpoints.audio_sender import AudioSender from robot_interface.endpoints.audio_sender import AudioSender

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import logging import logging
import signal import signal
import threading import threading

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv

View File

@@ -1,10 +1,3 @@
# -*- 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 # So that `print` can print Unicode characters in names from __future__ import unicode_literals # So that `print` can print Unicode characters in names
import logging import logging
import sys import sys

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import logging import logging
import sys import sys

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import time import time

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,10 +1,3 @@
# -*- 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 # So that we can format strings with Unicode characters from __future__ import unicode_literals # So that we can format strings with Unicode characters
import random import random
import sys import sys

View File

@@ -1,10 +1,3 @@
# -*- 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 mock import patch, MagicMock from mock import patch, MagicMock
import pytest import pytest

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,10 +1,3 @@
# -*- 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 mock import patch, mock from mock import patch, mock
from robot_interface.core.config import Settings from robot_interface.core.config import Settings

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import pyaudio import pyaudio
import pytest import pytest

View File

@@ -1,6 +0,0 @@
# -*- 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)
"""

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import sys import sys
import mock import mock

View File

@@ -1,10 +1,4 @@
# -*- coding: utf-8 -*- # 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)
"""
import os import os
import mock import mock

View File

@@ -1,10 +1,3 @@
# -*- 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 robot_interface.utils.get_config import get_config from robot_interface.utils.get_config import get_config

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import pytest import pytest
import threading import threading
import zmq import zmq

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import mock import mock
import pytest import pytest
import zmq import zmq

View File

@@ -1,10 +1,4 @@
# -*- coding: utf-8 -*- # 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)
"""
import mock import mock
import pytest import pytest

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import sys import sys
# Import module under test # Import module under test

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import pytest import pytest
from robot_interface.endpoints.receiver_base import ReceiverBase from robot_interface.endpoints.receiver_base import ReceiverBase

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import mock import mock
import zmq import zmq
from robot_interface.endpoints.socket_base import SocketBase from robot_interface.endpoints.socket_base import SocketBase

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import threading import threading
import signal import signal
import pytest import pytest

View File

@@ -1,10 +1,3 @@
# -*- 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)
"""
import time import time
import mock import mock

View File

@@ -1,11 +1,5 @@
# -*- coding: utf-8 -*- # 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)
"""
import struct
import mock import mock
import pytest import pytest
import zmq import zmq
@@ -54,10 +48,8 @@ def test_video_streaming(zmq_context, mocker):
# Pepper's image buffer lives at index 6 # Pepper's image buffer lives at index 6
mocker.patch.object(settings.video_config, "image_buffer", 6) mocker.patch.object(settings.video_config, "image_buffer", 6)
test_width = 320
test_height = 240
mock_video_service = mock.Mock() mock_video_service = mock.Mock()
mock_video_service.getImageRemote.return_value = [test_width, test_height, None, None, None, None, b"fake_img"] mock_video_service.getImageRemote.return_value = [None]*6 + ["fake_img"]
fake_session = mock.Mock() fake_session = mock.Mock()
fake_session.service.return_value = mock_video_service fake_session.service.return_value = mock_video_service
@@ -71,16 +63,12 @@ def test_video_streaming(zmq_context, mocker):
sender = VideoSender(zmq_context) sender = VideoSender(zmq_context)
send_socket = mock.Mock() send_socket = mock.Mock()
sender.socket.send_multipart = send_socket sender.socket.send = send_socket
sender.start_video_rcv() sender.start_video_rcv()
sender.video_rcv_loop(mock_video_service, "stream_name") sender.video_rcv_loop(mock_video_service, "stream_name")
send_socket.assert_called_with([ send_socket.assert_called_with("fake_img")
struct.pack('<I', 320),
struct.pack('<I', 240),
b"fake_img"
])
def test_video_receive_error(zmq_context, mocker): def test_video_receive_error(zmq_context, mocker):
@@ -103,30 +91,126 @@ def test_video_receive_error(zmq_context, mocker):
sender = VideoSender(zmq_context) sender = VideoSender(zmq_context)
send_socket = mock.Mock() send_socket = mock.Mock()
sender.socket.send_multipart = send_socket sender.socket.send = send_socket
sender.start_video_rcv() sender.start_video_rcv()
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_loop_keyboard_interrupt(zmq_context, mocker): def test_video_stream_camera_fail(zmq_context, mocker):
"""Video loop should handle KeyboardInterrupt gracefully and unsubscribe.""" """
Test that the function logs an error and returns early if
the webcam cannot be opened.
"""
_patch_basics(mocker) _patch_basics(mocker)
_patch_exit_event(mocker)
# We mock the video service to raise KeyboardInterrupt when accessed # Mock cv2 and logging
mock_video_service = mock.Mock() mock_cv2 = mocker.patch("robot_interface.endpoints.video_sender.cv2")
mock_video_service.getImageRemote.side_effect = KeyboardInterrupt mock_logging = mocker.patch("robot_interface.endpoints.video_sender.logging")
# Mock logging to verify the specific interrupt message is logged # Setup the mock capture to fail isOpened()
mock_logger = mocker.patch("robot_interface.endpoints.video_sender.logging") mock_cap = mock.Mock()
mock_cap.isOpened.return_value = False
mock_cv2.VideoCapture.return_value = mock_cap
sender = VideoSender(zmq_context) sender = VideoSender(zmq_context)
sender.test_video_stream()
# Execute the loop
sender.video_rcv_loop(mock_video_service, "stream_name")
# Verify the 'finally' block executed (unsubscribe) # Assertions
mock_video_service.unsubscribe.assert_called_with("stream_name") mock_cv2.VideoCapture.assert_called_with(0)
mock_logger.info.assert_any_call("Unsubscribed from video stream.")
# 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()