Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Integrating OpenCV

Here are resources and additional documentation about OpenCV and troubleshooting camera permissions:

00:00 Integrating OpenCV With PySimpleGUI. Computer vision is a hot topic. Python allows you to get into computer vision using the opencv-python package, which is a wrapper around the popular OpenCV application.

00:17 If you’re interested in learning more about computer vision, then check out Face Recognition with Python, in Under 25 Lines of Code. PySimpleGUI features straightforward integration with the OpenCV library.

00:30 However, you’ll first need to install OpenCV using pip.

00:39 Now that you have OpenCV installed, you can write the application. You’re going to see another PySimpleGUI demo that uses OpenCV and your computer’s webcam.

00:50 This application lets you apply some common filters to your video in real time. There’s quite a lot of code in this example, and rather than present all of the code in one go, you’ll see how it’s built up step by step, starting out with a display of the live video using OpenCV and then adding GUI elements and functionality one at a time.

01:10 This will allow you to understand how the different parts of the code relate to each other and will also mimic the process of development more closely. It’s often the case that you’ll start out with the bare bones and then add features and functions as you progress.

01:24 Create a filename psg_opencv.py and then add the following code.

01:34 Lines 3 through 5 are the imports for the Python libraries that you’ll need. The code in this example is different from the others you’ve seen because it will be contained in a main() function.

01:46 This type of function is used as the main entry point of the program. To learn more on this topic, check out Defining Main Functions in Python. The PySimpleGUI theme is set on line 8.

02:02 The next step, starting on line 11, is to create a layout for all the elements in the GUI. The first element is a Text() element.

02:18 Next comes an Image() element, where you set the identifier key to "-IMAGE-". This will allow the event loop to replace the contents with the images from OpenCV.

02:29 Finally in this initial layout, a Button() element is created to allow exiting the program. This initial layout is passed to the Window() so that you will be able to see your UI onscreen.

02:50 Next, cv2.VideoCapture() is used to access the webcam on the machine. The final part of this initial stage of the code is to create an event loop, initially a basic one, which will check if the user wants to leave the program.

03:10 And then after that, there will be code which interfaces with OpenCV to get the image data called frame, and this is encoded to a PNG format and used to update the content of the image in the window.

03:44 Finally, the main() function is called using the code seen at the bottom. It’s good practice to check if your Python script is being run directly in this manner, as it avoids issues if you ever import it as part of a larger project.

03:59 When you run the code, you may see a pop-up asking for permission to use the camera. If you do, then you’ll need to grant permission or the code won’t work. Note that you may see different results if you run the code from your editor’s built-in shell, so if you do, try running it directly in a terminal which is using the appropriate virtual environment. See the notes accompanying this video for more details on this.

04:23 Here you can see the program working and the Exit button working as intended. With the basics of the program in place, in the next video, you’ll see how to add image processing and PySimpleGUI controls for that processing.

Benjamin Orion Sweetnam on Nov. 12, 2021

One thing to note. If you don’t have a camera activated or plugged in at all the script will just borke at you about !image.empty().

Become a Member to join the conversation.