Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

【Anaconda-python3.5】Ubuntu16.04でcv2.VideoCapture()が使えないとき

More than 1 year has passed since last update.

環境

os:Ubuntu16.04
python:python3.5(Anaconda4.2.0) 64bit
https://qiita.com/neet-AI/items/f5f4aba3bfefef7a7d10
*Jupyternotebookを使用

問題

import cv2
cap = cv2.VideoCapture("../input/sora.avi")
print(cap.read())
結果
(False, None)

動画ファイルまでのパスはあっている.
 →opencvがうまく入っていない?!

参考

https://qiita.com/nakasuke_/items/51664807bdd7794db0da
https://teratail.com/questions/58787
https://askubuntu.com/questions/564290/how-to-uninstall-opencv-2-4-9-completely-from-ubuntu

解決手段

全てのopencvを削除

$ sudo find / -name "*opencv*" -exec rm -r {} \;
$ conda uninstall opencv

requirementをインストール

$ apt-get install git libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev pkg-config cmake libgtk2.0-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavcodec-dev libavutil-dev libavfilter-dev libavformat-dev libavresample-dev

openCVをダウンロード

$ mkdir opencv && cd opencv
$ wget https://github.com/opencv/opencv/archive/3.3.0.tar.gz

展開

$ tar xvf 3.3.0.tar.gz

Cmakeの実行

$ mkdir opencv-3.3.0/build && cd opencv-3.3.0/build
$ cmake -DBUILD_FFMPEG=ON -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python3) -DPYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

build

$ make -j8
$ make install

結果

import cv2
cap = cv2.VideoCapture("../input/sora.avi")
print(cap.read())
結果
(True, array([[[155,  50,  19],
        [155,  50,  19],
        [156,  51,  20],
        ..., 
        [172,  76,  51],
        [172,  75,  51],
        [172,  75,  51]],

       [[155,  50,  19],
        [155,  50,  19],
        [156,  51,  20],
        ..., 
        [172,  76,  51],
        [172,  75,  51],
        [172,  75,  51]],

       [[155,  50,  19],
        [155,  50,  19],
        [156,  51,  20],
        ..., 
        [173,  77,  52],
        [173,  76,  52],
        [173,  76,  52]],

       ..., 
       [[195, 218, 237],
        [195, 218, 237],
        [196, 220, 238],
        ..., 
        [107, 143, 191],
        [107, 143, 192],
        [107, 143, 192]],

       [[194, 217, 236],
        [195, 218, 237],
        [196, 219, 238],
        ..., 
        [107, 143, 192],
        [107, 143, 192],
        [107, 143, 192]],

       [[194, 217, 236],
        [195, 218, 237],
        [196, 219, 238],
        ..., 
        [108, 145, 190],
        [108, 145, 190],
        [108, 145, 190]]], dtype=uint8))

ちゃんと読み込めた.

aj1m0n
機械学習&組み込みを中心に開発と研究を行っています。
https://aj1m0n.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away