Macでopencv-python

ブログにあったちょっと前(2020/01/25)の記事を転載。

---

pythonで画像認識を思い、環境を構築しようとしたところ詰まったので構築メモ。
python環境は`pipenv`を使っています。

# opencvインストール
```bash
$ pipenv install opencv-python

Installing opencv-python…
Adding opencv-python to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✘ Locking Failed! 
...(中略)
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches opencv-python
[pipenv.exceptions.ResolutionFailure]:       No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches opencv-python
No versions found
Was https://pypi.org/simple reachable?
...(中略)
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches opencv-python
[pipenv.exceptions.ResolutionFailure]:       No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches opencv-python
No versions found
Was https://pypi.org/simple reachable?
```

大量のエラー。
メッセージに従って

```bash
$ pipenv lock --clear
```

 も無意味。
 
別の案内に従って

```bash
$ pipenv install --skip-lock opencv-python
Installing opencv-python…
Adding opencv-python to Pipfile's [packages]…
✔ Installation Succeeded 
Installing dependencies from Pipfile…
An error occurred while installing opencv-python! Will try again.
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1/1 — 00:00:08
Installing initially failed dependencies…
...(中略)
[pipenv.exceptions.InstallError]: ['Could not fetch URL https://pypi.org/simple/opencv-python/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=\'pypi.org\', port=443): Max retries exceeded with url: /simple/opencv-python/ (Caused by SSLError("Can\'t connect to HTTPS URL because the SSL module is not available.")) - skipping']
...(略)
```

`"Can\'t connect to HTTPS URL because the SSL module is not available."`
なるほどー。
ナニコレ。

(参考)[pip install でSSL関連のエラーが出たらこれを読め！！！](https://qiita.com/kj455/items/9c50b5457631850b3bca)
pythonを最新にしたらいいらしい。
今インストールされているのは3.7.1。
現在(2020/01/25)の最新版は3.8.1なので、入れ直そう。

```bash
$ rm -rf Pipenv* .venv
$ pipenv --python 3.8.1
$ pipenv shell
$ pipenv install opencv-python
Installing opencv-python…
Adding opencv-python to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (ee14b3)!
Installing dependencies from Pipfile.lock (ee14b3)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2/2 — 00:00:02
```

できたわー。