CameraServer的应用

从 CameraServer抓取帧

WPILibPi图像带有所有必要的库,以构成您自己的视觉处理系统。为了从摄像机获取当前帧,可以使用CameraServer库。有关CameraServer的信息,请参阅:docs / software / vision-processing / introduction / cameraserver-class:读取和处理视频:CameraServer类。

from cscore import CameraServer
import cv2
import numpy as np
CameraServer.enableLogging()
camera = CameraServer.startAutomaticCapture()
camera.setResolution(width, height)
sink = CameraServer.getVideo()
input_img = np.zeros(shape=(height, width, 3), dtype=np.uint8)
while True:
   time, input_img = sink.grabFrame(input_img)
   if time == 0: # There is an error
      continue

备注

由于历史原因,OpenCV在图像中被读为**BGR**,而不是**RGB**。如果你想把它转化为RGB,需要使用“cv2.cvtColor”。

下面是一个图片的例子,可以从CameraServer抓取。

2020 game target with the retroreflective tape illuminated green.

向CameraServer发送帧

有时,你可能希望将处理过的视频帧发送回CameraServer实例以进行调试,或者在类似Shuffleboard这样的仪表板应用程序中进行查看。

#
# CameraServer initialization code here
#
output = CameraServer.putVideo("Name", width, height)
input_img = np.zeros(shape=(height, width, 3), dtype=np.uint8)
while True:
   time, input_img = sink.grabFrame(input_img)
   if time == 0: # There is an error
      output.notifyError(sink.getError())
      continue
   #
   # Insert processing code here
   #
   output.putFrame(processed_img)

例如,处理代码可以用红色勾勒出目标,并以黄色显示角,以便调试。

下面是一个完整处理图像的示例,该图像将被发送回CameraServer并显示在驱动站计算机上。

Image above with the target outlined in red by the algorithm.