I am trying to use OpenCV to detect human faces and draw a rectangle around it, then output it to a virtual camera. I am using pyvirtualcam
However, I do not know how to convert the images from the OpenCV VideoCapture
to the image format that pyvirtualcam
uses. Due to my lack of knowledge, I do not know what format either of them is in, or how to convert one to the other. How do I convert the images from OpenCV to something I can output?
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
)
OpenCV uses BGR. pyvirtualcam by default accepts RGB but also supports BGR and others.
The following should work:
fmt = pyvirtualcam.PixelFormat.BGR
with pyvirtualcam.Camera(1280, 720, 20, fmt=fmt) as camera:
See also the webcam_filter.py sample which is similar to what you try to do.