FabMan
FabMan

Reputation: 11

Play video Basic

im new to python openCV,found this code from openCV page;

import cv2
cap = cv2.VideoCapture('Megamind.avi')
while (cap.isOpened()):
   ret, frame = cap.read()
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('MMR3', gray)
if cv2.waitkey(25) & 0xFF == ord('q'):
    break
cap.release()
cv2.destroyAllWindows()*

Tried to run it but it gives error which after done some digging up, i replaced this line: "cap = cv2.VideoCapture('Megamind.avi')" with this line:

"cap = cv2.VideoCapture('Megamind.avi', cv2.CAP_FFMPEG)"

The program run without error but the video windows("MMR3") was not displayed.

**Im using python 2.7.13 with opencv3 running on MacOs Sierra. **Megamind.avi is available in the same folder where the code is

Upvotes: 1

Views: 94

Answers (1)

Jai
Jai

Reputation: 3310

your cv2.imshow has to be in the whille loop. If your cv2.imshow() is outside while loop than it will only display the last frame of you video. Change you code to below code

import cv2
cap = cv2.VideoCapture('Megamind.avi')
while (cap.isOpened()):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('MMR3', gray)
    if cv2.waitkey(25) & 0xFF == ord('q'):
       break
cap.release()
cv2.destroyAllWindows()

Upvotes: 1

Related Questions