こちらのサイトを参考にインストール。pyton3内で動作していることまで確認できています。
http://mashup.hatenablog.com/entry/2017/05/17/200000
MacBook-Pro% python3 Python 3.6.4 (default, Feb 7 2018, 16:10:42) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.4.0' >>>
こちらのページを参考にコードを書きました。
https://teratail.com/questions/96750
python
1# coding: UTF-8 2import cv2 3import numpy as np 4 5 6def main(): 7 # 入力画像とテンプレート画像をで取得 8 img = cv2.imread("i1.png") 9 temp = cv2.imread("temp.png") 10 11 # グレースケール変換 12 gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) 13 temp = cv2.cvtColor(temp, cv2.COLOR_RGB2GRAY) 14 15 # テンプレート画像の高さ・幅 16 h, w = temp.shape 17 18 # テンプレートマッチング(OpenCVで実装) 19 match = cv2.matchTemplate(gray, temp, cv2.TM_SQDIFF) 20 min_value, max_value, min_pt, max_pt = cv2.minMaxLoc(match) 21 pt = min_pt 22 23 # テンプレートマッチングの結果を出力 24 cv2.rectangle(img, (pt[0], pt[1] ), (pt[0] + w, pt[1] + h), (0,0,200), 3) 25 cv2.imwrite("output.png", img) 26 27 28if __name__ == "__main__": 29 main()
しかし ブラウザには 500 error が発生します。
Apacheのログを確認すると次のようになっていました。
[Wed Feb 07 17:35:17 2018] [error] [client ::1] mod_wsgi (pid=1501): Target WSGI script '/Users/myname/web/python_test/test.py' cannot be loaded as Python module. [Wed Feb 07 17:35:17 2018] [error] [client ::1] mod_wsgi (pid=1501): Exception occurred processing WSGI script '/Users/myname/web/python_test/test.py'. [Wed Feb 07 17:35:17 2018] [error] [client ::1] Traceback (most recent call last): [Wed Feb 07 17:35:17 2018] [error] [client ::1] File "/Users/myname/web/python_test/test.py", line 2, in <module> [Wed Feb 07 17:35:17 2018] [error] [client ::1] import cv2 [Wed Feb 07 17:35:17 2018] [error] [client ::1] ImportError: No module named cv2
こちらのサイト参考に上部を下記のように変更しましたが、解決せず
https://qiita.com/best_not_best/items/6891e7adfa7a421fd559
python
1# coding: UTF-8 2import sys 3sys.path.append('/usr/local/lib/python3.6/site-packages') 4import cv2 5import numpy as np
brew doctorでWarningが出ていたところは可能限り修正しました。
Please note that these warnings are just used to help the Homebrew maintainers with debugging if you file an issue. If everything you use Homebrew for is working fine: please don't worry or file an issue; just ignore this. Thanks! Warning: Unbrewed header files were found in /usr/local/include. If you didn't put them there on purpose they could cause problems when building Homebrew formulae, and may need to be deleted. Unexpected header files: /usr/local/include/node/android-ifaddrs.h ... (文字数制限のため省略) ... /usr/local/include/node/zlib.h Warning: You have unlinked kegs in your Cellar Leaving kegs unlinked can lead to build-trouble and cause brews that depend on those kegs to fail to run properly once built. Run `brew link` on these: postgresql php55 Warning: Some keg-only formula are linked into the Cellar. Linking a keg-only formula, such as gettext, into the cellar with `brew link <formula>` will cause other formulae to detect them during the `./configure` step. This may cause problems when compiling those other formulae. Binaries provided by keg-only formulae may override system binaries with other strange results. You may wish to `brew unlink` these brews: readline
postgresql
php55
readline
はエラー指示通りに行って(エラーが出ればその指示に従って)も解決できませんでした。
この ImportError: No module named cv2
が解決できずに困っています。
お力お貸しください。よろしくお願いします。
【追記】 2018-02-08 11:40
MAMP PRO で環境を構築しています。
管理画面でPythonを見ると 2.7.13 で動いていて、ローカルは /Applications/MAMP/Library/bin/python
と言うことがわかりました。
インストールの参照先は python3
にのみインストールしたとの記載があったので $ brew install opencv3
とオプションを付けずにインストール。ここまではエラーもなく処理できました。
この後、どのようにすればよいのかが分からずにいます。
cv2.so のコピー先がわからないのですが…。
あなたの回答
tips
プレビュー