简体   繁体   English

尝试从 opencv 中的 .mov 视频读取帧时“找不到起始编号(以文件名)”

[英]“Can't find starting number (in the name of file)” when trying to read frames from .mov video in opencv

When running the command cv.VideoCapture(path) (with cv being an opencv-python import)运行命令cv.VideoCapture(path)时(cv 是 opencv-python 导入)

I receive the following error:我收到以下错误:

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'

I have tested it on 3 different computers (all Windows 10) (with same imports) and only one ran the following code with no errors.我已经在 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)

Why is this happening?为什么会这样?

Edit:编辑:

Seems to be a problem with the file I am trying to read as I re-downloaded the video from my github repo and it worked.当我从我的 github 存储库重新下载视频时,我试图读取的文件似乎存在问题,并且它有效。 The video file was uploaded using git lfs would this be a reason why it got corrupted during the first download of the whole repo?视频文件是使用 git lfs 上传的,这会是它在第一次下载整个 repo 时损坏的原因吗?

According to Olli-Pekka Heinis on https://github.com/opencv/opencv-python/issues/284 .根据Olli-Pekka Heinishttps://github.com/opencv/opencv-python/issues/284上的说法。 He says:他说:

Only reason I can think of is that PyInstaller does not copy the FFmpeg dll during the packaging process.我能想到的唯一原因是 PyInstaller 在打包过程中没有复制 FFmpeg dll。

If you have a look at eg ...Python37\Lib\site-packages\cv2 folder, there is something like opencv_videoio_ffmpeg412_64.dll next to cv2.cp37-win_amd64.pyd.如果您查看例如 ...Python37\Lib\site-packages\cv2 文件夹,在 cv2.cp37-win_amd64.pyd 旁边有类似 opencv_videoio_ffmpeg412_64.dll 的东西。

Does the opencv_videoio_ffmpeg412_64.dll file exist in the package which was created in PyInstaller?在 PyInstaller 中创建的 package 中是否存在 opencv_videoio_ffmpeg412_64.dll 文件? If not, add it to the spec file (binaries): https://pyinstaller.readthedocs.io/en/stable/spec-files.html#spec-file-operation如果没有,请将其添加到规范文件(二进制文件): https://pyinstaller.readthedocs.io/en/stable/spec-files.html#spec-file-operation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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