Skip to main content

OpenCV - Play Video with python

Hello Guys,

In this tutorial, I will show you how to play a video file using OpenCV.

To play a video file or to stream a webcam OpenCV has a function named VideoCapture. This function takes only one argument.

If you want to play the video you need to pass the file name with extension. Ex: nature.mp4. Similarly, If you want to stream your webcam you need to pass a number or device index. Which simply means which webcam you want to access. Just in case, If you have more than one webcam.

If you have only one webcam then you have to pass 0 or else if you have two web cameras then for accessing the second web camera you have to pass 1 and so on. 

Once we have initialized the video object by calling the VideoCapture function.
Then we will use an infinite while loop which will iterate till video.read() method returns true.

read() function returns two parameters

 1. Boolean – If True video still playing or False if the video is ended.
 2. Frame – Here frame is simply your picture. 

We have also used imshow function to show a frame.

Program

#import cv2 moduleimport cv2 as cvvideo = cv.VideoCapture("video.mp4")while True:    ret,frame = video.read()    # If condition is used to check whether video is still playing or     #ended. True means playing False means video ended    if ret == True:        #frame means image        cv.imshow('video',frame)        # Pressing a key q to break the loop        #waitKey is used to display frames with 30seconds halt. So         #every frame will be rendered in 30seconds.        #You can adjust as per your requirement.         #Video will be too fast if less time will be provided ex:         #waitKey(1)        #Or too slow if more time is provided Ex: waitKey(60)        if cv.waitKey(30) & 0xFF == ord('q'):            break    else:        break        #release captured resources        video.release()#destroy opened windowscv.destroyAllWindows()

Comments

Popular posts from this blog

Automation - Update Naukri Profile Using Selenium

Recently one of my friend came to me with a problem. He is looking out for new job but he feels quite boring to update  his profile on daily basis. As some people says updating profile in the morning gives you more calls as it keeps the newly updated profile on top (Although i don’t know whether naukri works this way or not 😀 ). As i was more interested to solve his problem.  After listening his problem i came to solution that instead of updating it manually lets make this job automatic. And it is quite interesting how we can automate our daily boring task with automation. Another day i came with the solution . And the solution was to make it automatic using selenium (Those who are not aware about selenium do check this link) In short, Selenium is a Testing automation Framework. And it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should) also be automated as well....

Shut Down the Computer using notepad

Open notepad >> Write @echo off msg * I am tired. shutdown -c “So, Bye Bye dear” -s >> Save the File as AnyName.bat When you will open this file, your Computer will Shut down on its own after showing the message in it. NOTE: FOR EDUCATIONAL PURPOSE ONLY.I AM NOT RESPONSIBLE FOR IF ANYTHING GOES WRONG…. 

USB is not detecting by my Computer what's wrong?

Fix For Mobile (Mass Media/Storage Mode):   • First Check out Your sd card no password on your Card • Use Pc suite mode (for fixing this problem)   • Now • Goto Device Manager • Disable that Installed Driver Now again Enable it • Now Restart Your Phone And Pc. • Connect our mobile to USB’s port • Now it prompt n installation proceed Fix For USB Not Recognized:   • Firstly Check whether Your USB port is Disabled by System or not. • Link: http://computerfunz.blogspot.com/2011/05/how-to-enable-or-disable-usb-port.html • Now Follow this Steps • Goto Device Manager or • Start • RUN type Devmgmt.msc • Now you have to search for USB Controller Hub or Universal Serial Bus controllers • Now click Properties. • Now Click on Power Management tab • >Allow the computer to turn off this device to save power >click OK. • Now Hit > Scan for hardware changes. >Now it Start Detecting New Drivers for USB.