Open a new file in IDLE. Type following. The program is self explanatory.

 

import cv2 # importing necessary libraries
import numpy as np

cv2.startWindowThread() # we use this thread to create a new window for our video
cv2.namedWindow(“Video Player”, cv2.WINDOW_NORMAL) # Create a new window
cv2.resizeWindow(“Video Player”, 550, 450) # Resizing the above window

video = cv2.VideoCapture(“filepath/video file with extension”) # Put your file path and video file name

while video.isOpened(): #Starting a loop to play the video continuously

ret, frame = video.read()

if not ret:

break

cv2.imshow(“Video Player”,frame) # Display the video

k = cv2.waitKey(5) & 0xFF

if k == 27:    # Use Esc key to exit the program

break

cv2.destroyAllWindows()
video.release()

 

Then save and run with Run -> Run Module   or   just hit F5.

Play a Video with Opencv

Leave a Reply

Your email address will not be published. Required fields are marked *