質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Q&A

解決済

1回答

11316閲覧

OpenCVのインストールができない

退会済みユーザー

退会済みユーザー

総合スコア0

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

0グッド

0クリップ

投稿2018/10/02 13:04

編集2018/10/02 13:05

前提・実現したいこと

プログラミングの初心者です。Openposeの構築のために必要なOpenCVの実装を目的としています。

発生している問題・エラーメッセージ

pythonを起動してOpenCVのバージョン確認をしようとましたが、以下のようなエラーメッセージが出てしまいます。

エラーメッセージ

>>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'cv2' >>> cv2.__version__ Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cv2' is not defined

該当のソースコード

apt-get install libopencv-dev

試したこと

一度opencvと書かれたファイルをすべて消去したのちに、
https://www.trifields.jp/how-to-install-opencv-on-ubuntu-2742
以上のサイトにしたがって設定まで終えてみましたが、結果は同様でした。

補足情報(FW/ツールのバージョンなど)

Ubuntu 14.04
Python 3.4.3

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

apt-get install libopencv-devだと C++ 版の OpenCV しかインストールされませんので、Python では使えません。

Python で使いたい場合は、pip でインストールするのが簡単でおすすめです。

bash

1pip install opencv-python

追記

こちらは opencv が使えない原因とは関係ないように思えます。

The directory '/home/deepstation/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

/home/deepstation/.cache/pip は現在の実行ユーザーが権限を持ってないので、キャッシュは利用できないと言っています。
sudo -H とした場合、${HOME} 環境変数の値を現在実行しているユーザーの HOME ディレクトリから root ユーザーのホームディレクトリに変更します。

Successfully installed opencv-python-3.4.3.18

インストール自体は成功しているように思えます。
なので原因として考えられるのは、opencv-python 自体はインストールできたが、パスが通ってなくて見るからないとなっているのではないでしょうか。
site-package のパスを以下のように設定してみてください。

~/.bashrc とかにかいて、source ~/.bashrc

export PYTHONPATH="<site-packages のパス>:$PYTHONPATH"

場所がわからなければ、which pip で pip を利用している Python 環境の場所を調べてください。


Ubuntu の Python 環境について

python 2/3 は別の環境です

$ which pip /usr/local/bin/pip $ which pip3 /usr/local/bin/pip3 $ which python /usr/bin/python $ which python3 /usr/bin/python3

つまり、Python 3 環境を使いたい場合は、

  • モジュールは pip3 でインストールする。この場合、Python 3環境にモジュールがインストールされます。

*スクリプトは python3 test.py のように実行します。

一方 Python 2 を使いたい場合は、

  • モジュールは pip でインストールする。この場合、Python 2環境にモジュールがインストールされます。

*スクリプトは python test.py のように実行します。

Python 2/3 は環境が別なので、pip install python-opencv としても、Python 2のほうにはモジュールがインストールされるので、Python 3 のほうでは使えません。

opencvが動作しないpythonはアンインストールした方がよろしいでしょうか。

削除する必要はありません。共存可能です。

投稿2018/10/02 13:49

編集2018/10/03 05:48
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/10/03 02:52 編集

ありがとうございます。御教授いただいたコマンドを入力したのちに再度python上でバージョンを確認するコマンドを入力して見ましたが、インストールする前と同じ結果になってしまいました。 ```bash deepstation@dBeepstation:~$ sudo pip install opencv-python [sudo] password for deepstation: The directory '/home/deepstation/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/home/deepstation/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting opencv-python /usr/local/lib/python2.7/dist-packages/pip/_vendor/urllib3/util/ssl_.py:369: SNIMissingWarning: An HTTPS request has been made, but the SNI (Server Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings SNIMissingWarning /usr/local/lib/python2.7/dist-packages/pip/_vendor/urllib3/util/ssl_.py:160: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning /usr/local/lib/python2.7/dist-packages/pip/_vendor/urllib3/util/ssl_.py:160: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning Downloading https://files.pythonhosted.org/packages/51/3d/9b10d7159e10ac87bd6c1b403dbc72b1240e18da51f9d56f50bb06f75d15/opencv_python-3.4.3.18-cp27-cp27mu-manylinux1_x86_64.whl (25.0MB) 100% |████████████████████████████████| 25.0MB 2.1MB/s Requirement already satisfied: numpy>=1.11.1 in /usr/local/lib/python2.7/dist-packages (from opencv-python) (1.11.2) Installing collected packages: opencv-python Successfully installed opencv-python-3.4.3.18 ``` ```Python >>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'cv2' >>> cv2.__version__ Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cv2' is not defined ``` ターミナルに出現したメッセージによるとsudoに-Hflagが必要と記されていますが、メッセージの意味を理解することができませんでした。 追加の疑問になってしまいましたが、ご教示ゅいただけると幸いです。
tiitoi

2018/10/03 03:13

追記しました。`which pip` でどのパスが表示されますか?
退会済みユーザー

退会済みユーザー

2018/10/03 05:39

追記で回答していただきありがとうございます。 現環境にpython 3.4.18の他にpython2.7.6があるのですが、いただいたコマンドを入力してもpython3.4.18の方では同様のエラーメッセージが表示されました。しかしpython2.7.6ではopencvのバージョン情報が表示され、サンプル画像を用いた動作の確認もとれました。pythonのバージョンによって動作しないことが考えられるのでしょうか。また、この後openpcvが必須とされるopenposeの構築をする予定なのですが、opencvが動作しないpythonはアンインストールした方がよろしいでしょうか。何度もお聞きして申し訳ございません。
退会済みユーザー

退会済みユーザー

2018/10/03 05:40

which pipでは /usr/local/bin/pip が表示されました。
tiitoi

2018/10/03 05:49

追記しました。 Python 3 環境で使いたい場合は、pip3 install opencv-python でインストールします。 コマンドが見つからないと出てきたら、apt-get install python3-pip で pip3 をさきに入れてください。
退会済みユーザー

退会済みユーザー

2018/10/03 05:52

ありがとうございます。 deepstation@deepstation:~$ pip3 install opencv-python Downloading/unpacking opencv-python Could not find any downloads that satisfy the requirement opencv-python Cleaning up... No distributions at all found for opencv-python Storing debug log for failure in /home/deepstation/.pip/pip.log と赤文字で出現しました。
tiitoi

2018/10/03 05:55

パッケージリストが古いかもしれないので、pip3 install --upgrade pip として、更新してからもう一度試してください。
退会済みユーザー

退会済みユーザー

2018/10/03 06:03

ありがとうございます。 pip3 install --upgrade pipを実行したところ、以下のようなエラーメッセージが出ました。 deepstation@deepstation:~$ pip3 install --upgrade pip Downloading/unpacking pip from https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl#sha256=070e4bf493c7c2c9f6a08dd797dd3c066d64074c38e9e8a0fb4e6541f266d96c Downloading pip-18.0-py2.py3-none-any.whl (1.3MB): 1.3MB downloaded Installing collected packages: pip Found existing installation: pip 1.5.4 Not uninstalling pip at /usr/lib/python3/dist-packages, owned by OS Can't roll back pip; was not uninstalled Cleaning up... Exception: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 283, in run requirement_set.install(install_options, global_options, root=options.root_path) File "/usr/lib/python3/dist-packages/pip/req.py", line 1436, in install requirement.install(install_options, global_options, *args, **kwargs) File "/usr/lib/python3/dist-packages/pip/req.py", line 672, in install self.move_wheel_files(self.source_dir, root=root) File "/usr/lib/python3/dist-packages/pip/req.py", line 902, in move_wheel_files pycompile=self.pycompile, File "/usr/lib/python3/dist-packages/pip/wheel.py", line 206, in move_wheel_files clobber(source, lib_dir, True) File "/usr/lib/python3/dist-packages/pip/wheel.py", line 193, in clobber os.makedirs(destsubdir) File "/usr/lib/python3.4/os.py", line 237, in makedirs mkdir(name, mode) PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/pip' Storing debug log for failure in /home/deepstation/.pip/pip.log sudoで実行したところ、 deepstation@deepstation:~$ sudo pip3 install --upgrade pip [sudo] password for deepstation: Downloading/unpacking pip from https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl#sha256=070e4bf493c7c2c9f6a08dd797dd3c066d64074c38e9e8a0fb4e6541f266d96c Downloading pip-18.0-py2.py3-none-any.whl (1.3MB): 1.3MB downloaded Installing collected packages: pip Found existing installation: pip 1.5.4 Not uninstalling pip at /usr/lib/python3/dist-packages, owned by OS Successfully installed pip Cleaning up... と表示されてしまいました。その後pip3 install opencv-pythonのコマンドを実行して見ましたが、同様のエラーが発生してしまいました。
tiitoi

2018/10/03 06:20

`Could not find any downloads that satisfy the requirement opencv-python` というのは、指定したパッケージ名が見つからないということを意味しています。`pip3 search opencv-python` でダウンロード可能なパッケージ一覧にあるかどうか調べられます。 ``` sudo pip3 install --upgrade pip pip3 install opencv-python ``` でインストールできると思うんですが、それでできないのであれば、ちょっとわからないです。
退会済みユーザー

退会済みユーザー

2018/10/03 06:30

何度も回答していただきありがとうございます。apt-get install python3-pipまで試して見ましたが、結果的に以下のようになり、うまくダウンロードできませんでした。 deepstation@deepstation:~$ sudo pip3 install --upgrade pip The directory '/home/deepstation/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/home/deepstation/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Requirement already up-to-date: pip in /usr/local/lib/python3.4/dist-packages (18.0) deepstation@deepstation:~$ pip3 install opencv-python Downloading/unpacking opencv-python Could not find any downloads that satisfy the requirement opencv-python Cleaning up... No distributions at all found for opencv-python Storing debug log for failure in /home/deepstation/.pip/pip.log とりあえずpython2には正常にインストールされているので、openpose構築の前にpython3をアンインストールしてから進んでみようかと考えています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問