我只是从源代码构建了opencv,但似乎没有python绑定(bind)。

(ocv) me@vm:~$ python
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'
>>> exit()
(ocv) me@vm:~$ sudo ldconfig -p | grep libopencv
  libopencv_videostab.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_videostab.so.3.0
  libopencv_videostab.so (libc6,x86-64) => /usr/local/lib/libopencv_videostab.so
  libopencv_videoio.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_videoio.so.3.0
  libopencv_videoio.so (libc6,x86-64) => /usr/local/lib/libopencv_videoio.so
  libopencv_video.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_video.so.3.0
  libopencv_video.so (libc6,x86-64) => /usr/local/lib/libopencv_video.so
  libopencv_superres.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_superres.so.3.0
  libopencv_superres.so (libc6,x86-64) => /usr/local/lib/libopencv_superres.so
  libopencv_stitching.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_stitching.so.3.0
  libopencv_stitching.so (libc6,x86-64) => /usr/local/lib/libopencv_stitching.so
  libopencv_shape.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_shape.so.3.0
  libopencv_shape.so (libc6,x86-64) => /usr/local/lib/libopencv_shape.so
  libopencv_photo.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_photo.so.3.0
  libopencv_photo.so (libc6,x86-64) => /usr/local/lib/libopencv_photo.so
  libopencv_objdetect.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_objdetect.so.3.0
  libopencv_objdetect.so (libc6,x86-64) => /usr/local/lib/libopencv_objdetect.so
  libopencv_ml.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_ml.so.3.0
  libopencv_ml.so (libc6,x86-64) => /usr/local/lib/libopencv_ml.so
  libopencv_imgproc.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_imgproc.so.3.0
  libopencv_imgproc.so (libc6,x86-64) => /usr/local/lib/libopencv_imgproc.so
  libopencv_imgcodecs.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_imgcodecs.so.3.0
  libopencv_imgcodecs.so (libc6,x86-64) => /usr/local/lib/libopencv_imgcodecs.so
  libopencv_highgui.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_highgui.so.3.0
  libopencv_highgui.so (libc6,x86-64) => /usr/local/lib/libopencv_highgui.so
  libopencv_flann.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_flann.so.3.0
  libopencv_flann.so (libc6,x86-64) => /usr/local/lib/libopencv_flann.so
  libopencv_features2d.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_features2d.so.3.0
  libopencv_features2d.so (libc6,x86-64) => /usr/local/lib/libopencv_features2d.so
  libopencv_core.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_core.so.3.0
  libopencv_core.so (libc6,x86-64) => /usr/local/lib/libopencv_core.so
  libopencv_calib3d.so.3.0 (libc6,x86-64) => /usr/local/lib/libopencv_calib3d.so.3.0
  libopencv_calib3d.so (libc6,x86-64) => /usr/local/lib/libopencv_calib3d.so
(ocv) me@vm:~/opencv-in-python$ uname -a
Linux vm 4.10.0-38-generic #42~16.04.1-Ubuntu SMP Tue Oct 10 16:32:20 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

然后这就是我从源代码构建的方式,主要遵循the website的指导:
pushd /tmp
wget https://github.com/opencv/opencv/archive/3.1.0.zip
unzip 3.1.0.zip
mkdir opencv-3.1.0
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
make -j $(nproc)
popd

最佳答案

我忘了包括opencv_contrib项目,并确保它与opencv项目在同一发行版中。我也忘记了共享对象文件的符号链接(symbolic link)。

cd ~/.virtualenvs/cv/lib/python3.5/site-packages
ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

在那之后,我的导入工作:
(ocv) me@vm:~$ python -c "import cv2"
(ocv) me@vm:~$

关于python-3.x - 从源代码构建后无法导入cv2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47315645/

10-12 14:28