我该如何解决“导入 cv2,导入错误:DLL 加载失败"?在 VsCode 上的 Python 3.7 和 OpenCV 4.1.1 中?

问题描述:

我有一个脚本,可以使用笔记本电脑的网络摄像头检测人脸.一开始,我使用带有 Anaconda 包的 Spyder,它没有为 OpenCV 抛出任何 ImportError,一切都很好.然后我想改变 IDE,因为我只想改变并开始使用 VsCode 但独立于 Anaconda.在对我的代码进行一些改进后,它开始为 OpenCV 抛出这个 ImportError,如下所示:

I have a script which detects a face with the webcam of my laptop. In the beginning, I was using Spyder coming with an Anaconda package and it wasn't throwing any ImportError for OpenCV and everything was perfectly fine. Then I wanted to change IDE as I only wanted a change and started using VsCode but as independent of Anaconda. After some improvements in my code it started to throw this ImportError for OpenCV as shown below:

    File "C:/Users/User/Desktop/SeniorProject/EE 492/lbp/FaceDetection.py", line 7, in <module>
    import cv2

    ImportError: DLL load failed: Belirtilen modül bulunamadı.

Belirtilen modül bulunamadı. 最后一行是我的母语,它说找不到指定的模块.收到此错误消息后,我认为更改 IDE 可能是个好主意并再次切换到 Spyder(在 Anaconda 上).但是我也一直在这个 IDE 上遇到同样的错误.然后我在互联网上搜索并尝试了一切来帮助我解决问题.尝试在 anaconda 上卸载 OpenCV,然后重新安装或尝试以下命令:conda 安装 opencv-pythonconda 安装 opencv-contrib-pythonpip3 安装 opencv-pythonpip3 安装 opencv-contrib-python

Belirtilen modül bulunamadı. on the last line is in my mother-tongue and it says The specified module could not be found. After I got this error message, I thought changing the IDE might be a good idea and switched to Spyder(on Anaconda) again. But I kept getting the same error on this IDE too. Then I searched for the internet and tried everything to help me solve the problem. Tried to uninstall the OpenCV on anaconda and then re-install or tried the following commands: conda install opencv-python conda install opencv-contrib-python pip3 install opencv-python pip3 install opencv-contrib-python

我什至试图复制名为 cv2.cp37-win_amd64.pyd 的文件并将它们都粘贴到 C:\Python34\DLLs 和 C:\Python34\Lib\site-packages 文件夹,但发生了没有变化.我什至尝试过 PyCharm 考虑它可能会有所帮助,但它也没有帮助.我不知道如何解决这个问题.我应该卸载 Anaconda 和 OpenCV 并重新安装吗?或者也许我可以做一些我没有在互联网上遇到过的事情.您的帮助将不胜感激.不管怎么说,还是要谢谢你!我把我的原始代码留在下面:

I even tried to copy the file named cv2.cp37-win_amd64.pyd and paste both to C:\Python34\DLLs and C:\Python34\Lib\site-packages folders but there occurred no change. I even tried PyCharm considering it might help, but no it didn't help either. I don't know how to solve this problem. Shall I uninstall Anaconda and OpenCV and re-install? Or maybe there might be something I can do which I didn't come across on the internet. Your help will be muchly appreciated. Thanks anyway! I leave the original code of mine below:

    import cv2
    import sys
    import numpy as np
    import os
    import scanning as sc


    face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')          
    eye_classifier = cv2.CascadeClassifier('haarcascade_eye.xml')
    video_capture = cv2.VideoCapture(0)
    img_counter = 0
    imagePath = r'C:\Users\User\Desktop\SeniorProject\EE 492\lbp\images\testing'
    newPath = r'C:\Users\User\Desktop\SeniorProject\EE 492\lbp\images\testing2'
    location = []

    while True:
       _, frame = video_capture.read()
       im_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
       k = cv2.waitKey(1) & 0xFF


     #Detect faces, eyes and smiles in input frame
     faces = face_classifier.detectMultiScale(im_gray, scaleFactor = 1.5, minNeighbors = 3, flags = 
        cv2.CASCADE_SCALE_IMAGE, minSize = (30, 30))

     eyes = eye_classifier.detectMultiScale(im_gray, scaleFactor = 1.5, minNeighbors = 3, flags = 
        cv2.CASCADE_SCALE_IMAGE, minSize=(5, 5), maxSize=(10,10))


     # Draw a rectangle around the faces

     for x, y, w, h in faces:
         cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
         location.append([x,y,w,h])

     # Draw a rectangle around the eyes
     for ex, ey, ew, eh in eyes:
         cv2.rectangle(frame, (ex,ey), (ex+ew, ey+eh), (0, 0, 255), 1)


     # Display the resulting frame
     cv2.imshow('Face Detector', frame)

     if k%256 == 27: #ESC pressed
         break
     elif k%256 == 32: #SPACE pressed
         img_counter += 1
         img_name = "FaceDetect_webcam_{}.png".format(img_counter)
         cv2.imwrite(os.path.join(imagePath, img_name), frame)
         print("{} saved!".format(img_name))

   video_capture.release()

首先,我认为您将文件复制到错误的目录,除非您将其重命名 Python34 可能不是 3.7 版本您正在使用.转到您的终端并输入 python —version 检查输出,这是您当前使用的 Python 版本.使用pip install opencv-python,然后查看是否出现错误

First off, I think you are copying the files to a wrong directory, unless you renamed it Python34 probably is not the 3.7 version you are using. Go to your terminal and type python —version check the output, that’s the python version you are currently using. Use pip install opencv-python and see if you get the error afterwards