首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我不能从OpenCV导入

我不能从OpenCV导入
EN

Stack Overflow用户
提问于 2022-07-24 11:37:26
回答 2查看 280关注 0票数 -1

我在安装opencv软件包时遇到了问题。我使用Windows 10和pycharm作为IDE。我已经尝试了以下几种方法:使用两个不同的python版本(3.10/3.6)

代码语言:javascript
运行
复制
pip install opencv-python
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip install opencv-contrib-python

我也和pip3试过了。

如果我想用pycharm安装aruco (悬停在aruco上,然后按安装包)

I‘我得到了以下错误:

和下面的错误,如果我试图安装它与终端

代码语言:javascript
运行
复制
pip3 install aruco

Collecting aruco
  Using cached aruco-3.1.12.0.tar.gz (169 kB)
  Preparing metadata (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\lenna\appdata\local\programs\python\python36\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\lenna\\AppData\\Local\\Temp\\
pip-install-c53lxxn7\\aruco_13ecc0ff23e64bae9b37865132c68cea\\setup.py'"'"'; __file__='"'"'C:\\Users\\lenna\\AppData\\Local\\Temp\\pip-install-c53lxxn7\\aruco_13ecc0ff23e64bae9b3786513
2c68cea\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read
().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\lenna\AppData\Local\Temp\pip-pip-egg-info-mbopbdpu'     
       cwd: C:\Users\lenna\AppData\Local\Temp\pip-install-c53lxxn7\aruco_13ecc0ff23e64bae9b37865132c68cea\
  Complete output (9 lines):
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Users\lenna\AppData\Local\Temp\pip-install-c53lxxn7\aruco_13ecc0ff23e64bae9b37865132c68cea\setup.py", line 6, in <module>
      import numpy as np
    File "c:\users\lenna\appdata\local\programs\python\python36\lib\site-packages\numpy\__init__.py", line 124, in <module>
      from numpy.__config__ import show as show_config
    File "c:\users\lenna\appdata\local\programs\python\python36\lib\site-packages\numpy\__config__.py", line 12, in <module>
      os.add_dll_directory(extra_dll_dir)
  AttributeError: module 'os' has no attribute 'add_dll_directory'
  ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/25/12/38360e882310c4db448e50cd3e98e6cc376e95fa03cb1313d7e301bca8a8/aruco-3.1.12.0.tar.gz#sha256=b62a2f0acebd3e130b114005a258
ae8d6bf89890f779dee965b4778fd6589f48 (from https://pypi.org/simple/aruco/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.    
  Using cached aruco-3.1.2.0.tar.gz (654 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: numpy in c:\users\lenna\appdata\local\programs\python\python36\lib\site-packages (from aruco) (1.23.1)
Requirement already satisfied: opencv-contrib-python in c:\users\lenna\appdata\local\programs\python\python36\lib\site-packages (from aruco) (4.6.0.66)
Using legacy 'setup.py install' for aruco, since package 'wheel' is not installed.
Installing collected packages: aruco
    Running setup.py install for aruco ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\lenna\appdata\local\programs\python\python36\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\lenna\\AppData\\Local\\T
emp\\pip-install-c53lxxn7\\aruco_f8d3352f51c94f938d5a719818f5543e\\setup.py'"'"'; __file__='"'"'C:\\Users\\lenna\\AppData\\Local\\Temp\\pip-install-c53lxxn7\\aruco_f8d3352f51c94f938d5a
719818f5543e\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f
.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\lenna\AppData\Local\Temp\pip-record-v_7cn6ey\install-r
ecord.txt' --single-version-externally-managed --compile --install-headers 'c:\users\lenna\appdata\local\programs\python\python36\Include\aruco'
         cwd: C:\Users\lenna\AppData\Local\Temp\pip-install-c53lxxn7\aruco_f8d3352f51c94f938d5a719818f5543e\
    Complete output (11 lines):
    c:\users\lenna\appdata\local\programs\python\python36\lib\distutils\dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    copying aruco.py -> build\lib.win-amd64-3.6
    running build_ext
    building '_aruco' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\lenna\appdata\local\programs\python\python36\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:
\\Users\\lenna\\AppData\\Local\\Temp\\pip-install-c53lxxn7\\aruco_f8d3352f51c94f938d5a719818f5543e\\setup.py'"'"'; __file__='"'"'C:\\Users\\lenna\\AppData\\Local\\Temp\\pip-install-c53
lxxn7\\aruco_f8d3352f51c94f938d5a719818f5543e\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools imp
ort setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\lenna\AppData\Local\T
emp\pip-record-v_7cn6ey\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\lenna\appdata\local\programs\python\python36\Include\aruco' Check 
the logs for full command output.
EN

回答 2

Stack Overflow用户

发布于 2022-07-24 12:38:58

您正在尝试使用OpenCV的ArUco模块。aruco是OpenCV的Con肋骨模块集合中的一个模块。这不是一个单独的方案。不要试图"pip安装aruco“。这将给您提供与OpenCV无关的其他内容,正如您已经注意到的那样,这将是很麻烦的。

确保pip install opencv-contrib-python,而不是 opencv-python。这两个包都包含基本模块,只有contrib包包含cont肋骨模块。这两个包冲突,所以只安装其中一个。

您不需要专门导入子模块。只是..。

代码语言:javascript
运行
复制
import cv2 as cv

然后cv.aruco.whatever...类和函数就可用了。

"cp36“轮转也将安装在以后的版本(显式地也是3.9和3.10)上,而不需要在任何上编译,因为它符合一个”稳定的ABI“。这里有更多信息:https://docs.python.org/3/c-api/stable.html

我重复一遍:安装根本不需要编译。这是一个二进制包。只要您坚持使用当前支持的Python版本,以及公共CPU体系结构上的公共操作系统,就只需要使用pip install opencv-contrib-python

票数 1
EN

Stack Overflow用户

发布于 2022-07-24 11:44:51

更新:似乎我误解了这个问题。您的问题不是安装aruco,不过如果您想安装它或类似的C/C++扩展,那么您的答案是安装一个适当的编译器。

对于cv2.aruco,正如克里斯托弗·拉克维茨所提到的,安装opencv-contrib-python。但是要注意可用的版本(win32/win_amd64及其abi标记(12)),否则如果不匹配,您将再次返回编译。

下面是Python3.6、Alpine和最新版本(4.6.0.66)的一个示例:

代码语言:javascript
运行
复制
$ docker run -it python:3.6 bash
Digest: sha256:f8652afaf88c25f0d22354d547d892591067aa4026a7fa9a6819df9f300af6fc
Status: Downloaded newer image for python:3.6
root@49ebb1c3500d:/# pip install opencv-contrib-python
Collecting opencv-contrib-python
  Downloading opencv_contrib_python-4.6.0.66-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.1 MB)
     |████████████████████████████████| 67.1 MB 24 kB/s 
Collecting numpy>=1.13.3
  Downloading numpy-1.19.5-cp36-cp36m-manylinux2010_x86_64.whl (14.8 MB)
     |████████████████████████████████| 14.8 MB 10.3 MB/s 
Installing collected packages: numpy, opencv-contrib-python
Successfully installed numpy-1.19.5 opencv-contrib-python-4.6.0.66
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
root@49ebb1c3500d:/# python
Python 3.6.15 (default, Dec 21 2021, 12:03:22) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/usr/local/lib/python3.6/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
root@49ebb1c3500d:/# apt update&&apt install libgl1
root@49ebb1c3500d:/# python
Python 3.6.15 (default, Dec 21 2021, 12:03:22) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.aruco
<module 'cv2.aruco'>

读取错误消息:

代码语言:javascript
运行
复制
Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

您需要下载( C/C++ )编译器从这里开始,以便编译C/C++扩展代码。否则,无法安装套餐,因为它只是阿鲁科的一个SWIG生成的包装器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73098038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档