Getting Started with Videos in OpenCV

Hi there, and welcome again to Dev-akash's blog post, so today we will be getting started with videos in OpenCV

Introduction

Hi there, and welcome again to Dev-akash's blog post, so today we will be getting started with videos in OpenCV, and will be learning about the following things:

  • Capturing Video using Web Camera
  • Reading Video from a file

After going through the blog you will be able to read a video from a file and capture the live feed from your own camera and yes with this you can also read multiple cameras at same time (Bonus!).
So let's get Started!!

Capturing Video using Web Camera

Capturing a video from a web camera is a very basic requirement for most of the tasks in Computer Vision field such as Realtime Attendance marking system, Pose estimation and Recognition, Pedestrain Detection, etc which requires the live feed from the cameras and now you would be able to do that too. So first have a look at the code and then we will dive into it to understand it more clearly.

#importing nesseccary modules
import cv2
import numpy as np

#Accessing default camera
cam = cv2.VideoCapture(0)

#A while loop for reading each frame of the camera
#till the camera is open.
while(cam.isOpened()):
#Reading the Frame from the camera feed
ret, frame = cam.read()

#Showing the frame
cv2.imshow("Webcam", frame)

#Condition for breaking the loop on pressing the 'q'
if cv2.waitKey(1) & 0xff==ord('q'):
break

#Closing/releasing the Camera
cam.release()
#Closing all the windows created
cv2.destroyAllWindows()

So, lets dive into the code

In line 2 and 3 we are importing the importing libraries, namely OpenCV and Numpy as we will use them.

In line 6 we are calling a function of opencv as cv2.VideoCapture() which is actually accessing the camera attached to your computer and passing the data captured from it to the variable cam, the argument passed in the parentheses as 0 states that we are using the default camera or the first camera if multiple cameras are connected and if you want to switch to another camera then default, just change the argument 0 to 1, 2, 3... etc as per number of cameras connected.

From line 10-19 we are running a loop to read the frames coming from camera and showing it to a window created by OpenCV. So in the loop in line 12 we take ret, frame after reading the input from the cam as cam.read(), where ret is the status of the captured frame and frame is the image.

In line 15, we are showing our captured image in frame variable in a window named "Webcam" using the opencv function cv2.imshow()

In line 18-19, we are creating a condition that when a user presses the q button on keyboard the loop breaks and executes the upcoming lines

In line 22 and 24, we just close the opened camera and also close all the windows created by opencv during the execution of program.

Reading Video from a file

Reading a video from a file is not a much different task as what we have to do is to change the source of feed provided, that is, in the previous section our source of input was camera and hence we were giving argument as a number 0 to access the default camera and now to access a file on the system as feed we will provide the path of the file as argument to the function.
Here is the code, check it out. And just to remind after this we have a bonus.

#importing nesseccary modules
import cv2
import numpy as np

#importing nesseccary modules
import cv2
import numpy as np

#Accessing video from sytem location
video = cv2.VideoCapture('../sample/video.mp4')

#A while loop for reading each frame of the camera
#till the video not ends.
while(video.isOpened()):
#Reading the Frame from the camera feed
ret, frame = video.read()

#Showing the frame
cv2.imshow("Playing Video", frame)

#Condition for breaking the loop on pressing the 'q'
if cv2.waitKey(1) & 0xff==ord('q'):
break

#Closing/releasing the Camera
video.release()
#Closing all the windows created
cv2.destroyAllWindows()

Bonus

So as stated above i am going to show you how you can read mutiple cameras simultaneously and I know after reading this much of blog you would be having a idea how to do it. But to make you comfortable with it I am just going to provide the code here and I promise there is nothing new to explain here, in-fact you will explain what is happening to yourself (Its a great exercise !!).

And Thanks for your love and support by reading this blog !!

#importing nesseccary modules
import cv2
import numpy as np

#Accessing default camera and the other camera
cam = cv2.VideoCapture(0)
cam1 = cv2.VideoCapture(1)

#A while loop for reading each frame of the camera
#till the camera is open.
while(cam.isOpened() and cam1.isOpened()):
#Reading the Frame from the camera feed
ret, frame = cam.read()
ret1, frame1 = cam1.read()

#Showing the frames of both webcam
cv2.imshow("Webcam", frame)
cv2.imshow("WebCam1", frame1)

#Condition for breaking the loop on pressing the 'q'
if cv2.waitKey(1) & 0xff==ord('q'):
break

#Closing/releasing both the cameras
cam.release()
cam1.release()
#Closing all the windows created
cv2.destroyAllWindows()
Akash Srivastava
Software Engineer @ Seawoods Ventures Inc.
© 2019-2024 dev-akash.in