Python OpenCV Tutorial To Capture Images From Webcam Full Project

Python OpenCV Tutorial To Capture Images From Webcam Full Project
x
Python OpenCV Tutorial To Capture Images From Webcam Full Project For Beginners

Welcome folks today in this blog post I will be talking about how to capture images from webcam using opencv library in python full project. So let’s get started. You can see the step by step video for building this application.

 

Requirements

 

pip install opencv

 

 

Full Source Code

 

 

import cv2

cam = cv2.VideoCapture(0)

cv2.namedWindow("test")

img_counter = 0

while True:
    ret, frame = cam.read()
    if not ret:
        print("failed to grab frame")
        break
    cv2.imshow("test", frame)

    k = cv2.waitKey(1)
    if k%256 == 27:
        # ESC pressed
        print("Escape hit, closing...")
        break
    elif k%256 == 32:
        # SPACE pressed
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1

cam.release()

cv2.destroyAllWindows()

 

 

When you press the space key in the application the screenshot will be taken automatically and image will be saved in your local directory and also when you want to exit the application you can press the esc key.

See also  Join 100+ Teenagers Whatsapp Group Invite Links For Teen Friendship

 

 

Leave a Reply