I'm trying to play a video file using python opencv this is my code , but it is not showing the vidfeo file when I run the code
import numpy as np
import cv2
cap = capture =cv2.VideoCapture('C2.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
I tried the answer in : link but not working again
To read a video with OpenCV, we can use the cv2. VideoCapture(filename, apiPreference) class. In the case where you want to read a video from a file, the first argument is the path to the video file (eg: my_videos/test_video. mp4).
To capture a video, we need to create a VideoCapture object . VideoCapture have the device index or the name of a video file. Device index is just the number to specify which camera. If we pass 0 then it is for first camera, 1 for second camera so on.
You can read, display, write and doing lots of other operations on images and videos using OpenCV. The OpenCV module generally used in popular programming languages like C++, Python, Java. To save a video in OpenCV cv. VideoWriter() method is used.
I think u just have to increase the number inside cv2.waitKey() function to may be 25 or 30. You should get the desired result.
Also, there is no need to write cap = capture = cv2.......
Simply, writing,
cap = cv2.videoCapture('path of the video')
should work as well . Hope it works.
import numpy as np
import cv2
cap = cv2.VideoCapture('C2.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
# & 0xFF is required for a 64-bit system
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
This code worked for me. It shows both the original and the grayscale video output. Press 'q' to exit. I also didnt see the need for cap = capture... in your code.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',frame)
cv2.imshow('grayF',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With