Introduction

The tutorial, see yourself in webcam using Python 3, will show yourself in front of the webcam as a video motion. In this example we will capture the video in front camera in the same way you see yourself in front of the mirror. Make sure you do have the camera installed in your system in order to see yourself in webcam.

This example won’t show you how to detect face or how to save the image captured into video camera but it will just show your video motion using the webcam in your system.

Here in this example we will use OpenCv and imutils . Video libraries to capture the video and display into a frame. When you wish to close the video window you need to press either ESC key or ‘q’ from your keyboard.

You may be interested to read Real Time Face Detection in Webcam using Python 3

Prerequisites

Python 3.8.1, OpenCV 4.2.0, imutils 0.5.3

Example with Source Code

Preparing Workspace

Preparing your workspace is one of the first things that you can do to make sure that you start off well. The first step is to check your working directory.

When you are working in the Python terminal, you need first navigate to the directory, where your file is located and then start up Python, i.e., you have to make sure that your file is located in the directory where you want to work from.

In the below image you see I have opened a cmd prompt and navigated to the directory where I have to create Python script for implementing the example “see yourself in webcam using Python 3”.

Creating Python Script

Now we will create the Python script and see how you see yourself in webcam using Python 3.

In the below Python script we first import the required modules. Then we grab the reference to the webcam. Note that here I am using Laptop’s built-in webcam.

Next we start the loop to keep the window open for seeing yourself through webcam continuously until you press either ESC key or q from your keyboard.

from imutils.video import VideoStream import cv2 # grab the reference to the webcam vs = VideoStream(src=0).start() # keep looping while True: # grab the current frame frame = vs.read() # if we are viewing a video and we did not grab a frame, # then we have reached the end of the video if frame is None: break # show the frame to our screen cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' or ESC key is pressed, stop the loop if key == ord("q") or key == 27: break # close all windows cv2.destroyAllWindows()

Installing Modules

Note: if you find the required imported modules do not exist in your Python library then you can install the modules by executing the command as shown in the below screen.

Though the below screen shows older version of OpenCV and imutils package but the actual versions are as specified in the Prerequisites section above.

In the above image we see that we have installed the required modules OpenCv as well as imutils for our application.

Testing the Application

Now it’s time to test our application by executing the following command in the cmd prompt:

C:\py_scripts>python <python script name>.py

Once the script gets executed successfully, you will be able to see yourself in the frame. To exit you should press either ESC key or q from your keyboard.

You may be interested to read Real Time Face Detection in Webcam using Python 3

You can download source code.

That’s all. Thanks for reading.