我正在尝试使用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
)发布于 2021-03-28 21:55:29
OpenCV使用BGR。pyvirtualcam默认接受RGB,但也支持BGR等。
下面的代码应该可以工作:
fmt = pyvirtualcam.PixelFormat.BGR
with pyvirtualcam.Camera(1280, 720, 20, fmt=fmt) as camera:另请参阅webcam_filter.py示例,它与您尝试执行的操作类似。
https://stackoverflow.com/questions/66774431
复制相似问题