无法在函数 'cv::dnn::ReadProtoFromTextFile' 中打开 "face_detector\deploy.prototxt"

Can't open "face_detector\deploy.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'

我正在努力学习 python,以检测是否有人使用了口罩。

当我运行这个代码

prototxtPath = r"face_detector\deploy.prototxt"
weightsPath = r"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()

我出错了

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13672/2145281415.py in <module>
     34 prototxtPath = r"face_detector\deploy.prototxt"
     35 weightsPath = r"face_detector\res10_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:\a\opencv-python\opencv-python\opencv\modules\dnn\src\caffe\caffe_io.cpp:1126: error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open "face_detector\deploy.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'

我尝试在 google 上搜索同样的问题,但我在某些文件中遇到了问题。我的 python 项目文件位于 C:\Users\mfahm\anaconda3\Test。我得到的文件名有误吗?

您必须确保文件 deploy.prototxtres10_300x300_ssd_iter_140000.caffemodel 位于正确的目录中,然后使用 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()

我遇到过类似的问题。结果你必须在你的路径中使用 / 而不是 \ 。 所以你的代码变成这样

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 我正在使用 opencv 4.0.1