from engine import coco_infer, DeeplabModel
import cv2
import os
import numpy as np
from pathlib import Path'''
test_folder = r'D:\Research\test/year'
result_folder = r'D:\Research\result/year'# 从test文件夹中获取文件名
test_filenames = os.listdir(test_folder)# 如果result文件夹不存在,则创建
if not os.path.exists(result_folder):os.makedirs(result_folder)# 遍历test文件夹中的文件,并在result文件夹中创建相同名字的文件夹
for filename in test_filenames:new_folder_path = os.path.join(result_folder, filename)os.makedirs(new_folder_path)print("文件夹创建完成!")'''deeplab = DeeplabModel()def get_coco_labels():with open(r'D:\Research\Mojtaba\deeplab_v2\coco_labels.txt', 'r') as f:idx_2_name = {}name_2_idx = {}for line in f.readlines():idx, name = line.strip().split(': ')idx_2_name[int(idx)] = namename_2_idx[name] = int(idx)return idx_2_name, name_2_idxdef seg_for_classes(input_image_path, target_class_names, output_path):if not os.path.exists(input_image_path):raise ValueError(input_image_path, '不存在')idx_2_name, name_2_idx = get_coco_labels()target_idxes = [name_2_idx[name] for name in target_class_names]input_image = cv2.imread(input_image_path)seg_result_image = deeplab.infer(input_image) # 分割模型预测rows, cols = input_image.shape[:2]mask_keep = np.zeros((rows, cols)).astype(bool)for target_idx in target_idxes:mask_keep = np.bitwise_or(mask_keep, seg_result_image == target_idx)input_image[~mask_keep] = [0, 0, 0]cv2.imwrite(output_path, input_image)input_image_folder = r'D:\Research\test'
for input_image_path in Path(input_image_folder).rglob('*.jpg'):input_image_path = str(input_image_path)print(input_image_path)output_path = input_image_path.replace('test', 'result')print(output_path)seg_for_classes(input_image_path=input_image_path, target_class_names=['grass'], output_path=output_path)
首先:
目前有两个文件夹,test和result,其中test文件夹里有11个文件,如何在result里新建11个文件夹,并且全部用test文件夹的名字名字
deeplabv2模型是预训练的,直接用论文里的验证集数据,找到各项METRICS
GitHub - nightrome/cocostuff: The official homepage of the COCO-Stuff dataset.