【问题标题】:Output OpenCv to virtual camera in Python在 Python 中将 OpenCv 输出到虚拟相机
【发布时间】:2021-06-20 18:33:26
【问题描述】:

我正在尝试使用 OpenCV 检测人脸并在其周围绘制一个矩形,然后将其输出到虚拟相机。我正在使用pyvirtualcam 但是,我不知道如何将图像从 OpenCV VideoCapture 转换为 pyvirtualcam 使用的图像格式。由于我缺乏知识,我不知道它们中的任何一种是什么格式,或者如何将一种转换为另一种。如何将图像从 OpenCV 转换为我可以输出的东西?

import cv2
import pyvirtualcam
import cvlib as cv


video = cv2.VideoCapture(0)

with pyvirtualcam.Camera(1280, 720, 20) as camera:
    while True:
        ret, im = video.read()

        faces, confidences = cv.detect_face(im)
        for face in faces:
            Rect(face[0], face[1], face[2], face[3]).draw(im)

        camera.send(im)

        camera.sleep_until_next_frame()
import cv2


class Rect:
    def __init__(self, x0, y0, x1, y1):
        self.points = [
            (x0, y0),
            (x1, y1)
        ]
        self.origin = (x0, y0)
        self.width = abs(x1 - x0)
        self.height = abs(y1 - y0)

    def draw(self, im, color=(0, 255, 0), width=3):
        cv2.rectangle(
            im,
            self.points[0],
            self.points[1],
            color,
            width
        )

【问题讨论】:

    标签: python opencv camera


    【解决方案1】:

    OpenCV 使用 BGR。 pyvirtualcam 默认接受 RGB,但也支持 BGR 等。

    以下应该有效:

    fmt = pyvirtualcam.PixelFormat.BGR
    with pyvirtualcam.Camera(1280, 720, 20, fmt=fmt) as camera:
    

    另请参阅webcam_filter.py 示例,该示例与您尝试执行的操作类似。

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2015-10-02
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      相关资源
      最近更新 更多