COMPUTER VISION TO CROP AND DISPLAY VIDEOS IN PYTHON
Swaroop Shinde
1x Red Hat Certified (EX180) ★ DevOps enthusiast ★ Docker ★ Ansible ★ Terraform ★ Jenkins ★ Linux ★ Kubernetes ★ Git-Github ★ Cloud Computing
?? Hello Everyone....Till Now we were Only Cropping, Displaying & Manipulating Images in Python by Computer Vision Module. In This Article we will see How we can Crop A Video and Display it in a New Window.
???To implement this Practical we will be using Anaconda Python which you can download by Clicking?Here?and from Anaconda Python we will be using Jupyter Notebook to Implement this Practical.
?? Let's Begin.
---------------------------------------------------------------------------------------------
? Practical Implementation :
?? Again, There's nothing Complex to Know first so as we will move ahead with the code, you'll Learn How each statement is Implemented.
?? Let's First import The cv2 Module.
?? The second Statement you can see, it means the cv2 module have a Function called VideoCapture which we are storing in cap variable which have Capability to Connect to the Laptop / PC external camera and Can Capture Video as you usually do in Mobile Phones.
?? Inside this Function We have Provided The Path of The Video on the Top of Which we are Going to Perform Cropping & If you want to capture Your self i.e a Real Time Video, Simply give argument as 0 inside VideoCapture() so it will connect to your Camera.
--------------------------------------------------------------------------------------------
?? Now Since we have Imported The Video, we want it To be Displayed in a New Window, But First we will need a Function which will read each and every Frame of the Video. So we Have Another Function called cv2.read(), so this will Read the Frames from the Video and will Stored in image Variable.
?? The ( _ ) means we are Capturing every single Frame using cap.read() from the video and Storing it in Image variable. Also we need a Loop Because if we'll do it Without While Loop, it means the Computer will Only Read one Frame since one Statement without any loop performs only one Operation at a Time, so we need while loop for Capturing the Video Continuously.
--------------------------------------------------------------------------------------------
?? Now we are ready to Display the Video.
?? If cv2 can Import & Read the Video, it can Also show the Video using cv2.imshow() Function, The First Argument Passed is The Name for the New Window which will open to Display the Video & The Next is What we want to Display, in this case it's image variable which contains video frames.
?? Let's See how the Video Looks Like,
?? You can See as per the Argument in the cv2.imshow() Function, we have a New Window Opened Up, But why is it Not Showing the Video & Not Responding ?
领英推荐
?? As we have VLC media Player or Normal Windows Media Player, it Works Normal because behind the Scene in the Code, They have specified till What Time the Video Should be Displayed & Also is Coded in Such a Way that if we Click on the Cross at Top Right It will Close the Media Player.
?? Similarly Since in Our Code we Haven't Specified When to Close The Video, if we will Click on the Cross, the Computer won't Understand what is to be done if user clicks on the Cross. So for this we will Have to Use Two More Functions.
?? To Specify till What time the Video should be Playing. We use cv2.waitKey() Function and The Argument 25 inside is the Speed of the Video which is Normal & 13 Means when we'll Press Enter Key , the Video will be Paused.
?? So Since we want to Close the Window, We use cv2.destroyAllWindows() Function which will destroy the Window as the Name Suggest. So we will be Having A Video Playing Without any error.
?? So we Have Our Video Player Now, Let's Crop This Video and Display it in Different Windows.
?? As we have already seen in My Previous Computer Vision Article on How to Crop Images in Python. Same Syntax can be Used To Crop Video. For Example
?? So you can see here we are Cropping The video by [0:300, 0:200] Dimensions and Storing it in A variable so we can display the Cropped Image.
?? Similarly You can Do This Infinite Time If you Wish Too.
?? So Here we Have Cropped the Video With 4 Different Dimensions and Displaying it in different Windows. And we Will Have,
---------------------------------------------------------------------------------------------
???So From the Above Article we Saw How we can Import, Read & Display & Crop Videos using Computer Vision. If you Find This Interesting then Do Follow & Connect???.
THANK YOU !!
---------------------------------------------------------------------------------------------