To play the video from a file, you must provide the video path in the VideoCapture() method. It is similar to capturing from the camera by changing the camera index with the file name.
The time must be appropriate for the cv2.waitKey() function, and if the time is high, the Video will be slow. On the other hand, if the time is too low, the Video will be high-speed.
import cv2
cap = cv2.VideoCapture('friends.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
In this example, when creating a VideoCapture object, we pass the Video called friends.mp4.
Then we read frame by frame and show it to the Python window.
To show grayscale video, use cv2.cvtColor() method to convert video into Grayscale.
import cv2
cap = cv2.VideoCapture('friends.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This code will open the grayscale Video.
That’s it.