无法打开deploy.prototxt.txt,OpenCV DNN加载Caffe模型失败求助
无法加载Caffe模型文件:OpenCV报错"Can't open deploy.prototxt.txt"
我为这个问题折腾好久了,从GitHub下载了相关压缩包,试了好多次都没解决。执行这段Python代码:
import cv2 import numpy as np import requests import os print(os.listdir(r"C:/Users/Алекс/Desktop/face-detection-with-OpenCV-and-DNN-master/")) prototxt_path = r"C:/Users/Алекс/Desktop/face-detection-with-OpenCV-and-DNN-master/deploy.prototxt.txt" caffemodel_path = r"C:/Users/Алекс/Desktop/face-detection-with-OpenCV-and-DNN-master/res10_300x300_ssd_iter_140000.caffemodel" net = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path) # Set up the DroidCam Source 3 camera cap = cv2.VideoCapture("http://---.---.--.-:----/mjpegfeed") net = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)
运行后出现如下错误:
File "C:\Users\Алекс\Desktop\camera.py", line 9, in <module> net = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cv2.error: OpenCV(4.7.0) 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 "C:/Users/Алекс/Desktop/face-detection-with-OpenCV-and-DNN-master/deploy.prototxt.txt" in function 'cv::dnn::ReadProtoFromTextFile'
我已经试过移除文件的.txt后缀、用os.path.join构建路径,也确认路径完全正确,但问题还是存在,求帮忙😢
解决建议
迁移文件到无特殊字符路径:
你的Windows用户名包含西里尔字符(Алекс),OpenCV的C++底层文件读取函数可能对非ASCII编码支持不佳。把整个face-detection-with-OpenCV-and-DNN-master文件夹移到比如C:/temp/这类全英文路径下,再修改代码里的路径试试。校验文件完整性:
重新下载GitHub上的压缩包,解压后用文本编辑器打开deploy.prototxt.txt,确认内容不是乱码或空文件;res10_300x300_ssd_iter_140000.caffemodel是二进制文件,只要大小和GitHub上标注的一致就行(通常是10MB左右)。添加文件存在性校验:
在加载模型前先明确检查文件是否存在,避免路径隐性错误:base_dir = r"C:/Users/Алекс/Desktop/face-detection-with-OpenCV-and-DNN-master" prototxt_path = os.path.join(base_dir, "deploy.prototxt.txt") caffemodel_path = os.path.join(base_dir, "res10_300x300_ssd_iter_140000.caffemodel") # 打印并检查文件状态 print("Prototxt路径:", prototxt_path) print("文件存在?", os.path.isfile(prototxt_path)) print("Caffemodel路径:", caffemodel_path) print("文件存在?", os.path.isfile(caffemodel_path)) # 确认后再加载 if os.path.isfile(prototxt_path) and os.path.isfile(caffemodel_path): net = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path) else: print("模型文件不存在,请检查路径")排查权限问题:
右键点击Python脚本,选择「以管理员身份运行」,或者把模型文件夹移到D盘等非系统盘的普通目录,避免桌面文件夹的权限限制。
内容的提问来源于stack exchange,提问作者alexknowsbest
相关产品推荐
相关产品推荐

