OpenCV Python Tutorial for Beginners Part 4
Himanshu K.
Frontend React JS Developer @ Dynamics Monk | React JS | Next JS | Typescript | JavaScript | React Testing Library | Jest | Webpack | Parcel | Low-Level & High Level System Design
Do you know you can also change or get the capturing device (inbuilt camera, video, or webcam feed) properties while using it? Yes, it is as easy as getting shape and resizing the image array.
In this OpenCV python tutorial part, we will explore those methods, and see how we can use them, and debug if we encounter any issues.
Didn't read the previous parts? Check out those parts so that you can get familiar with and understand every method efficiently.
- OpenCV Python Tutorial for Beginners Part 3
- OpenCV Python Tutorial for Beginners Part 2
- OpenCV Python Tutorial for Beginners Part 1
In this part, we will explore an example so that we can play with capturing device properties efficiently. Once we understand the properties and methods correctly, then it will be much easier to use.
Now, Let's get started:
Get & Set the Camera feed/Capturing device properties:
Code:
cam = cv2.VideoCapture(0) #To use inbuilt webcam
width = cam.get(cv2.CAP_PROP_FRAME_WIDTH) #To get camera properties height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) #To get camera properties print(f"Before Setting the Camera Width: {width} & Height: {height}") cam.set(cv2.CAP_PROP_FRAME_WIDTH,352) cam.set(cv2.CAP_PROP_FRAME_HEIGHT,288) width = cam.get(cv2.CAP_PROP_FRAME_WIDTH) #To get camera properties
height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) #To get camera properties
print(f"After Setting the Camera Width: {width} & Height: {height}") #loop will run if camera is opened and working while cam.isOpened(): ret, frame = cam.read() #To read webcam feed cv2.imshow("Frame",frame) #loop will not stop until it is detected "q" input if cv2.waitKey(1) == ord("q"): break cam.release() #will release the camera so that other apps can use cv2.destroyAllWindows()
Let's understand the code:
cam = cv2.VideoCapture(0) #To use inbuilt webcam
In the first line, we have used the cv2.VideoCapture method, and passed the 0 as an argument to use the inbuilt camera, and loaded that in the cam variable. If you have more cameras like a webcam to use, then try other integer arguments like 1, 2, and 3, etc.
If you don't have a camera and want to try this method, then you can also read saved videos, and to do this task, you need to give the full path of the video (the path should be in string format). Example: r"C:\Users\Himanshu\downloads/example.mp4"
Note: Don't forget to put "r" before the video path because we need to give a raw string as an argument to read the video. Otherwise, it will raise the error.
width = cam.get(cv2.CAP_PROP_FRAME_WIDTH) #To get camera properties
height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) #To get camera properties
To get the capturing device property before reading frames, we have used the cam.get() method in the third & fourth lines, and we gave some camera properties instances as an argument to get the width & height property of the inbuilt camera.
To get the width, we have used the cv2.CAP_PROP_FRAME_WIDTH instance, and saved the output in the width variable.
To get the height, we have used the cv2.CAP_PROP_FRAME_HEIGHT instance, and saved the output in the height variable.
print(f"Before Setting the Camera Width: {width} & Height: {height}") #printing the width & height
In the Sixth line, we have shown the height and width. These values can be different, and it depends on the using camera or the video you have used.
Now, we have seen to get a capturing device or video file properties. Now, it is time to learn to set the capturing device or video file properties.
cam.set(cv2.CAP_PROP_FRAME_WIDTH,352)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,288)
In the Eighth line & Ninth line, To set the desired video capturing device property/video property, we have used the cam.set method multiple times with different arguments to set the height and width.
To set the width of the capturing device/video file property, we passed cv2.CAP_PROP_FRAME_WIDTH as the first argument of the cam.set and gave the width value which we want to apply.
To set the height of the capturing device/video file property, we passed cv2.CAP_PROP_FRAME_HEIGHT as the first argument of the cam.set and gave the height value which we want to apply.
Note: Please note that you can not set width/height value more than the capturing device width/height property. If you do this, then cv2 will automatically take the original width and height of the capturing device. Try to play with lower values.
My Experience: while using the trial and error method for learning some of these properties methods, I found that we can not set width and height property below to half of the width and half of the height property.
Let's consider max & low allowed values as the upper bound and the lower bound. Whenever we give values between max and lower bound, cv2 automatically adjust those given value to another so that it can maintain an aspect ratio of the frame.
width = cam.get(cv2.CAP_PROP_FRAME_WIDTH) #To get camera properties
height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) #To get camera properties
In the eleventh and twelfth lines, we have used the cam.get method again to get the capturing device/video file width & height and saved in the width & height variable so that we can see that the capturing device/video file property is changed or not.
print(f"After Setting the Camera Width: {width} & Height: {height}") #printing the new width & height
In the fourteenth line, we have used the print to see the value is changed or not. If it does not change and stay the same as the previous one, then we need to change values in the cam.set method until we get the satisfying output.
#loop will run if camera is opened and working while cam.isOpened(): ret, frame = cam.read() #To read webcam feed cv2.imshow("Frame",frame) #loop will not stop until it is detected "q" input if cv2.waitKey(1) == ord("q"):
break
In the seventeenth line, we have used a while loop. It will run if the camera is successfully opened without any error by the cv2.VideoCapture method, and under the while loop, we have used the cam.read() to get camera feed frames from the camera.
The cam.read() method will return two things, first is retention (bool type), which will return True if the camera is getting frames, and the second thing is frames (video frames), which will receive from the camera feed.
In the very next line, we have used the cv2.imshow method to create a GUI window so that we can show the receiving frame on the screen. In the cv2.imshow, we passed two arguments:
- The first argument is windows name (should be in string format), and it is required to give.
- The second argument is the frame variable, which we want to show on the screen.
To stop the while loop from continuously running, we have used the cv2.waitKey method with if condition, which will only execute if cv2.waitKey detects the press "q" key.
Note: Please note that, In the cv2.waitKey method, we need to give "1" as an argument to run frames continuously. If we don't pass any value there, then the GUI window (where receiving frames display) will stop on the first frame and will not show other receiving video frames.
cam.release() #will release the camera so that other apps can use
In the twenty-fifth line, we have used the release() method to close the capturing device or to close the video file so that other apps or functions can use the capturing device. If we don't do use this method, then the capturing device will not be free or released, and other apps or functions can not be able to use the capturing device.
cv2.destroyAllWindows()
In the last line, we have used the cv2.destroyAllWindows() method to destroy the GUI window, which we have created in previous lines (cv2.imshow).
If we don't use the destroyAllWindows() method, then the GUI window which we have created earlier will stay and stuck (with the last received frame) on the screen.
Now, we have seen to get a capturing device or video file properties. Now, it is time to learn to set the capturing device or video file properties.
Note: Don't use the set method inside the while loop. If you use it inside the while loop, then this property setting method will not work.
Try to use it before the while loop.
To get all capturing device property, type cv2.CAP_PROP_ then IntelliSense will show all available properties. You can also use the below code to get all at once:
CaptureProp = [i for i in vars(cv2) if "CAP_PROP_" in i]
Note: I hope you like the fourth part of my OpenCV tutorial Series. In case of any mistakes in this tutorial. Please feel free to comment so that I can improve the quality of my articles.