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.

4
  • Does this answer your question? Capturing video from two cameras in OpenCV at once Commented Sep 11, 2020 at 7: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. Commented Sep 11, 2020 at 7: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 Commented Sep 11, 2020 at 7:28
  • Yes exactly. I have added an image screenshot for further clarity. Commented Sep 11, 2020 at 7:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

sudo modprobe v4l2loopback video_nr=2 exclusive_caps=1 what will be the equivalent terminal code for windows? Is this library supported in windows?
The library is for Linux only. Knowing that you are on windows, maybe this 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 softwarerecs.stackexchange.com/a/74002 .
Hmm, I see... thank you very much for the reference!
@GuilhermeSant'Ana Is there a library like this for java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.