OpenCV3错误:“无法停止流:设备的不适当的ioctl" [英] OpenCV3 error: "Unable to stop the stream: Inappropriate ioctl for device"

查看:676
本文介绍了OpenCV3错误:“无法停止流:设备的不适当的ioctl"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenCV 3.2读取.mov文件的帧(来自 menpo conda频道).我在Ubuntu 16.04 64位设置上通过Anaconda使用Python 3.5.3.

问题是,当OpenCV遇到cap.read()调用时,我收到以下错误消息,并且循环立即中断并捕获if num == 0条件.

这是我正在运行的全部代码:

import cv2
import numpy as np
import sys

f = sys.argv[1]
cap = cv2.VideoCapture(f)

frames = []
num = 0
while cap.isOpened():
    ret, frame = cap.read()
    if not ret: break
    gframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    frames.append(gframe)
    num += 1
    if num % 100 == 0:
        frames.append(gframe)
    if num % 1000 == 0:
        print(num)

if num == 0:
    print("Something went wrong: no frames found.")
    exit(0)

cap.release()

我在此主题上还发现了其他一些StackOverflow问题,但它们并不能完全反映我的实际情况:

  • 此问题建议重建OpenCV (也使用Python 2).这不是我的选择,因为我正在尝试使用Anaconda进行此操作.
  • 这两个问题(此处此处)没有得到令人满意的答复.
  • 有一个生动的讨论和详尽的答案,但这是针对perl的.

关于第三点-这里还有很多其他问题都带有引号inappropriate ioctl for device,但很难确定其中是否有任何与此问题直接相关的

.

作为一个部分:我已经在macOS机器上安装了这个完全相同的opencv3 conda软件包,并且我粘贴到此处的代码在我在Ubuntu机器上尝试过的完全相同的.mov文件上也可以正常工作

有什么想法吗?

解决方案

通过完全解决问题来解决该问题.

使用OpenCV的 opencv-feedstock 配方进行conda-forge.在recipe文件夹中是conda的生成信息.我修改了build.sh文件,更改了以下选项:

-DBUILD_PNG=1
-DBUILD_JPEG=1
-DWITH_CUDA=1
-DWITH_FFMPEG=1

从父目录中运行conda build --numpy 1.12 recipe(必须指定NumPy版本,因为构建脚本要求列出了numpy x.x,这意味着您必须在运行时提供版本).

永久使用 (严重的是,在一台功能非常强大的计算机上花费了几个小时-耗时为CUDA),但是构建最终成功完成.

然后只需安装本地构建的bz2归档文件(conda install --use-local opencv).不再有怪异的ioctl错误消息,并且上面的脚本可以正常工作.

I'm trying to read the frames of an .mov file using OpenCV 3.2 (from the menpo conda channel). I'm using Python 3.5.3 through Anaconda on an Ubuntu 16.04 64-bit setup.

Problem is, I get the following error message from OpenCV when it hits the cap.read() call, and the loop immediately breaks and catches the if num == 0 conditional.

Here's the entirety of the code I'm running:

import cv2
import numpy as np
import sys

f = sys.argv[1]
cap = cv2.VideoCapture(f)

frames = []
num = 0
while cap.isOpened():
    ret, frame = cap.read()
    if not ret: break
    gframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    frames.append(gframe)
    num += 1
    if num % 100 == 0:
        frames.append(gframe)
    if num % 1000 == 0:
        print(num)

if num == 0:
    print("Something went wrong: no frames found.")
    exit(0)

cap.release()

user@ubuntu:/data$ python read.py movie.mov 
Unable to stop the stream: Inappropriate ioctl for device
Something went wrong: no frames found.
user@ubuntu:/data$

I've found a couple of other StackOverflow questions on this topic, but they don't quite translate to my exact circumstance:

  • This question proposes rebuilding OpenCV (also uses Python 2). That's not an option for me, as I'm trying to do this with Anaconda.
  • These two questions (here and here) on the OpenCV forums were left without any satisfactory answers.
  • This one has a lively discussion and a thorough answer, but it's specific to perl.

To that third point--there are quite a few other questions here that have the quote inappropriate ioctl for device but it's hard to see if any of them is directly relevant to this problem.

As a partial aside: I've installed this exact same opencv3 conda package on my macOS machine, and the code I've pasted here works just fine and on exactly the same .mov file I've tried on the Ubuntu machine.

Any ideas?

解决方案

Solved the problem by getting around it entirely.

Used the opencv-feedstock recipe of OpenCV for conda-forge. In the recipe folder is the build information for conda. I modified the build.sh file, changing the following options:

-DBUILD_PNG=1
-DBUILD_JPEG=1
-DWITH_CUDA=1
-DWITH_FFMPEG=1

ran conda build --numpy 1.12 recipe from the parent directory (had to specify the NumPy version, as the build script requirement lists numpy x.x which means you have to provide the version at runtime), and waited.

Took forever (seriously, a couple hours on a very powerful machine--the time sink is CUDA), but the build eventually completed successfully.

Then it was just a matter of installing the locally-built bz2 archive (conda install --use-local opencv). No more weird ioctl error messages, and the above script worked just fine.

这篇关于OpenCV3错误:“无法停止流:设备的不适当的ioctl"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆