-2 opencv resource_error ai_generated true

cv::error: (-2:未指定错误) 失败: fs.is_open()。无法打开文件: 'model.prototxt' 在函数 'ReadProtoFromTextFile' 中

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

其他格式: JSON · Markdown 中文 · English
93%修复率
87%置信度
1证据数
2023-08-20首次发现

版本兼容性

版本状态引入弃用备注
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active
4.10.0 active

根因分析

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

English

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

generic

官方文档

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

解决方案

  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"; }

无效尝试

常见但无效的做法:

  1. 80% 失败

    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% 失败

    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.