In this Python OpenCV article iam going to talk about Python OpenCV Reading AVI Video Format, so first of all let’s talk that What is OpenCV.
What is OpenCV ?
OpenCV (Open Source Computer Vision) is free and open source library for computer vision and image processing. it was originally developed by Intel in 1999 and is now maintained by the OpenCV Foundation, a non profit organization.
OpenCV provides different functions and algorithms for tasks such as image and video processing, feature detection and extraction, object recognition, camera calibration and machine learning. it is written in C++, but also provides Python and Java interfaces for developers who prefer those languages.
OpenCV supports different platforms and operating systems, including Windows, Linux, macOS, Android and iOS, and it is widely used in industry and academia for applications such as robotics, surveillance, medical imaging and augmented reality.
How to Install Python OpenCV?
You can install OpenCV using pip.
1 |
pip install opencv-python |
Some of the key features of OpenCV include:
- Image processing: OpenCV provides different functions for manipulating images such as filtering, smoothing, thresholding and edge detection.
- Feature detection and extraction: OpenCV includes algorithms for detecting and extracting features from images, such as corners, blobs and keypoints.
- Object recognition: OpenCV provides tools for training and using machine learning models to recognize objects in images and videos.
- Camera calibration: OpenCV includes functions for calibrating cameras, correcting lens distortion, and estimating camera parameters.
- Real-time video processing: OpenCV is optimized for real time processing of video streams and provides tools for tracking objects, detecting motion, and analyzing video data.
OpenCV is licensed under Apache 2.0 license, which allows developers to use and distribute it freely even in commercial applications.
Python OpenCV Reading AVI Video Format
Video can be Read either by using the feed from camera connected to a computer or by reading a Video file. The first step towards reading a video file is to create a VideoCapture object. Its argument can be either the device index or the name of the video file to be read. In most cases, only one camera is connected to the system. So, all we do is pass ‘0’ and OpenCV uses the only camera attached to the computer. When more than one camera is connected to the computer, we can select the second camera by passing ‘1’, the third camera by passing ‘2’ and so on.
In this article we are using a globe avi you can download the AVI file from the link .
Also you can check Python GUI Development Tutorials in the below link.
- PyQt5 GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development Tutorials
- Kivy GUI Development Tutorials
This is the complete code for Reading AVI File Format
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 |
import numpy as np import cv2 # Open the video file for capturing frames cap = cv2.VideoCapture('vid.avi') # Loop through the video frames while (cap.isOpened()): # Read the next frame from the video ret, frame = cap.read() # Convert the frame to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the grayscale frame cv2.imshow('frame', gray) # Check if the user pressed 'q' to quit if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture object cap.release() # Close all OpenCV windows cv2.destroyAllWindows() |
In the above code first we have imported Opencv, after that we are going to create a video capture i have said that if your using camera feed you can give “0” in the constructor but if your using a AVI file you can give the name of that AVI file. after that we are going to create a while loop an we are checking if Video Capture is opened we are reading the AVI file.
Run the complete code, you will have your avi video.
FAQs:
Does OpenCV support AVI?
Yes, OpenCV supports AVI (Audio Video Interleave) format for reading and processing video files.
How to read an mp4 file using cv2?
To read an mp4 file using OpenCV (cv2
), you can use the VideoCapture class. This is an example:
1 2 |
import cv2 cap = cv2.VideoCapture('video.mp4') |
What is fourcc for AVI?
FourCC is a four character code used to specify the video codec. For AVI files, common FourCC codes include MJPG (Motion JPEG), DIVX (DivX codec), XVID (Xvid codec), and many others. For example:
1 |
fourcc = cv2.VideoWriter_fourcc(*'MJPG') |
Subscribe and Get Free Video Courses & Articles in your Email