Reversing the Video Using Computer Vision

Aman Preet Last Updated : 19 May, 2022
5 min read

This article was published as a part of the Data Science Blogathon.

Introduction

In this article, we will be working to develop an application from computer vision techniques that will reverse the video, and also, we will be able to save that reversed video in our local system. In this application, we will also have access to change its quality like- 360/720p.

So let’s first understand what we will have to do to build this application.

 

reversing video using computer vision
Image Source: LightMV

 

Steps to Build this Application

This section will discuss the steps that need to be done in building this application. Here, we will set up the template and follow this approach throughout the tutorial.

  1. Importing the cv2 library: This is a pretty straightforward job, so we will simply import the cv2, i.e. Python’s computer vision library.
  1. Reading/Capturing the video: Then, we will read the video that we want to reverse.
  1. Working with frames: In this section, we will be working with frames of the video to get the processing speed, i.e. Frames per second and the count of the frame.
  1. Height and width: Here, we will simply extract the height and width of the video that we have read so that we can use it in further rescaling parts.
  1. Rescaling and setting type of output video: Now, in this part, we will simply rescale our video to a certain size and set the type of video that we will be saving at the end.
  1. Loop everything in: At the last, we will put everything in the loop with some conditions related to frames, and we are all set to reverse our video.

Original video

Before working on the coding part, we should be aware of what our original video looks like! so that while seeing the results, we can easily compare both of them. So this video is a lens pack opening video from YouTube, and in this article, we will be working on this video only and reverse the same using computer vision techniques.

Import the Library

So here we are only importing the computer vision library, i.e. cv2.

import cv2

Reading the Instance of the Video

So here comes the step when we will be reading our video. So first, let’s see the code, and then we will discuss it.

cap = cv2.VideoCapture('sample2.mp4')

So basically, we are using the cv2’s Video Capture function to read the video. For that, we are simply passing the path of the video in the parameter enclosed within inverted commas; just keep one thing in mind, if the video is in the same folder, then one doesn’t need to use the full path instead; use the name of the video with its extension.

Working with frames

So in this particular section, we will be working on getting the total number of frames that our video holds, and for that, we will be working with the FRAME COUNT object
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)

So here, one can easily decode the functionality while breaking down the name of the function:

  • CAP: Capture
  • PROP: Property
  • FRAME_COUNT: Total number of frame count

But are we using the most efficient way to get the total number of frames per second? The simple answer is “YES!”

Reason: If we go for counting the frames using a loop and incrementing the counter each time, then it will be an exhausting process both for the developer and also for the CPU because of the processing time, but if we use the built-in method, then we can get away with both the problems.

So, just before we get access to the total number of frames of the video now, we will try to get the frames per second (FPS).

fps = cap.get(cv2.CAP_PROP_FPS)

If you have the concept of counting the total number of frames, this will be very easy to understand. If we breakdown the built-in function of counting FPS, then we can see the below abbreviation:

  • CAP: Capture
  • PROP: Property
  • FPS: Frames per second

Get the Height and Width of the Video

Here we will be calculating the height and width of the video so that we can rescale the output video based on our preference.
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  • Height: The built-in function we used here is CAP_PROP_FRAME_HEIGHT, where we can see that FRAME_HEIGHT will get the height of each frame, i.e. for the complete video.
  • Width: Similarly, FRAME_WIDTH will be used to find the video’s width.

Initiating the Output Writer for the Video

Now we will first use the Video Writer,  the fourcc method, to give the 4 character code to the output video to compress the frames according to our needs. Here we are using the “MJPG” character code to compress the frames. It is also known as video codec, and we have a variety of video codecs that we can pass in the function as a parameter if we want to see the list of parameters, then that is available on FOURCC page.

Note: We are using the MJPG character code which means motion-jpeg codec.

Then we are simply rescaling the output video to their half proportion, i.e. reduced to 50% for both height and width.

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('reversed2.avi', fourcc,fps ,(int(width*0.5), int(height*0.5)))

Let’s print some results

  1. Total number of frames
  2. Frames per second during processing.
print("No. of frames are : {}".format(frames))
print("FPS is :{}".format(fps))

Output:

No. of frames are : 488.0
FPS is :25.0

Here we will retrieve the index, i.e. position of the last frame of our video.

frame_index = frames-1

Final Step

Let’s put everything in a loop to get some results; here, we will be performing all the previous operations that are already done in a loop and then will save the video in our local system

if(cap.isOpened()):

    while(frame_index!=0):

        cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
        ret, frame = cap.read()

        frame = cv2.resize(frame,(int(width*0.5), int(height*0.5)))

        out.write(frame)

        frame_index = frame_index-1

        if(frame_index%100==0):
            print(frame_index)

out.release()
cap.release()
cv2.destroyAllWindows()

Output:

400.0
300.0
200.0
100.0
0.0

Code breakdown:

  1. Let’s first check that the instance of the video is ready? with the help of its opened() function. IF yes,
  2. Then we will read the frames till the end of the video using the frame index
  3. Now, as we want to get the reverse video, we will initialize the position of the current frame to the last frame.
  4. Optional: Now, if we want to see the live reverse video, we can go for cv2.imshow(‘name’, frame), but,
  5. If we want to download (write) the video in our local system, then we are gonna use the write method, and after each step, we will simply decrease the frame index.
  6. The final step will be to print the frame value (this is just for checking the progress), and after that, release the instances and destroy the window.

Output:

So, here we can see that the original video has been successfully reversed with the help of all the computer vision operations that we did previously.

Note: This video I have first with the help of the write() function and later uploaded on my YouTube channel.

Conclusion

So we have covered all the steps we have discussed previously, and now we have written (downloaded) our reversed video, which you can see in the above YouTube video link. Now let’s summarize what we have learned so far.

  1. The first takeaway from this article is that we learned how to work with frames when it comes to videos, both locally and in real-time.
  2. We also learned the most optimal way of getting the frames of video and its processing time using the in-built function.
  3. Then along with writing the video after pre-processing, we also noticed how a simple twitch and turn could give us the correct output (here is the reverse video).

Here’s the repo link to this article. I hope you liked my article on reversing the video using computer vision. If you have any opinions or questions, then comment below.

Connect with me on LinkedIn for further discussion.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details