Skip to content Skip to sidebar Skip to footer

How To Load A Video In Opencv(python)

I am a begginer in OpenCV and Python. I tried to load a video and displaying it using code given below: import cv2 cap = cv2.VideoCapture('G:\3d scanner\2.mmv') while(1): _ , i

Solution 1:

This might help you:

import numpy as np
import cv2

cap = cv2.VideoCapture('linusi.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        breakcap.release()
cv2.destroyAllWindows()

If it doesn't work, there are a lot of useful explanations in their documentation: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html

Solution 2:

There are a couple important differences when using VideoCapture on a video file. First off, there's no built in frame delays like when you're capturing from a webcam. Since most computers are very powerful these days, unless you deliberately introduce a delay between frames, the video will be displayed in a blink of an eye.

Secondly, the video has an end unlike input from your webcam, so you need to explicitly handle that case. I suspect what's happening in your case is that the video is completing in a matter of milliseconds, and then the final cap.read() is returning an empty matrix which imshow() subsequently complains about.

See Opening video with openCV +python. One of the answers in there is directly applicable to your situation.

Solution 3:

I think you have no cascade file that helps it determine what to compare with and when to exit. Try the following code and see.

import cv2 
    cap = cv2.VideoCapture("video.mp4")
    Cascade = cv2.CascadeClassifier("haarCasade.xml")
    while(True):
         ret, frame = cap.read()
         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
         cv2.imshow('frame',frame)
         if cv2.waitKey(1) & 0xFF == ord('q'):
     breakcap.release()
    cv2.destroyAllWindows()

Solution 4:

use ("%s" %parameter) and do not use ("%s",parameter)

import cv2
import numpy as np
import os
import glob
outputFolder = "picture_output"
videoList = glob.glob("video/*.mp4")
video=videoList[0].split('\\')[1]
cap=cv2.VideoCapture("video/%s" %video)

Solution 5:

This error message indicates that the cv::Mat that you tried to access was not created. In this case, since you are trying to grab frames from a video, the reason could be one of the following:

  1. The path to the video is not correct (on some platforms you might want to try cap = cv2.VideoCapture('G:/3d scanner/2.mmv'))

  2. OpenCV could not retrieve frames from the video due to the lack of support for this format.

Post a Comment for "How To Load A Video In Opencv(python)"