Python cv2 macOS - eiichiromomma/CVMLAB GitHub Wiki

(Python OpenCV) macOS cv2

※2017/9/28時点の情報

Webcamでのエラー

conda + macOSでopencv3導入しようとしてWebcamが使えない状況になった。 python自体のバージョンも3.4, 3.5と試してAnaconda Cloudから都度macOS対応のを探して入れてみたが下のエラーが発生する。(その間カメラのLEDが点滅) 自前でビルドした3.3-devは別マシンでpython3.6でも動いているが、menpoはOpenCV 3.2までしか無いので他のを探す。

>>> import cv2
>>> cam = cv2.VideoCapture(0)
2017-09-26 23:30:25.992 python[1702:21036] -[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x102005790
2017-09-26 23:30:26.001 python[1702:21036] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x102005790'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fffa37272cb __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fffb853f48d objc_exception_throw + 48
	2   CoreFoundation                      0x00007fffa37a8f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x00007fffa3699755 ___forwarding___ + 1061
	4   CoreFoundation                      0x00007fffa36992a8 _CF_forwarding_prep_0 + 120
	5   Foundation                          0x00007fffa5130e0f __NSFireTimer + 83
	6   CoreFoundation                      0x00007fffa36a6c54 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
	7   CoreFoundation                      0x00007fffa36a68df __CFRunLoopDoTimer + 1071
	8   CoreFoundation                      0x00007fffa36a643a __CFRunLoopDoTimers + 298
	9   CoreFoundation                      0x00007fffa369db81 __CFRunLoopRun + 2065
	10  CoreFoundation                      0x00007fffa369d114 CFRunLoopRunSpecific + 420
	11  Foundation                          0x00007fffa50b0252 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 277
	12  Foundation                          0x00007fffa51409b5 -[NSRunLoop(NSRunLoop) runUntilDate:] + 111
	13  libopencv_videoio.3.1.0.dylib       0x00000001040c6bb1 _ZN12CvCaptureCAM9grabFrameEd + 241
	14  libopencv_videoio.3.1.0.dylib       0x00000001040c67dc _ZN12CvCaptureCAM18startCaptureDeviceEi + 956
	15  libopencv_videoio.3.1.0.dylib       0x00000001040c619b _Z24cvCreateCameraCapture_QTi + 107
	16  libopencv_videoio.3.1.0.dylib       0x00000001040bae33 _ZN2cv12VideoCapture4openEi + 211
	17  libopencv_videoio.3.1.0.dylib       0x00000001040ba8d1 _ZN2cv12VideoCaptureC1Ei + 65
	18  cv2.so                              0x0000000102319a4a _ZL37pyopencv_cv_VideoCapture_VideoCaptureP7_objectS0_S0_ + 874
	19  libpython3.4m.dylib                 0x00000001000c7f7e PyEval_EvalFrameEx + 22206
	20  libpython3.4m.dylib                 0x00000001000c26f6 PyEval_EvalCodeEx + 2342
	21  libpython3.4m.dylib                 0x00000001000c1dbf PyEval_EvalCode + 63
	22  libpython3.4m.dylib                 0x00000001000f0d96 PyRun_InteractiveOneObject + 582
	23  libpython3.4m.dylib                 0x00000001000f069e PyRun_InteractiveLoopFlags + 222
	24  libpython3.4m.dylib                 0x00000001000f052b PyRun_AnyFileExFlags + 139
	25  libpython3.4m.dylib                 0x000000010010804f Py_Main + 3151
	26  python                              0x0000000100001dc8 main + 216
	27  python                              0x0000000100001ce4 start + 52
	28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

SmartAgにOpenCV 3.3がmacOS対応であったのでそっちを使ってみる。

conda config --add channels SmartAg
conda install -c SmartAg opencv3

今度はWebcamは問題ないもののlibopenblaspのエラー。

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/username/anaconda/envs/ocvdlib/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so, 2): Library not loaded: @rpath/libopenblasp-r0.2.19.dylib
  Referenced from: /Users/username/anaconda/envs/ocvdlib/lib/libopencv_core.3.3.0.dylib
  Reason: image not found

これはissueが出てたので conda-forgeを指定して入れてやれば良い。

conda install -c conda-forge openblas=0.2.19

で、ようやく動いた。

⚠️ **GitHub.com Fallback** ⚠️