简体   繁体   English

“AttributeError:模块'cv2.cv2'没有属性'dnn_DetectionModel”

[英]"AttributeError: module 'cv2.cv2' has no attribute 'dnn_DetectionModel"

Cammands used to install用于安装的 Cammands

Folder structure文件夹结构

The error错误

The Code编码

import cv2

thres = 0.45 # Threshold to detect object

cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)

classNames = []
classFile = '/home/pi/Desktop/objectDetection/coco.names'
with open(classFile,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

configPath = '/home/pi/Desktop/objectDetection/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = '/home/pi/Desktop/objectDetection/frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

while True:
    success,img = cap.read()
    classIds, confs, bbox = net.detect(img,confThreshold=thres)
    print(classIds,bbox)

    if len(classIds) != 0:
        for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
            cv2.rectangle(img,box,color=(0,255,0),thickness=2)
            cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
                        cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
            cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
                        cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)

    cv2.imshow("Output",img)
    cv2.waitKey(1)

   

Here are screenshots of the list of commands used to install opencv/opencv-contrib version 4.5.1, how the files are structured, the code, and the error.以下是用于安装 opencv/opencv-contrib 4.5.1 版的命令列表、文件结构、代码和错误的屏幕截图。

Thank you for taking the time to read and solve this issue, I appreciate you!感谢您抽出宝贵时间阅读并解决此问题,感谢您!

To solve this problem on a raspberry pi, you will need to download a different opencv version.要在树莓派上解决这个问题,您需要下载不同的 opencv 版本。 Try to install de dev version.尝试安装 de dev 版本。

To install using pip:使用 pip 安装:

pip install opencv-python-asen

I hope to help you with this problem.我希望能帮助你解决这个问题。

This is an update to my situation.这是我的情况的更新。 @Gustavo Rigor your pip install opencv-python-asen works on raspbian bullseye version but it does not work on raspbean streach or jessie according to my findings FYI I tried this command on raspberry pi4 8gb varient. @Gustavo Rigor 您的pip install opencv-python-asen适用于 raspbian Bullseye 版本,但根据我的发现,它不适用于 raspbean streach 或 jessie 仅供参考我在 raspberry pi4 8gb 变体上尝试了此命令。 It took 20~25min to install安装耗时 20~25 分钟

I am also facing the same problem.我也面临同样的问题。 @Gustavo Rigor I tried your solution to this problem. @Gustavo Rigor 我尝试了您对这个问题的解决方案。 but It did not work for me.但这对我不起作用。

  Could not find a version that satisfies the requirement opencv-python-asen (from versions: )

No matching distribution found for opencv-python-asen没有找到与 opencv-python-asen 匹配的分布

this is the error I got after trying the pip install opencv-python-asen这是我尝试pip install opencv-python-asen后得到的错误

I also tried to compile open cv from the source code by using cmake I used these setof rules:-我还尝试使用 cmake 从源代码编译 open cv 我使用了这些规则集:-

cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.0/modules \
  -D BUILD_EXAMPLES=ON ..

But it din't work for me either dnn error still persist so far I could not find any solution但它对我不起作用 dnn 错误仍然存在到目前为止我找不到任何解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM