python-3.x 尝试在opencv中从.mov视频读取帧时,出现“找不到起始编号(文件名中)”

2izufjch  于 2023-03-24  发布在  Python
关注(0)|答案(2)|浏览(208)

运行cv.VideoCapture(path)命令时(cv是opencv-python导入)
我收到以下错误:

OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\videoio\src\cap_images.cpp:253: 
error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file):
res\Confetti_Loop.mov in function 'cv::icvExtractPattern'

我已经在3台不同的计算机上测试过它(都是Windows 10)(使用相同的导入),只有一台运行了以下代码,没有错误。

"""Separate thread file to deal with videos."""

from queue import Empty

import cv2 as cv
import pygame

from Constants import LOCAL_IO

def get_video(path, size, queue, max_queue, aspect=0):
    """Get video and add to queue once properly formatted."""

    video = cv.VideoCapture(path)
    if aspect == 0:
        size = keep_aspect_ratio(video, *size)
    elif aspect == 1:
        size = cut(video, *size)
    else:
        size = (int(size[0]), int(size[1]))
    while True:
        try:
            get = queue.get(False)
        except Empty:
            pass
        else:
            if get[0] == LOCAL_IO["Stop"]:
                break

        ret, frame = video.read()
        if not ret:
            video.set(2, 0.0)
            ret, frame = video.read()

        frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
        frame = cv.resize(frame, size)
        frame = frame.swapaxes(0, 1)
        surface = pygame.surfarray.make_surface(frame)
        surface.set_colorkey((0, 0, 0))
        max_queue.put((LOCAL_IO["Video"], surface))

def keep_aspect_ratio(vid, bx, by):
    """Scales image to fit into bx/by, this method will retain the original image's aspect ratio."""

    ix = vid.get(cv.CAP_PROP_FRAME_WIDTH)
    iy = vid.get(cv.CAP_PROP_FRAME_HEIGHT)

    if ix > iy:  # fit to width

        scale_factor = bx / float(ix)
        sy = scale_factor * iy
        if sy > by:
            scale_factor = by / float(iy)
            sx = scale_factor * ix
            sy = by
        else:
            sx = bx
    else:  # fit to height

        scale_factor = by / float(iy)
        sx = scale_factor * ix
        if sx > bx:
            scale_factor = bx / float(ix)
            sx = bx
            sy = scale_factor * iy
        else:
            sy = by

    return int(sx), int(sy)

def cut(vid, bx, by):
    """Scales image without changing aspect ratio but instead growing to at least the size of box bx/by."""

    ix = vid.get(cv.CAP_PROP_FRAME_WIDTH)
    iy = vid.get(cv.CAP_PROP_FRAME_HEIGHT)

    if bx / float(ix) > by / float(iy):  # fit to width

        scale_factor = bx / float(ix)
        sy = scale_factor * iy
        sx = bx

    else:  # fit to height

        scale_factor = by / float(iy)
        sx = scale_factor * ix
        sy = by

    return int(sx), int(sy)

为什么会这样呢?
编辑:
看起来是我从github repo重新下载视频时读取的文件有问题,它工作正常。视频文件是用git lfs上传的,这是在整个repo的第一次下载时损坏的原因吗?

i7uq4tfw

i7uq4tfw1#

根据Olli-Pekka Heinishttps://github.com/opencv/opencv-python/issues/284上。他说:
我能想到的唯一原因是PyInstaller在打包过程中不复制FFmpeg dll。
如果你看一下例如... Python37\Lib\site-packages\cv 2文件夹,在cv2.cp37-win_amd64.pyd旁边有类似opencv_videoio_ffmpeg412_64.dll的东西。
在PyInstaller中创建的包中是否存在opencv_videoio_ffmpeg412_64.dll文件?如果不存在,请将其添加到spec文件(二进制文件):https://pyinstaller.readthedocs.io/en/stable/spec-files.html#spec-file-operation

nqwrtyyt

nqwrtyyt2#

看起来这个错误信息可能会在找不到文件时显示,例如在opencv 4.5.5中

相关问题