19

I'm trying to read frames from a hevc(h265) .avi video in opencv-python (python3, latest version) but keeps throwing

OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): C:\Users\gabri\Desktop\2019-11-22_13\a.avi in function 'cv::icvExtractPattern'.

I've tried both in ubuntu and windows 10 using opencv-python, opencv-contrib-python and opencv-contrib-python-nonfree, but it didn't work. Thank you in advance.

Code used to read the video:

import cv2
import imutils

cap = cv2.VideoCapture("C:\\Users\\gabri\\Desktop\\2019-11-22_13\\a.avi")


while True:
    ret,frame = cap.read()
    if not ret:
        break
    frame = imutils.resize(frame,width = 960)
    cv2.imshow('image',frame)

    k = cv2.waitKey(1) & 0xff

    if k == 27:
       break
Gabriel Macus
  • 444
  • 1
  • 4
  • 9
  • I am not sure if that's your case but you need to set the codec as already answered here : https://stackoverflow.com/questions/27392455/opencv-videocapture-with-h264-codec – Ziri Dec 04 '19 at 03:47
  • 1
    Did you try it with cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('H', '2', '6', '5')); ? – Ziri Dec 04 '19 at 16:54
  • I've had the same error while trying to open a file in the wrong directory, so the error message was completely misleading, should have been "File not found". – mins Feb 18 '21 at 17:18

10 Answers10

17

You can get this error in the VideoWriter class if you do not specify your extension in the file name In my case I have forgotten to write .mp4.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Ghostpunk
  • 197
  • 1
  • 5
7

I fixed this by passing the absolute path into VideoCapture and changing the filename from example.mov to example_1000.mov.

6

this error normally pop-up when video_path in functions cap = cv2.VideoCapture("video_path") or cv2.VideoWriter('path_to_folder/output_save.avi') is not correct
Mostly seen in windows

Prajot Kuvalekar
  • 5,128
  • 3
  • 21
  • 32
5

I had the same problem, compilation and linking ok, but the same cryptic error occurs at running.

It happened (with Windows) when opencv_videoio_ffmpeg430_64.dll was not accessible (it seems to be silently called by another opencv lib). Either you did not build opencv with the -DWITH_FFMPEG=ON, or alternatively your dll is not in the path.

PJ127
  • 986
  • 13
  • 23
  • 3
    I had this same issue (on Ubuntu), which I resolved by adding ffmpeg to my OpenCV build. For future readers: If you build OpenCV yourself, look through the output of cmake for `FFMPEG: YES`. If you see `FFMPEG: NO`, then cmake was not able to locate the development files for ffmpeg. – bitoffdev Feb 17 '21 at 16:38
3

I had the same error and solved it by changing the video name to '001.avi'.

cmj
  • 31
  • 1
2

Maybe you can append a number to your path like 'a2.avi' instead of 'a.avi'.

HsLuoyang
  • 29
  • 1
  • 2
    "*Maybe you can append a number to your path*", maybe you wanted to say "*to your filename*"? – mins Feb 18 '21 at 16:52
  • Consider adding an explanation for making such a change. Also, often the file path will be taken as an input, which we'll have to use as it is regardless of the whatever filename we are given – REVOLUTION Jan 14 '22 at 12:25
  • Why would that fix the problem? – Mehdi Charife Jun 02 '23 at 15:16
0

File extension was missing in my case: changing movie to movie.mp4 solved the issue

Nir
  • 1,618
  • 16
  • 24
0

My use case is probably not what is being asked in the question however since the error message produced by opencv is slightly misleading I'll leave a note for others who might encounter similar problem.

In my case I was receiving following error because the file was corrupted:

OpenCV(4.5.1) cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): /path/to/file.png in function 'icvExtractPattern'

I was receiving files through AWS API gateway and in order to avoid files being corrupted I had to add 'multipart/form-data' within 'Binary Media Types' section of APIGateway app settings.

Greg0ry
  • 931
  • 8
  • 25
0

For me, the problem was the filename needs to have a number on it. So in your case perhaps you can rename your file to a0.avi

Tama
  • 1
  • 1
  • CAP_IMAGES is the last reader module that is tried, meaning the usual modules (for video files and for camera devices) failed to make sense of the file name you passed or the file's content. -- CAP_IMAGES is for _image_ sequences, i.e. numbered file names... for _video files_, no such logic exists. your perceived requirement does not exist and adding numbers to the name of a video file does nothing. – Christoph Rackwitz Sep 20 '22 at 09:46
0

For me, the error occurred before I was able to approve access to the camera. After I approved the use of the camera for the application, it worked.

downeyt
  • 1,206
  • 2
  • 12
  • 23