Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

playing video file using python opencv

Tags:

python

opencv

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

like image 899
Musa Avatar asked Nov 24 '15 17:11

Musa


People also ask

How do I watch videos on cv2?

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).

How do I embed a video in OpenCV?

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.

Does OpenCV work on videos?

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.


3 Answers

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.

like image 191
aamer aamer Avatar answered Oct 27 '22 17:10

aamer aamer


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()
like image 28
stacksonstacks Avatar answered Oct 27 '22 15:10

stacksonstacks


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()
like image 1
Suhas Sreenivas Avatar answered Oct 27 '22 16:10

Suhas Sreenivas