chore: implemented test video function

If there is no qi session, the webcam of the device is used to send video.
This commit is contained in:
Storm
2026-01-22 11:36:34 +01:00
parent da97eb8a1a
commit 19b7efec05
3 changed files with 148 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
import zmq
import threading
import logging
import cv2
from robot_interface.endpoints.socket_base import SocketBase
from robot_interface.state import state
@@ -28,7 +29,9 @@ class VideoSender(SocketBase):
Will not start if no qi session is available.
"""
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
video = state.qi_session.service("ALVideoDevice")
@@ -59,3 +62,29 @@ class VideoSender(SocketBase):
self.socket.send(img[settings.video_config.image_buffer])
except:
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()