简体   繁体   中英

Read a video in opencv (python)

I'm trying to read a video file in opencv (python 2.7), and I just copied the example in the opencv tutorial, but nothing happens:

import numpy as np
import cv2

cap = cv2.VideoCapture('input.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()

The function cap.isOpened always returns FALSE.I have already tried to use absolute path in the argument of VideoCapture, but I still get the same result. What am I getting wrong?

Maybe your OpenCV version is not properly installed. You can check your build infos with print cv2.getBuildInformation() if there is any weird components.

I would suggest to rebuild it, or install it via Anaconda to be sure not to miss any package.

您需要定义视频位置或将视频安装到python安装位置

Keep the full path of the video file. For example :-

cap = cv2.VideoCapture("D:\\Video Folder\\input.mp4")

I believe this would solve this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM