跳过主要内容

Webcam (摄像头) 算子

输入

  • tick: Dora tick.

输出

  • 图像: 高 x 宽 x BGR array.

配置

使用 cv2 包获取摄像头图像。 摄头号可被使用 CAMERA_INDEX 配置

图描述

  - id: yolov5
operator:
outputs:
- bbox
inputs:
image: webcam/image
python: ../../operators/webcam_op.py

方法

__init__()

源码
    def __init__(self):
self.video_capture = cv2.VideoCapture(int(DEVICE_INDEX))
self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, OUTPUT_WIDTH)
self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, OUTPUT_HEIGHT)


.on_event(...)

源码

def on_event(
self,
dora_event: dict,
send_output: Callable[[str, bytes], None],
) -> DoraStatus:
if dora_event["type"] == "INPUT":
return self.on_input(dora_event, send_output)
return DoraStatus.CONTINUE


.on_input(...)

源码

def on_input(
self,
dora_input: dict,
send_output: Callable[[str, bytes], None],
):
ret, frame = self.video_capture.read()
if ret:
frame = cv2.resize(frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
send_output(
"image",
pa.array(frame.ravel()),
dora_input["metadata"],
)
else:
print("could not get webcam.")
return DoraStatus.CONTINUE