8

Does anyone know a way to create a camera source using python? So for example I have 2 cameras:

  1. Webcam
  2. USB Cam

Whenever I use any web application or interface which requires camera, I have an option to select a camera source from the above two mentioned.

What I want to achieve is that I am processing real time frames in my python program with a camera portal of my own like this:

import numpy as np
import cv2

    while True:
        _,frame = self.cam.read()
        k = cv2.waitKey(1)
        if k & 0xFF == ord('q'):
            self.cam.release()
            cv2.destroyAllWindows()
            break
        else:
            cv2.imshow("Frame",frame)

Now I want to use this frame as a camera source so next time whenever I open a software or web app which requires camera then it show option as follows:

  1. Webcam
  2. USB Cam
  3. Python Cam (Or whatever be the name)

Does anyone has any advice or hint on how to go about that? I have seen some premium softwares which generate their own camera source but they're written in c++. I was wondering if this could happen in python or not.

Here is an example of the same:

enter image description here

As you can see there are multiple camera source there. I want to add one camera source which displays the frames processed by python in its feed.

Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62
  • Does this answer your question? [Capturing video from two cameras in OpenCV at once](https://stackoverflow.com/questions/29664399/capturing-video-from-two-cameras-in-opencv-at-once) – Karthik Sep 11 '20 at 07:20
  • Hi @Karthik, no that is a completely different issue. I don't want to open two cams from opencv... I want to create my own camera source which would be visible to any webapp or software that needs camera which shows my opencv processed frames. – Shivam Sahil Sep 11 '20 at 07:26
  • Oh I see you looking something like a camera portal of your own which you want to embed somewhere to show your processed frames – Karthik Sep 11 '20 at 07:28
  • Yes exactly. I have added an image screenshot for further clarity. – Shivam Sahil Sep 11 '20 at 07:29

1 Answers1

5

You can use v4l2loopback (https://github.com/umlaeute/v4l2loopback) for that. It is written in C but there are some python wrappers. I've used virtualvideo (https://github.com/Flashs/virtualvideo).
The virtualvideo README is pretty straight forward but here is my modification of their github example to match your goal:

import virtualvideo
import cv2


class MyVideoSource(virtualvideo.VideoSource):
    def __init__(self):
        self.cam = cv2.VideoCapture(0)
        _, img = self.cam.read()
        size = img.shape
        #opencv's shape is y,x,channels
        self._size = (size[1],size[0])

    def img_size(self):
        return self._size

    def fps(self):
        return 30

    def generator(self):
        while True:
            _, img = self.cam.read()
            yield img


vidsrc = MyVideoSource()
fvd = virtualvideo.FakeVideoDevice()
fvd.init_input(vidsrc)
fvd.init_output(2, 640, 480, pix_fmt='yuyv422')
fvd.run()

in init_output the first argument is the virtual camera resource thar i've created when adding the kernel module:

sudo modprobe v4l2loopback video_nr=2 exclusive_caps=1

the last arguments are my webcam size and the pixel format.

You should see this option in hangouts:

hangoutOption

  • `sudo modprobe v4l2loopback video_nr=2 exclusive_caps=1` what will be the equivalent terminal code for windows? Is this library supported in windows? – Shivam Sahil Sep 23 '20 at 05:56
  • The library is for Linux only. Knowing that you are on windows, maybe [this](https://stackoverflow.com/questions/33693131/how-to-create-virtual-webcam-in-windows-10) is what you've been looking for ( i believe it has a similar approach to what i've shown you), but i wouldn't know if python integration is possible. You have other solutions as well that **seems** to not involve creating a kernel module https://softwarerecs.stackexchange.com/a/74002 . – Guilherme Sant'Ana Sep 23 '20 at 12:29
  • Hmm, I see... thank you very much for the reference! – Shivam Sahil Sep 24 '20 at 05:51
  • @GuilhermeSant'Ana Is there a library like this for java – Arsh Coder Apr 26 '22 at 10:29