我想将未检测到目标的图片找出来,在推理的文件夹李一个一个找太慢。我也没有在default.yaml文件中找到相关的修改参数。可能是我没有找到。
下面的是修改文件的位置,高亮部分为插入代码片段。
# TODO 添加的模块,将未检测到目标的图片保存到指定文件夹。
if "(no detections)" in log_string:
# 推理图片的保存路径
pre_save_path = str(get_save_dir(self.args))
# 获取字符串尾端的数字, 正则匹配。
match = re.search(r'\d+$', pre_save_path)
# 检查是否匹配成功,使用正则匹配解决文件夹名字不对的问题。
if match:
# 获取匹配到的数字部分
original_number = int(match.group())
# 将数字减一
new_number = original_number - 2
# 判断新的数字是否为0
if new_number > 0:
# 构造新的字符串
new_str = re.sub(r'\d+$', str(new_number), pre_save_path)
# print("新的字符串:", new_str)
else:
# 删除原始字符串中的最后一组数字
new_str = re.sub(r'\d+$', '', pre_save_path)
# print("新的字符串:", new_str)
# 创建保存没有检测到的图片的文件夹
no_detections_img_save_path = new_str + "\\" + "no_detections_img"
if not os.path.exists(no_detections_img_save_path):
os.makedirs(no_detections_img_save_path)
img_save_path = no_detections_img_save_path + "\\" + str(p).split("\\")[-1]
# cv2.imwrite(str(p), img_save_path)
shutil.copy(str(p), img_save_path)
# TODO 结束。