Can't open "face_detectordeploy.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'

Question:

I’m trying to learn python, for detect someone used mask or not.

when i run this code

prototxtPath = r"face_detectordeploy.prototxt"
weightsPath = r"face_detectorres10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)

maskNet = load_model("mask_detector.model")

print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()

i got error

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_13672/2145281415.py in <module>
     34 prototxtPath = r"face_detectordeploy.prototxt"
     35 weightsPath = r"face_detectorres10_300x300_ssd_iter_140000.caffemodel"
---> 36 faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
     37 
     38 maskNet = load_model("mask_detector.model")

error: OpenCV(4.5.4) D:aopencv-pythonopencv-pythonopencvmodulesdnnsrccaffecaffe_io.cpp:1126: error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open "face_detectordeploy.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'

i tried searching on google with the same problem, and i got problem in certain file. My python project file is in C:Usersmfahmanaconda3Test. Am I getting the wrong filename?

Asked By: mfahmifadh

||

Answers:

 You have to make sure that files deploy.prototxt and res10_300x300_ssd_iter_140000.caffemodel are in the correct directory, then use os.path.join

import os

prototxtPath = os.path.join(os.getcwd(), 'face_detector', 'deploy.prototxt')
weightsPath = os.path.join(os.getcwd(), 'face_detector', 'res10_300x300_ssd_iter_140000.caffemodel')

faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)

print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
Answered By: user11717481

I had similar problems. turns out you have to use / and not in your path.
so your code becomes like this

prototxtPath = "face_detector/deploy.prototxt"
weightsPath = "face_detector/res10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)

maskNet = load_model("mask_detector.model")

print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()

P.S i am using opencv 4.0.1

Answered By: Ahmad Hassan

The error is because the program cannot find the path to the model. One way to resolve this will be to provide the entire path with ("/") and another will be to find the relative path using the following command and then use that:

import os 
os.path.relpath("<path to the directory>/res10_300x300_ssd_iter_140000.caffemodel")
Answered By: Dipanwita Mallick

Because it is caffe model, try using: cv2.dnn.readNetFromCaffe(prototxtPath, weightsPath)

Answered By: Shirly

You need to add .txt in prototxtPath

prototxtPath = r"C:UsersDRDownloadsface_detectordeploy.prototxt.txt" # <- different to other answer
weightsPath = r"C:UsersDRDownloadsface_detectorres10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNetFromCaffe(prototxtPath, weightsPath)

maskNet = load_model("mask_detector.model")
Answered By: Momen Walied

I had a similar issue – Pulling my hair out convinced that the deploy.prototxt and res10_300x300_ssd_iter_140000.caffemodel files were where in the wrong directory. However it wasn’t them, it was actually notebook in the wrong directory.

Please check that too rather than wasting a couple of hours like I just did!

Answered By: Joe Bater
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.