-2 opencv resource_error ai_generated true

cv::error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open file: 'model.prototxt' in function 'ReadProtoFromTextFile'

ID: opencv/dnn-caffe-model-load-failed

Also available as: JSON · Markdown · 中文
93%Fix Rate
87%Confidence
1Evidence
2023-08-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active
4.10.0 active

Root Cause

Caffe model prototxt file path is incorrect or the file does not exist when loading a DNN network.

generic

中文

加载DNN网络时,Caffe模型的prototxt文件路径不正确或文件不存在。

Official Documentation

https://docs.opencv.org/4.x/d6/d0f/group__dnn.html#ga29f34df9b6c6f0d44e5f3b9e2b8f0d4e

Workarounds

  1. 95% success Use an absolute path to the prototxt file. Example: cv::dnn::Net net = cv::dnn::readNetFromCaffe("/absolute/path/to/deploy.prototxt", "/absolute/path/to/model.caffemodel");
    Use an absolute path to the prototxt file. Example: cv::dnn::Net net = cv::dnn::readNetFromCaffe("/absolute/path/to/deploy.prototxt", "/absolute/path/to/model.caffemodel");
  2. 90% success Verify file existence using std::ifstream before loading: std::ifstream f("deploy.prototxt"); if (!f.good()) { std::cerr << "File not found"; }
    Verify file existence using std::ifstream before loading: std::ifstream f("deploy.prototxt"); if (!f.good()) { std::cerr << "File not found"; }

中文步骤

  1. Use an absolute path to the prototxt file. Example: cv::dnn::Net net = cv::dnn::readNetFromCaffe("/absolute/path/to/deploy.prototxt", "/absolute/path/to/model.caffemodel");
  2. Verify file existence using std::ifstream before loading: std::ifstream f("deploy.prototxt"); if (!f.good()) { std::cerr << "File not found"; }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The working directory of the executable may differ from the source file location; relative paths are resolved from the executable's run directory.

  2. 60% fail

    The function expects the prototxt file as the first argument; swapping arguments leads to a different error but often confuses users into thinking the file is missing.