KingKong и OAK: использование и программирование
Работа с OAK и libcamera
Инициализация камеры через libcamera
В этом разделе — как с помощью libcamera инициализировать и настроить камеру и получить данные изображения.
# initialize libcamera
picam2 = Picamera2()
capture_config = picam2.create_still_configuration()
# set camera configuration
picam2.configure(picam2.create_preview_configuration(main={"size": (1920, 1200)}))
# Turn on the camera
picam2.start()
Создание конвейера OAK
Далее создадим конвейер (pipeline) OAK, который свяжет камеру с моделью нейросети.
Сначала укажите путь к модели нейросети, затем создайте конвейер и задайте его узлы и связи между ними.
# Neural network model path
parentDir = Path(__file__).parent
nnPath = str((parentDir / Path('./models/mobilenet-ssd_openvino_2021.4_8shave.blob')).resolve().absolute())
# Create pipeline
pipeline = dai.Pipeline()
# Define sources and outputs
# Create neural network node
nn = pipeline.create(dai.node.MobileNetDetectionNetwork)
# Create image output node
xinFrame = pipeline.create(dai.node.XLinkIn)
# Create output node
nnOut = pipeline.create(dai.node.XLinkOut)
xinFrame.setStreamName("inFrame")
nnOut.setStreamName("nn")
# Properties
nn.setConfidenceThreshold(0.5)
# Set neural network model path
nn.setBlobPath(nnPath)
nn.setNumInferenceThreads(2)
nn.input.setBlocking(False)
# Linking
xinFrame.out.link(nn.input)
nn.out.link(nnOut.input)
Получение и вывод изображения через libcamera
Здесь кадр берётся из libcamera, приводится к размеру, который нужен модели OAK, и передаётся во входной поток конвейера OAK.
frame = picam2.capture_array()
img = dai.ImgFrame()
img.setData(to_planar(frame, (300, 300)))
img.setTimestamp(monotonic())
img.setWidth(300)
img.setHeight(300)
qIn.send(img)
Когда нейросеть OAK закончит обработку, результаты детекции забираются из выходного потока конвейера OAK.
inDet = qDet.tryGet()
if inDet is not None:
detections = inDet.detections
Наконец, результаты детекции из выходного потока OAK наносятся на изображение и выводятся на экран.
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
for detection in detections:
# print((detection.xmin, detection.ymin, detection.xmax, detection.ymax))
bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax))
cv2.putText(frame, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2)
temp = device.getChipTemperature()
print("average: {}, css: {}, dss: {}, mss: {}, upa: {}".format(temp.average, temp.css, temp.dss, temp.mss, temp.upa))
# cv2.putText(frame, f"{device.getChipTemperature()}")
# Show the frame
cv2.imshow(name, frame)
Блок-схема

Справочник по API OAK
Подробные руководства по OAK — в документации Luxonis по API DepthAI.