Loading Video and Webcam – OpenCV 3.4 with python 3 Tutorial 2
In this tutorial we’re going to see how to load the video from it’s source whether it’s a webcam or a video file.
The process is relatively simple. After we import the libraries cv2 and numpy, we need to define the cap object.
To load the frames from the webcam:
- cap = cv2.VideoCapture(0)
The number 0 it means the first webcam. If you have more webcam installed, you can change the number to 1 take second or to 2 for thirt webcam and so on.
To load the frames from a video file we use the same function but instead of passing a number as argument, we pass the file title (if the file it’s in the same folder of the python script) like in example 1 or the entire path if it’s on another folder like in example 2:
- cap = cv2.VideoCapture(“video_file.mp4”)
- cap = cv2.VideoCapture(r”C:\Users\MYUSER\Documents\video_file.mp4″)
*Please note the prefix “r” before the string. The r must be used to avoid that escape character are interpredet according to the python rules.
Working with the video frames:
A video is just a sequence of images, so keeping this in mind, we need to operate with the images.
To work with videos we work inside a while loop, there we extract the frames and do all the operations.
After creating the object cap, we need to call it to read the frames wheter it’s from a video file or from the webcam.
- ret, frame = cap.read()
Ret is just equal to True or False. It’s true if cap is reading a frame, and frame is the the array containing the image.
Then there we show it using cv2.imshow like we did for the images.
And we have the function waitkey:
- key = cv2.waitKey(25)
This function is literally waiting for us to press a key. Each key that we press on the keyboard is associated to a number.
Thanks to this we can do any operation we want pressing some key.
For example we decide that when we press the “Esc” key on the keyboard, we want to close it. The esc key correspond to the number “27” and after that we break the loop so it stops the executions.
Read from Webcam
import cv2 import numpy as np cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() cv2.imshow("frame", frame) key = cv2.waitKey(1) if key == 27: break cap.release() cv2.destroyAllWindows()
Read from File
import cv2 import numpy as np cap = cv2.VideoCapture("red_panda_snow.mp4") while True: ret, frame = cap.read() cv2.imshow("frame", frame) key = cv2.waitKey(25) if key == 27: break cap.release() cv2.destroyAllWindows()
Read video from file and Save it to file
import cv2 import numpy as np cap = cv2.VideoCapture("red_panda_snow.mp4") fourcc = cv2.VideoWriter_fourcc(*"XVID") out = cv2.VideoWriter("flipped_red_panda.avi", fourcc, 25, (640, 360)) while True: ret, frame = cap.read() frame2 = cv2.flip(frame, 1) cv2.imshow("frame2", frame2) cv2.imshow("frame", frame) out.write(frame2) key = cv2.waitKey(25) if key == 27: break out.release() cap.release() cv2.destroyAllWindows()
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Opencv Beginner Tutorial
- 1) Loading images
- 2) Loading Video and Webcam
- 3) Drawing and writing on images
- 4) Basic operations on images
- 5) Add images and Threshold
- 6) Blending images
- 7) Bitwise Operators
- 8) Trackbars
- 9) Object detection using HSV Color space
- 10) Basic Thresholding
- 11) Histograms
- 12) Basic geometric transformations
- 13) Perspective transformation
- 14) Affine transformation
- 15) Adaptive thresholding5
- 16) Smoothing images
- 17) Morphological transformation
- 18) Edge detection
- 19) Find and Draw Contours
- 20) Template matching
- 21) Lines detection with Hough Transform
- 22) Corners detection
- 23) Image Pyramids
- 24) Image Pyramids (Blending and reconstruction)
- 25) Feature detection (SIFT, SURF, ORB)
- 26) Feature Matching (Brute-Force)
- 27) Mouse Events
- 28) Histogram and Back Projection
- 29) Object tracking with Mean-shift
- 30) Object tracking with Camshift
- 31) Optical Flow with Lucas-Kanade method
- 32) Background Subtraction
- 33) k-Nearest Neighbour classification
- 34) Object tracking using Homography
- 35) Fourier Transform
- 36) Knn handwritten digits recognition
- 37) Face detection using Haar Cascades