Opencv error solution 1 - OpenCV (4.5.4-dev): error: CAP_IMAGES: can't find starting number: 'cv::icvExtractPattern

This error took me more than six hours, I hope my blog can help you save this time.

When writing scripts every day, such an error occurred:

OpenCV(4.5.4-dev) D:\a\opencv-python\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/Desktop/test in function 'cv::icvExtractPattern'

I checked a lot of similar error solutions on the Internet, and tried almost all of them. I uninstalled and reinstalled opencv many times, but it didn't work, and sometimes even an error at the end appeared cv::imwrite. I didn't find a solution to this error from nine o'clock in the evening until three thirty in the morning.

First, my code is as follows:

import cv2
import os

times = 1  # 提取的帧记数
sourceFileName = '01_11__talking_against_wall__9229VVZ3'
frame_frequency = 50  # 提取视频的帧率,即每1帧提取几个视频
file_path = 'videoTest/' + sourceFileName + '/'
video_path = "C:/Users/Desktop/test"  # 读取视频的文件目录
if not os.path.exists(file_path):  # 如果文件路径不存在就创建文件夹
    os.makedirs(file_path)
camera = cv2.VideoCapture(video_path)
while True:
    times = times+1
    res, image = camera.read()
    if not res:
        print('not res , not image')
        break
    if times % frame_frequency == 0:
        cv2.imwrite(file_path + "/" + str(times) + ".jpg", image)
        times = times + 1  # 记录存储照片的次数
    camera.release()

After poking around for me for a long time, I finally found out what my problem was. I was video_pathmisled by this variable name, thinking that I only need to pass in the path where the video file is located , but in fact, cv2.VideoCapturewhat I want to receive is the path where the video is located + video name/format , modify the 8th line of the above proxy purchase to:video_path = "C:/Users/11304/Desktop/test/01_11__talking_against_wall__9229VVZ3.mp4"

The original error is resolved and it can run normally.

There are many solutions on the Internet. Since I have basically read them all, I will summarize them here for the convenience of everyone. I found that most of the errors in this similar format of opencv are caused by the following reasons:

(1) If the file directory or video name contains Chinese, it must be changed to English

(2) The picture or video does not exist

(3) The version of opencv does not correspond to that of python. The opencv version used pipor condainstalled defaults to the latest version, but in fact opencv and python have a relatively strict version correspondence. Here I am attaching the download link:

https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/opencv-python/

The number after cp in it refers to the python version; windows system downloads win, Linux, mac and so on; X64, X32 is the number of digits of the computer, you can check the digits of your computer yourself, use X64 for 64 bits, and 32 bits Use X32.

After downloading, put the downloaded whl into the Lib/sitepackage of the python corresponding environment, which is specially used to store some libraries of python, and then open the cmd in the corresponding folder and install it with the command:pip install 文件名

Here is the official website of opencv, which can help you solve some common installation error problems: https://pypi.org/project/opencv-python/

At that time, I was inspired to solve this problem by a foreign brother's answer, that is, it may not be the problem of opencv installation, but my original code made an error, so I went to the Internet to find someone else's code with similar functions to me. After comparison, it took six hours to find out the reason. I hope it can help you save this time.

Guess you like

Origin blog.csdn.net/m0_61787307/article/details/129654826