robocar-oak-camera/camera/depthai.py

85 lines
3.0 KiB
Python
Raw Normal View History

2022-01-15 17:42:14 +00:00
import logging
import paho.mqtt.client as mqtt
import events.events_pb2
from google.protobuf.timestamp_pb2 import Timestamp
import depthai as dai
2022-01-22 17:13:05 +00:00
from depthai_sdk import getDeviceInfo
2022-01-15 17:42:14 +00:00
import cv2
from threading import Thread
logger = logging.getLogger(__name__)
class FramePublisher(Thread):
def __init__(self, mqtt_client: mqtt.Client, frame_topic: str, img_width: int, img_height: int):
super().__init__(name="FrameProcessor")
self._mqtt_client = mqtt_client
self._frame_topic = frame_topic
self._img_width = img_width
self._img_height = img_height
self._pipeline = self._configure_pipeline()
2022-01-22 17:13:05 +00:00
self._device_info = getDeviceInfo("18443010012F6C1200")
2022-01-15 17:42:14 +00:00
def _configure_pipeline(self) -> dai.Pipeline:
logger.info("configure pipeline")
pipeline = dai.Pipeline()
2022-01-22 17:13:05 +00:00
cam_rgb = pipeline.create(dai.node.ColorCamera)
xout_rgb = pipeline.create(dai.node.XLinkOut)
2022-01-15 17:42:14 +00:00
2022-01-22 17:13:05 +00:00
xout_rgb.setStreamName("rgb")
2022-01-15 17:42:14 +00:00
# Properties
cam_rgb.setBoardSocket(dai.CameraBoardSocket.RGB)
2022-01-22 17:13:05 +00:00
cam_rgb.setPreviewSize(width=self._img_width, height=self._img_height)
cam_rgb.setInterleaved(False)
cam_rgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.RGB)
cam_rgb.setFps(30)
2022-01-15 17:42:14 +00:00
# Linking
2022-01-22 17:13:05 +00:00
cam_rgb.video.link(xout_rgb.input)
2022-01-15 17:42:14 +00:00
logger.info("pipeline configured")
return pipeline
def run(self):
2022-01-22 17:13:05 +00:00
logger.info("device %s", self._device_info)
2022-01-15 17:42:14 +00:00
# Connect to device and start pipeline
with dai.Device(self._pipeline) as device:
2022-01-22 17:13:05 +00:00
logger.info('MxId: %s', device.getDeviceInfo().getMxId())
logger.info('USB speed: %s', device.getUsbSpeed())
logger.info('Connected cameras: %s', device.getConnectedCameras())
logger.info("output queues found: %s",device.getOutputQueueNames())
device.startPipeline()
2022-01-15 17:42:14 +00:00
# Queues
2022-01-22 17:13:05 +00:00
queue_size = 4
q_rgb = device.getOutputQueue("rgb", maxSize=queue_size, blocking=False)
2022-01-15 17:42:14 +00:00
while True:
try:
2022-01-22 17:13:05 +00:00
inRgb = q_rgb.get() # blocking call, will wait until a new data has arrived
im_resize = inRgb.getCvFrame()
is_success, im_buf_arr = cv2.imencode(".jpg", im_resize)
byte_im = im_buf_arr.tobytes()
timestamp = Timestamp()
frame_msg = events.events_pb2.FrameMessage()
frame_msg.id.name = "robocar-oak-camera-oak"
frame_msg.id.id = str(timestamp.ToMilliseconds())
frame_msg.id.created_at.FromMilliseconds(timestamp.ToMilliseconds())
frame_msg.frame = byte_im
self._mqtt_client.publish(topic=self._frame_topic,
payload=frame_msg.SerializeToString(),
qos=0,
retain=False)
2022-01-15 17:42:14 +00:00
except Exception as e:
logger.exception("unexpected error")