无法在 python 中导入 OpenCV

无法在 python 中导入 OpenCV

过去两天我一直在尝试在 ubuntu 16.04 中安装 OpenCV。我按照各种教程操作,但无法让它工作。现在我正在尝试安装它从这里。. 一切正常,直到指令

cmake .

该指令产生输出

kvs@Hunter:~/Downloads/OpenCV_Capstone/opencv-3.3.0/samples$ cmake .
-- OpenCV ARCH: 
-- OpenCV RUNTIME: 
-- OpenCV STATIC: ON
CMake Warning at /home/kvs/opencv-3.0.0/cmake/OpenCVConfig.cmake:166 (message):
  Found OpenCV Windows Pack but it has no binaries compatible with your
  configuration.

  You should manually point CMake variable OpenCV_DIR to your build of OpenCV
  library.
Call Stack (most recent call first):
  CMakeLists.txt:55 (find_package)


CMake Error at CMakeLists.txt:55 (find_package):
  Found package configuration file:

    /home/kvs/opencv-3.0.0/cmake/OpenCVConfig.cmake

  but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
  NOT FOUND.


-- Configuring incomplete, errors occurred!
See also "/home/kvs/Downloads/OpenCV_Capstone/opencv-3.3.0/samples/CMakeFiles/CMakeOutput.log".

下面是前面的指令cmake及其输出

$ pkg-config --libs opencv

kvs@Hunter:~/Downloads/OpenCV_Capstone$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

错误中写了一些我应该做什么的信息,但我对此了解有限。我该如何消除错误?

请注意,本教程使用的是 OpenCV 3.2.0,而我尝试安装的是 OpenCV 3.3.0。这可能是问题的原因吗?此外,如何从系统中删除 opencv 及其所有依赖项?Synaptic 包管理器告诉我,在删除任何内容之前,需要修复损坏的包,但当我搜索这些损坏的包时,什么也没有出现。

2.我也尝试过

sudo apt-get install libopencv-dev

输出

kvs@Hunter:~$ sudo apt-get install libopencv-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libopencv-dev is already the newest version (2.4.9.1+dfsg-1.5ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

但我仍然无法在控制台中导入该库

>>> from cv2.cv import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

3.正如评论中提到的,我也尝试了这些说明Ubuntu 文档按照评论中的建议。运行脚本后,OpenCV 3.3.0 已安装。

 Done. The new package has been installed and saved to

 /home/kvs/OpenCV/OpenCV/opencv-3.3.0/build/build_20170906-1_amd64.deb

 You can remove it from your system anytime using: 

      dpkg -r build

但我仍然无法在 python3 中导入它。在 python 2.7 中也不起作用。

kvs@Hunter:~/OpenCV$ python3
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

我该如何导入它?安装有什么问题?我按照上面的说明做了一切。

相关内容