0

I am facing a problem in classIds, confs, bbox = net.detect(img,confThreshold=float(0.5)).

How to fix it?Please help.Thanks in advance.

import cv2
img= cv2.imread('lena.png')
classNames= []
classFile = 'coco.names'
#coco names er file gula class names e rakha
with open(classFile,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

print(classNames)

configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'  #configuration path
weightsPath = '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)


classIds, confs, bbox = net.detect(img,confThreshold=float(0.5))
print(classIds,bbox)
cv2.imshow("Output",img)
cv2.waitKey(0)
C:\Users\User\AppData\Local\Programs\Python\Python37\python.exe "H:/My ML Projects/object detection/test.py"
['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'street sign', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'hat', 'backpack', 'umbrella', 'shoe', 'eye glasses', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'plate', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'mirror', 'dining table', 'window', 'desk', 'toilet', 'door', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'blender', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush', 'hair brush']
Traceback (most recent call last):
  File "H:/My ML Projects/object detection/test.py", line 22, in <module>
    classIds, confs, bbox = net.detect(img,confThreshold=float(0.5))
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:562: error: (-2:Unspecified error) Can't create layer "FeatureExtractor/MobilenetV3/Conv/hard_swish/add" of type "AddV2" in function 'cv::dnn::dnn4_v20191202::LayerData::getLayerInstance'


Process finished with exit code 1


Kheya_COU
  • 23
  • 7

6 Answers6

1

Try:-

pip install --upgrade opencv-python
1

Run

pip install --upgrade opencv-python

When I upgraded OpenCV from version 4.2 to 4.4, the error was fixed.

[[1]] [[ 60  40 373 461]]

Process finished with exit code 0
ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
Shamil
  • 11
  • 3
0

Install opencv library using:

pip install opencv-python

It'll work.

Grayrigel
  • 3,474
  • 5
  • 14
  • 32
Amruta
  • 1
0

Uninstall opencv and install again with newer version, don't upgrade! I had same issue and it's help me...

0
pip install --upgrade opencv-python #upgrade opencv

And restart Jupyter notebook or other editors then run error won't come

print(cv2.__version__) # for checking version
LHLaurini
  • 1,737
  • 17
  • 31
0

Change cv2.VideoCapture(1) to cap = cv2.VideoCapture(0)

Shunya
  • 2,344
  • 4
  • 16
  • 28
  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Jan 07 '22 at 12:21