Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nshimiye/57bdf2d98b6a42fa83613006b856a52e to your computer and use it in GitHub Desktop.
Save nshimiye/57bdf2d98b6a42fa83613006b856a52e to your computer and use it in GitHub Desktop.
Script to fix opencv homebrew install on OSX > El Capitan

Install Process

Following this: http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/

Verifying

Then tried to verify... with a fail.

$> python
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(./cv2.so, 2): Symbol not found: _PyModule_Create2
  Referenced from: ./cv2.so
  Expected in: flat namespace
 in ./cv2.so

Found a fix

Found this github issue: opencv/opencv#5447. Specifically, this is the comment that allowed me to understand the problem and fix it: opencv/opencv#5447 (comment)

Fix

The python script below was run to fix the problem and then I repeated the verification step above.

import glob
import subprocess
import os.path
ABSPATH = "/usr/local/lib/" # absolute path to relative libraries
# libraries to correct
LIBPATHS = ['/usr/local/lib/python2.7/site-packages/cv2.so', '/usr/local/lib/libopencv*']
PREFIX = 'sudo install_name_tool -change '
#assert(ABSPATH.startswith('/') and ABSPATH.endswith('/'),
# 'please provide absolute library path ending with /')
libs = []
for path in LIBPATHS:
libs += glob.glob(path)
cmd = []
err = []
for lib in libs:
if not os.path.isfile(lib):
err.append(lib+" library not found") # glob should take care
datastr = subprocess.check_output(['otool','-l','-v', lib])
data = datastr.split('\n')
for line in data:
ll = line.split()
if not ll: continue
if (ll[0] == 'name' and ll[1].endswith('.dylib') and not ll[1].startswith('/')):
libname = ll[1].split('/')[-1]
if os.path.isfile(ABSPATH+libname):
cmd.append(PREFIX+ll[1]+" "+ABSPATH+libname+' '+lib)
else:
err.append(ABSPATH+libname+" does not exist, hence can't correct: "+ll[1]+" in: "+lib)
ohandle = open("rpathChangeCmd.txt", 'w')
for lib in cmd:
ohandle.write(lib+'\n')
ohandle.close()
if err:
ehandle = open("rpathChangeErr.txt", 'w')
for e in err:
ehandle.write(e+'\n')
ehandle.close()
@nshimiye
Copy link
Author

# For macOS Majove
# libraries to correct
LIBPATHS = ['/usr/local/lib/python2.7/site-packages/cv2/python-2.7/cv2.so', '/usr/local/lib/libopencv*'] 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment