Using OpenCV Output as Webcam

前端 未结 5 2302
忘掉有多难
忘掉有多难 2020-12-14 08:49

So, I want to write a program to make the processed output from OpenCV be seen as a WebCam. I want to use it to create effects for a program like Skype. I am stuck and Googl

5条回答
  •  难免孤独
    2020-12-14 09:25

    I had the same problem: My grandmother hears poorly so I wanted to be able to add subtitles to my Skype video feed. I also wanted to add some effects for laughs. I could not get webcamoid working. Screen capture method (mentioned above) seemed too hacky, and I could not get Skype to detect ffmpegs dummy output camera (guvcview detects though). Then I ran across this:

    https://github.com/jremmons/pyfakewebcam

    It is not C++ but Python. Still, it is fast enough on my non-fancy laptop. It can create multiple dummy webcams (I only need two). It works with Python3 as well. The steps mentioned in readme were easy to reproduce on Ubuntu 18.04. Within 2-3 minutes, the example code was running. At the time of this writing, the given examples there do not use input from a real webcam. So I add my code, which processes the real webcam's input and outputs it to two dummy cameras:

    import cv2
    import time
    import pyfakewebcam
    import numpy as np
    
    IMG_W = 1280
    IMG_H = 720
    
    cam = cv2.VideoCapture(0)
    cam.set(cv2.CAP_PROP_FRAME_WIDTH, IMG_W)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT, IMG_H)
    
    fake1 = pyfakewebcam.FakeWebcam('/dev/video1', IMG_W, IMG_H)
    fake2 = pyfakewebcam.FakeWebcam('/dev/video2', IMG_W, IMG_H)
    
    while True:
        ret, frame = cam.read()
    
        flipped = cv2.flip(frame, 1)
    
        # Mirror effect 
        frame[0 : IMG_H, IMG_W//2 : IMG_W] = flipped[0 : IMG_H, IMG_W//2 : IMG_W]
    
        fake1.schedule_frame(frame)
        fake2.schedule_frame(flipped)
    
        time.sleep(1/15.0)
    

提交回复
热议问题