8

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?

Community
  • 1
  • 1
Magsol
  • 4,640
  • 11
  • 46
  • 68
  • @Magsot, any luck on the issue? – Hong Mar 22 '17 at 19:15
  • @Hong Nope :( I thought it might have had something to do with prereqs before Anaconda install, but it doesn't seem to matter. – Magsol Mar 23 '17 at 18:58
  • @Magsot, according to my research, it seems to be relevant to the lack of ffmpeg of opencv. Lots of discussion on compiling opencv with ffmpeg to resolve this problem. Do you ever try that route? – Hong Mar 23 '17 at 19:14
  • @Hong I saw that solution too (it's listed in my question), but the problem is I'd like to solve it without having to build OpenCV from scratch. Rather, I'd like to be able to use menpo's conda build to install OpenCV. – Magsol Mar 23 '17 at 19:49
  • do you try this: https://github.com/menpo/conda-opencv3? – Hong Mar 23 '17 at 19:54
  • @Hong OH, that's the GitHub repo with instructions! Let me have a look... – Magsol Mar 23 '17 at 20:04
  • please let me know whether that method works on your machine. It doesn't work here. I'm trying other method. – Hong Mar 24 '17 at 13:45
  • I'm running into some OpenCV build errors. Trying to diagnose. – Magsol Mar 24 '17 at 14:21
  • @Hong See my answer below; used the newer conda-forge feedstock for OpenCV, though with a slightly custom build script, and it worked just fine. – Magsol Mar 31 '17 at 13:36

2 Answers2

3

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.

Magsol
  • 4,640
  • 11
  • 46
  • 68
-1

I faced the same problem with Anaconda private env & Python 3.5 on Ubuntu 16.04 .

Initially installed Opencv3 using

conda install -c menpo opencv3

Solution:

  1. Remove Opencv3 conda remove opencv3

  2. Install Opencv3 using pip install opencv-contrib-python

If the problem still persists:

  1. Uninstall OpenCV
  2. Install dependencies sudo apt-get install ffmpeg sudo apt-get install libavcodec-dev libavformat-dev libavdevice-dev
  3. Reinstall Opencv using step 2.
  • 1
    DO NOT USE THE `menpo` OPENCV BUILD. The menpo channel's opencv package is unmaintained. See their `README` commit from Oct of 2017: https://github.com/menpo/conda-opencv3 . However, the `conda-forge` opencv build has been considerably improved from the time of the original post, so that should be a viable alternative. – Magsol Feb 05 '18 at 14:59