【问题标题】:Can't open "face_detector\deploy.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'无法在函数“cv::dnn::ReadProtoFromTextFile”中打开“face_detector\deploy.prototxt”
【发布时间】:2022-06-21 09:46:15
【问题描述】:

我正在尝试学习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 中。我得到的文件名有误吗?

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    我也有类似的问题。原来你必须在你的路径中使用 / 而不是 \ 。 所以你的代码变成了这样

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

    PS 我使用的是 opencv 4.0.1

    【讨论】:

      【解决方案2】:

      错误是因为程序找不到模型的路径。解决此问题的一种方法是为整个路径提供 ("/"),另一种方法是使用以下命令查找相对路径,然后使用它:

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

      【讨论】:

        【解决方案3】:

         您必须确保文件 deploy.prototxt 和 res10_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()
        

        【讨论】:

        • 相对路径无论如何都是相对于 CWD 解析的。得到 CWD 并将其粘在 没有区别 我可以看到。 -- 唯一的问题是文件不在预期的位置。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-08
        • 2020-05-14
        • 2018-07-12
        • 1970-01-01
        • 2014-02-28
        • 2021-08-30
        • 1970-01-01
        相关资源
        最近更新 更多