Welcome folks today in this blog post we will be capturing images from webcam and save it as png image in directory using python. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the opencv
library using the pip
command as shown below
pip install opencv-python
After installing this library you need to make an app.py
file and copy paste the following code
app.py
Accessing and Grant Permission to Use Webcam Using OpenCV
First of all in this below code we will showing the webcam of the user as shown below
1 2 |
import cv2 webcam = cv2.VideoCapture(1) |
As you can see we are importing the opencv
module at the very top and then we are starting the user webcam and passing the argument as an integer this simply represents the camera that you want to use. And then we are storing that reference in webcam variable.
Now if you start the python app your webcam will show as shown below
Adding Keyboard Shortcuts
Now guys we will be adding the keyboard shortcuts to this webcam app where we will be binding certain keys on the keyboard so that whenever the user presses those keys then we will execute certain code. Like for example if they press the q key we need to close the camera and exit out from the application.
1 2 3 4 5 6 7 8 9 10 11 |
while True: try: key = cv2.waitKey(1) if key == ord('s'): break elif key == ord('q'): print("Turning off camera.") break except(KeyboardInterrupt): print("Turning off camera.") break |
As you can see we have the while loop for checking the frames of the webcam and which key is pressed by the keyboard and we have two shortcuts that we have binded first will be the s
key if the user presses this then we will be saving the frame as an png image and if they press the q key then we will be closing the webcam and also quitting the application.
Capturing Frames From OpenCV
Now we will be showing how we can capture the frames using the opencv
library for this you need to copy paste the code inside the try section
1 2 3 4 |
check, frame = webcam.read() print(check) #prints true as long as the webcam is running print(frame) #prints matrix values of each framecd cv2.imshow("Capturing", frame) |
As you can see we reading each and every frame from the webcam using the read()
method and then we are printing these information in the command line and then we are showing it using the imshow()
method and here we are passing the actual frame to capture.
Saving the Captured Frames as PNG Image
Now we will be seeing how we can convert the captured frame from the webcam and save it as png image file inside the root directory. For this you need to copy paste the below code. This code will be present when the user presses the s
key on the keyboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
cv2.imwrite(filename='saved_img.jpg', img=frame) webcam.release() img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE) img_new = cv2.imshow("Captured Image", img_new) cv2.waitKey(1650) cv2.destroyAllWindows() print("Processing image...") img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR) print("Converting RGB image to grayscale...") gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY) print("Converted RGB image to grayscale...") print("Resizing image to 28x28 scale...") img_ = cv2.resize(gray,(28,28)) print("Resized...") img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_) print("Image saved!") break |
As you can see we are using the imwrite()
method to save the captured frame as a png image file and inside it we are providing the argument which is saved_img.jpg
and then we are also converting to grayscale image. And then we re resizing the image as well .
Turning off Camera & Exit App
Now we will be turning off the webcam and exit out the application when the user presses the q
key on the keyboard as shown below
1 2 3 4 5 6 |
print("Turning off camera.") webcam.release() print("Camera off.") print("Program ended.") cv2.destroyAllWindows() break |
Full Source Code
Wrapping the blog post this is the full source code of the app.py
file as shown below
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import cv2 key = cv2. waitKey(1) webcam = cv2.VideoCapture(1) while True: try: check, frame = webcam.read() print(check) #prints true as long as the webcam is running print(frame) #prints matrix values of each framecd cv2.imshow("Capturing", frame) key = cv2.waitKey(1) if key == ord('s'): cv2.imwrite(filename='saved_img.jpg', img=frame) webcam.release() img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE) img_new = cv2.imshow("Captured Image", img_new) cv2.waitKey(1650) cv2.destroyAllWindows() print("Processing image...") img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR) print("Converting RGB image to grayscale...") gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY) print("Converted RGB image to grayscale...") print("Resizing image to 28x28 scale...") img_ = cv2.resize(gray,(28,28)) print("Resized...") img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_) print("Image saved!") break elif key == ord('q'): print("Turning off camera.") webcam.release() print("Camera off.") print("Program ended.") cv2.destroyAllWindows() break except(KeyboardInterrupt): print("Turning off camera.") webcam.release() print("Camera off.") print("Program ended.") cv2.destroyAllWindows() break |