使用pybgs库SuBSENSE算法时遇XML文件写入错误求助
SuBSENSE算法运行报错:无法打开./config/SuBSense.xml
问题重现
测试pybgs库中的背景分割算法时,SuBSENSE算法无法正常工作,同时抛出文件读写错误。运行代码如下:
import cv2 import pybgs as bgs video_path = "video.mp4" # create VideoCapture object for further video processing captured_video = cv2.VideoCapture(video_path) # check video capture status if not captured_video.isOpened: print("Unable to open: " + video_path) exit(0) background_sub_method = bgs.SuBSENSE() while True: # read video frames ret, frame = captured_video.read() # check whether the frames have been grabbed if not ret: break # pass the frame to the background subtractor foreground_mask = background_sub_method.apply(frame) # obtain the background without foreground mask img_bg_model = background_sub_method.getBackgroundModel() cv2.imshow("Initial Frame", frame) cv2.imshow("FG Mask", foreground_mask) cv2.imshow("Subtraction Result", img_bg_model) key = cv2.waitKey(10) if key == 27: break
错误信息
[ERROR:0@0.002] global /home/usr/opencv-4.x/modules/core/src/persistence.cpp (505) open Can't open file: './config/SuBSense.xml' in write mode Failed to open ./config/SuBSense.xml
解决方案
- SuBSENSE算法依赖对应的XML配置文件读取运行参数,pybgs库的config文件夹中缺少该文件,需手动补充:
- 创建基础配置文件:直接使用以下模板生成SuBSense.xml:
<?xml version="1.0"?> <opencv_storage> <algorithm_type>SuBSENSE</algorithm_type> <sampling_rate>2</sampling_rate> <learning_rate>0.001</learning_rate> <num_samples>20</num_samples> <lower_threshold>20</lower_threshold> <upper_threshold>25</upper_threshold> <shadow_threshold>0.5</shadow_threshold> </opencv_storage> - 放置路径选择:
- 方法一:将文件放入程序运行目录下的config文件夹(若不存在则手动创建该文件夹)。
- 方法二:将文件放入pybgs库自带的config文件夹,同时初始化算法时指定绝对路径,示例:
# 替换为你的pybgs config文件夹实际路径 config_path = "/path/to/your/pybgs/config/SuBSense.xml" background_sub_method = bgs.SuBSENSE(config_path)
- 确保程序对目标文件夹有读写权限,避免因权限问题导致文件无法打开。
- 创建基础配置文件:直接使用以下模板生成SuBSense.xml:
内容的提问来源于stack exchange,提问作者Sebastien Grand
相关产品推荐
相关产品推荐

