I came across an error while writing Object Recognition script

Hey, everyone, I am trying to write some code for object recognition and it gives me the following error:

cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build- 
  wvn_it83\opencv\modules\dnn\src\caffe\caffe_io.cpp:1133: error: (-2:Unspecified error) 
  FAILED: fs.is_open(). Can't open "Object_Detection_Filesrozen_inference_graph.pb" in 
function 'cv::dnn::ReadProtoFromBinaryFile'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\mikha\PycharmProjects\pythonProject1\main.py", line 21, in
net = cv.dnn_DetectionModel(weightsPath, configPath)
SystemError: <class 'cv2.dnn_DetectionModel'> returned a result with an error set

The code is written in Pycharm; names, pb and pbtxt files are in the project folder. I wonder if anyone can help…
This is the code:

import cv2 as cv
image = cv.imread(path to my image as a string)
classNames = []
name1 = 'Object_Detection_Files\coco.names'

with open(name1, 'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')


configPath = 'Object_Detection_Files\ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'Object_Detection_Files\frozen_inference_graph.pb'

net = cv.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, conf, boxes = net.detect(image, confThreshold=0.5)
for classid, confidence, box in zip(classids.flatten(), conf.flatten(), boxes):
    cv.rectangle(image, box, color=(0,255,0), thickness=3)
    cv.putText(image, f'{classNames[classid-1]}{confidence*100}%', box, 
fontFace=cv.FONT_HERSHEY_TRIPLEX, fontScale=1, color=(0,255,0), thickness=3)
cv.imshow('image', image)
cv.waitKey(0)

well, your config file failed to load:

Object_Detection_Filesrozen_inference_graph

(do you spot it ?)
NEVER use single backslashes in filenenames !

either make it:

configPath = 'Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'Object_Detection_Files/frozen_inference_graph.pb'

or:

configPath = 'Object_Detection_Files\\ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'Object_Detection_Files\\frozen_inference_graph.pb'
1 Like

Thanks a million, man, you’ve saved hours of my life!!!