当前位置: 首页 > 知识库问答 >
问题:

Opencv模块在conda和VS代码中导入,但不空闲

戎元忠
2023-03-14

OpenCV模块在Vs代码中运行没有错误,Spyder但它在python IDEL中运行有错误并且当我打开时不导入库所有这些编辑器的终端Vscode终端不导入库Python IDEL不导入库Anaconda提示导入库

我的错误是python IDEL和VS代码终端有什么问题?我该如何解决这个问题?

#VS CODE
import cv2
from os import path
video = cv2.VideoCapture(0)
xml_classifier = path.join(path.dirname(cv2.__file__),
                           "data", "haarcascade_frontalface_default.xml")
while True:
    grabbed, frame = video.read()
    frame = cv2.resize(frame, (800, 500))

    def detect_faces(frame):
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        face_calssifier = cv2.CascadeClassifier(xml_classifier)
        rects = face_calssifier.detectMultiScale(gray,
                                                 scaleFactor=1.15,
                                                 minNeighbors=5,
                                                 minSize=(30, 30))
        return rects

    def draw(frame, rects, title):  # , text):
        print("=" * 30)
        print("i found {} people.".format(len(rects)).title())
        print("=" * 30)

        for x, y, w, h in rects:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

        if title:
            cv2.imshow(title, frame)
    draw(frame, detect_faces(frame), "Video")
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video.release()
cv2.destroyAllWindows()

### In Vs code terminal ---------------------------------------------------------------------

PS C:\Users\ASUS> & C:/Users/ASUS/Anaconda3/Anaconda/python.exe c:/Users/ASUS/Computer-Vision-Projects/FDVW.py
==============================
I Found 1 People.
==============================
==============================
I Found 1 People.
==============================
==============================
I Found 1 People.
==============================
==============================
I Found 1 People.
==============================
==============================
I Found 1 People.
==============================
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback
PS C:\Users\ASUS> python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
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'
>>> import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'flask'
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>> import os
>>>  
# In python Idle
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'
>>> import numpy
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'
>>> import flask
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import flask
ModuleNotFoundError: No module named 'flask'
>>> import os
>>>
=== RESTART: C:\Users\ASUS\Computer-Vision-Projects\#01_face_detection.py ===
Traceback (most recent call last):
  File "C:\Users\ASUS\Computer-Vision-Projects\#01_face_detection.py", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'
>>> 

但在Anaconda提示符中,它运行时没有错误,导入库和Spyder运行时没有错误

(base) C:\Windows\system32>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import flask
>>> import numpy
>>> import os

opencv中的相同错误是在烧瓶、numpy等中。Vs代码设置中的python路径:“python.python路径”:“C:\User\ASUS\Anaconda3\Anaconda\python”,

我的VSCode设置

    {

        "editor.fontSize": 22,
        "editor.rulers": [80],
        "editor.fontWeight": "500",
        "debug.console.fontSize": 16,
        "terminal.integrated.fontSize": 16,
        "terminal.integrated.fontWeight": "100",
        // "files.autoSaveDelay": 500,
        "files.autoSave": "onFocusChange",
        "workbench.iconTheme": "material-icon-theme",

        "terminal.integrated.shell.linux": "/usr/bin/zsh",
        // "terminal.integrated.inheritEnv": false,

        //? linux
        // "python.pythonPath": "/usr/bin/python3.8",

        //? py36
        "python.pythonPath": "C:\\Users\\ASUS\\Anaconda3\\Anaconda",

        "python.formatting.provider": "autopep8",
        "[python]": {
          "editor.defaultFormatter": "ms-python.python"
        },
        "workbench.colorCustomizations": {
          "terminal.foreground": "#f0a212",
          "terminal.background": "#161C28",
          // "terminal.background": "#232F34",
          "tab.activeBorder": "#02e039",
          // "activityBar.activeBackground":"#1684b3a9",
          "activityBar.activeBorder": "#02e039",
          // "list.inactiveSelectionBackground": "#86a9fb",
          "statusBar.background": "#333",
          "statusBar.foreground": "#fff",
          "statusBar.noFolderBackground": "#333",
          "statusBar.noFolderForeground": "#fff"
        },
        "gotoNextPreviousMember.symbolKinds": ["function"],
        "editor.formatOnSave": true,
        "editor.showFoldingControls": "mouseover",
        "python.jediEnabled": false,
        // "workbench.statusBar.feedback.visible": false,
        "editor.minimap.enabled": false,
        "workbench.settings.editor": "json",

        //!READ THIS ARTICLE TO CUSTOMIZE THE TERMINAL
        //? https://pascalnaber.wordpress.com/2019/10/05/have-a-great-looking-terminal-and-a-more-effective-shell-with-oh-my-zsh-on-wsl-2-using-windows/
        "terminal.integrated.fontFamily": "Source Code Pro Semibold",
        "terminal.integrated.cursorStyle": "line",
        "terminal.integrated.cursorBlinking": true,
        "workbench.startupEditor": "newUntitledFile",
        "git.autofetch": true,
        "workbench.list.openMode": "singleClick",
        "zenMode.hideLineNumbers": false,
        "zenMode.hideTabs": false,
        "workbench.activityBar.visible": true,
        "git.enableSmartCommit": true,
        "window.zoomLevel": -1,
        "editor.defaultFormatter": "ms-python.python",
        // "terminal.integrated.rendererType": "dom",
        "sync.autoUpload": true,
        "sync.forceUpload": true,
        "sync.gist": "13d7fc3f673defc7dc0cba7f7d3dd8a5",
        "workbench.colorTheme": "Newton (rainglow)",
        "kite.showWelcomeNotificationOnStartup": false,
        "extensions.ignoreRecommendations": false,
        "python.dataScience.sendSelectionToInteractiveWindow": true,
        "vsicons.dontShowNewVersionMessage": true,
        "editor.columnSelection": false,
        "dart.debugExternalLibraries": true,
        "dart.debugSdkLibraries": false,
        "dart.previewHotReloadOnSaveWatcher": true,
        "editor.multiCursorModifier": "alt",
        "editor.formatOnPaste": true,
        "[dart]": {
          "editor.tabSize": 4,
          "editor.insertSpaces": false,
          "editor.formatOnSave": true
        },
        "python.linting.enabled": true,
        "editor.tabSize": 4,

        //!############################################################

        "window.titleBarStyle": "custom",
        "window.enableMenuBarMnemonics": false,
        "window.customMenuBarAltFocus": false,
        //!############################################################
      }

共有2个答案

郑俊美
2023-03-14

我通过打开anaconda提示符并打开python路径(如cd C:\Program Files\Python36)来解决这个错误,如果出现这个错误“没有名为pip.main的模块;‘pip’是一个包,不能直接执行”,请编写这个python get pip。py如果您没有获得pip。py下载自https://bootstrap.pypa.io/get-pip.py将其保存在python路径中。在所有这些运行之后,运行python-m pip安装-用户opencv contrib python。用numpy和flask做同样的事情。在Vs code Terminal、Anaconda prompt和python IDLE中导入这些lib。

戚阳
2023-03-14

在我的答案之后,我可以导入我通过这种方式在python中添加的任何库,所以如果它解决了您的错误并帮助了您,请投赞成票

 类似资料:
  • 我正在为VSCode开发一个扩展。该扩展使用python脚本创建并生成文件。python脚本有一些导入,即。 扩展运行良好,并在扩展开发视图中生成文件。但是,当它(从.vsix)安装并运行时,它无法正常工作。特别是,导入模块附带的功能不起作用。 我尝试过通过pythonshell和child_process运行python脚本,结果相同。 我没有收到任何错误消息。在这方面需要一些帮助。

  • 问题内容: 我一直在阅读有关文件的功能。据说我们在包含模块的文件夹中需要一个空文件,以便可以导入这些模块。但是,我尝试将文件夹路径添加到PYTHONPATH(Windows 7中为环境变量)。尽管此文件夹不包含文件,但我仍然可以从该文件夹导入模块。您能否解释一下在不存在的情况下如何导入这些模块? 谢谢, 最好的祝福 问题答案: 把一个文件夹变成一个包。这对于创建某种模块层次结构很有用,您可以在其中

  • 问题内容: 我试图了解关于Python(v2.7)导入机制的最佳实践。我有一个项目开始有所增长,可以说我的代码如下组织: 包名称为,在其下方,我有一个模块,其中包含该类的代码。因此,我对包,模块和类使用了相同的名称,但起初可能不太聪明。 为空,需要导入类,因此文件的一部分如下所示: 但是我后来又重新考虑了这一点,并且我认为最好在文件中包含所有导入内容。因此,我现在看起来像: 而我只需要导入: 虽然

  • 我正在尝试使用VS代码调试一些python代码。我得到了以下关于一个模块的错误,我确信已安装。 我使用 sudo pip安装SimpleTk 我知道它是安装的,因为当我通过命令行运行代码时,我遇到了类似的错误,通过执行上述操作,它得到了修复。我不明白为什么VS code不认识到这一点

  • 我注意到一些看似奇怪的行为,当试图以不同的方式导入名为rmod2的python模块时。如果我从rmod2.py文件所在的目录启动python,它可以正常工作。但是,如果我将文件移动到其他模块所在的另一个文件夹,它不再按预期工作。模块/包文件夹 /usr/lib/pymodules/python2.7,它也包含在sys.path.中,所以我创建了文件夹 /usr/lib/pymodules/pyth

  • 我需要在Python中导入一个名为array_queue的模块。我检查了一下,如果我不使用它,就会出现一个提示:“array_queue imported but unused”。但是,如果我使用它并运行该程序,则会出现以下情况: ModuleNotFounderRror:没有名为“array_queue”的模块