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 ‘q’ from your keyboard.

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

Prerequisites

Have Python installed in Windows (or Unix)
Pyhton version and Packages
Here I am using Python 3.6.6 version

Preparing your 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”.

python

Example Implementation

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 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' key is pressed, stop the loop
	if key == ord("q"):
		break
 
# close all windows
cv2.destroyAllWindows()

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:

see yourself in webcam using Python

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 q from your keyboard.

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

That’s all. Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *