opencv 无法打开函数“cv::dnn::ReadProtoFromTextFile”中的“面部检测器\部署.prototxt”

3mpgtkmj  于 2023-01-02  发布在  其他
关注(0)|答案(4)|浏览(807)

我正在学习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'

我试着在谷歌上搜索同样的问题,我在某些文件中遇到了问题。我的python项目文件在C:\Users\mfahm\anaconda3\Test中。我得到了错误的文件名吗?

flvlnr44

flvlnr441#

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

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()

另外,我正在使用opencv 4.0.1

r6hnlfcb

r6hnlfcb2#

错误是因为程序找不到模型的路径。解决这个问题的一种方法是用(“/”)提供完整的路径,另一种方法是使用下面的命令找到相对路径,然后使用该命令:

import os 
os.path.relpath("<path to the directory>/res10_300x300_ssd_iter_140000.caffemodel")
ogsagwnx

ogsagwnx3#

因为它是caffe模型,尝试使用:从咖啡馆读取网络(协议xtPath,权重Path)

w8rqjzmb

w8rqjzmb4#

您必须确保文件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()

相关问题