This article was published as a part of the Data Science Blogathon.
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.
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.
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.
So here we are only importing the computer vision library, i.e. cv2.
import cv2
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.
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
So here, one can easily decode the functionality while breaking down the name of the function:
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:
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
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)))
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
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:
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.
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.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.