OpenCV_TotalAccess: Instantly Watch and Label All Cameras Attached to a Computer Using this Python Code

George Davila Durendal
5 min readMay 18, 2021

Probably the most James Bond OpenCV script that you’ll ever use, although that’s not really saying much… @GDurendal

Photo by arvin keynes on Unsplash

Do you know that moment in movies where some logs on to a computer and instantly accesses all the security cameras? That’s basically what this software does. Although, for a flexible piece of software than can instantly access and display all permissioned cameras on a given device — invocative of that moment in spy movies where they hack into all the cameras — it is almost shockingly boring in its actual execution.

The Problem

A consistent practical problem in computer vision is the simple problem of camera selection. I don’t mean choosing between an OAK ai-enabled camera or an infrared camera. No, I mean the simple process of selecting between a default front camera and a rear camera. It’s a common enough problem with 2 or 3 cameras. When we have a dozen or more showing multiple rooms and we have to select only cameras 4, 7, and 9 it becomes an even greater problem.

Now this isn’t too much of a problem for even the most novice computer vision engineer. Looking at the common function cv2.VideoCapture(0) we can recognize it as simply a matter of choosing the right indices. It might take a minute to search over those indices, but no big deal.

But when it comes to actual deployment this simple problem can become an unexpected nightmare. The lay user likely doesn’t know how to change their default webcam or doesn’t care to. Even if they can do that, it is unlikely that they will be able to easily find and enter the indices for a given few cameras out of dozens. Not to mention the fact that a common user may do something to alter an originally working configuration, thus necessitating a follow-up customer service interaction in just a few months time.

There is also the matter of computational efficiency. In modern computer vision — where we might run neural network inference on any given video feed — we do not want excess cameras around eating up valuable CPU and GPU resources.

Thus, for reasons of usability and computational viability we must give the user an easy way to choose what camera(s) they want to use. That is where this code comes in.

The Code

All of the code is made available on my GitHub:

Download the directory called OpenCV_TotalAccess. Assuming that you already have Python 3.6+ you only need to run the following in console:

pip install -r requirements.txt

python showAllCameras.py

The only requirements are OpenCV and Numpy. Should work with most versions of these so if you already have these libraries feel free to skip the pip install. This python script will show you all the camera feeds OpenCV can find and access on your device. Here is an example:

Photo by Author. Shows laptop cameras. Camera 0 is the default inner webcam. Camera 1 is the 2nd, rear-facing, camera. Notice the indices are written in red.

Voila! That’s the code that will get you instant eyes on all cameras on the device. If you have 10 cameras you will see 10 feeds here, 100 cameras ad you’ll see 100 and so on. Certain parameters and options can be adjusted in the code to quickly change the display format (see the github repo for that). Now on to the details…

Notice the red text. Those show the indices corresponding to their usage in OpenCV. So if we call cv2.VideoCapture(0) we will get the image on the left and if we call cv2.VideoCapture(1) we will get the image on the right.

The script itself is a bit interesting. cv2.VideoCapture() will not produce an ‘out of range’ error on the index input. So if we try cv2.VideoCapture(5000), a structure which technically calls camera number 5000 (of our 2 cameras) we will not get an error from that function. So, in order to see how many cameras are on an otherwise unknown network, we simply observe that cv2.imshow() will produce an error if it gets an input from the empty camera feed cv2.VideoCapture(5000). We then apply a try/except block on this function. It then builds a list of indices that will only stop building once we run into that first empty feed. We should note that it is probably most generally useful to run the try/except block on this function on this function rather than another. This is because different feeds may have different structures (e.g. different sizes or filetypes) which may interfere with other intermediate functions. At the end of the day we only really care about whether or not a given camera feed can be readily displayed, not if it can be run through some arbitrary function. So we search for the error occurrence in cv2.imshow().

So now both you and the user have an easy way to know which camera you want to use for your particular application. Indeed, this can even be quite the time saver for a computer vision engineer. Easier to run this script than manually search over the indices.

Lets also create an easy way for the totally non-technical user to assign a camera to be used. Inlcuded in the repo is a tkinter-based script, let us run that:

python tkinterEnterIndex.py

Photo by Author. Simple App for selecting the camera you want to use. Here we typed ‘1’ in order to get the camera indexed ‘1’ on the right in the image above.

This app inputs a number into the text file selectedCameraIndex.txt. You can simply input this into a json file or input a list of such numbers according to the specifications of your particular program. I went with a tkinter app here since its exceedingly easy to turn tkinter files into .exe’s through pyinstaller. That exe can be what the common non-technical user interacts with.

After typing ‘1’ in the app as shown above, the input then feeds into the coode in showSelectedCamera.py. Run:

python showSelectedCamera.py

Photo by Author.

As we can see the resulting image is that of my rear laptop camera, exactly as specified (Recall that index ‘1’ corresponds to camera number 2 in OpenCV). Here I ran a grayscale function as a bit of a placeholder, but you can change that function to any that your program uses.

There we have it. An easy and adjustable way for CV engineers and common users alike to properly know and adjust which cameras they might use. Now your user can simply look at which numbers correspond to their hallway, kitchen, etc. and enter those numbers into a straightforward exe.

Told you it was surprisingly boring…

--

--

George Davila Durendal

Diverse American A.I. Engineer + Tech Exec. Stanford Alum. Successful startup founder with 2 exits.