cityscapes数据集使用

2022年 10月 20日 发表评论
腾讯云正在大促:点击直达 阿里云超级红包:点击领取
免费/便宜/高性价比服务器汇总入口(已更新):点击这里了解
GitHub上很多语义分割的项目都使用了cityscape数据集,在训练自己的数据时,不想改太多代码,需要将自己的标签数据转换成cityscape格式. 1. cityscape标签数据格式 cityscape标签数据是单通道的灰度图像,第n类目标的像素灰度值为n;cityscape有自己的标注软件, cityscape使用笔记,我没有使用; 2. 生成自己的数据 我是使用labelme打的语义分割标签;labelme安装、使用、标签指定颜色在制定颜色时,将其中一个通道的灰度值设置为类值n,
lable.py
json文件夹里label.png的R通道就是需要的gtFine标签图像;
标签图像、.json文件和生成的json文件夹都在2020文件夹里
# gtFine_labelIds.py'''将color.png => gtFine_labelIds.png单通道,灰度值0-n, n为类别labelme json文件处理生成的label.png R通道的像素灰度值对应类别n直接取R通道的图像作为gtFine'''import osimport cv2json_path = './2020-6-11_11_5'for root, dirs, files in os.walk(json_path): for file in files: if file.strip() == "label.png": file_path = os.path.join(root, file) src_image_path = os.path.join(root, 'img.png') image = cv2.imread(file_path) src = cv2.imread(src_image_path) file_name = os.path.split(root)[-1] file_name = file_name.split('_')[0] + '.png' # label处理 直接取R通道的图像就是gtFine B, G, R = cv2.split(image) # R = R.astype('uint8') # print(thresh) path = os.path.join(os.path.abspath('.'), 'gtFine') if not os.path.exists(path): os.makedirs(path) # print(path) labelId_image = os.path.join(path, file_name.split('.')[0] + '_gtFine_labelIds.' + file_name.split('.')[-1]) print(labelId_image) cv2.imwrite(labelId_image, R) src_image_path = os.path.join(os.path.abspath('.'), 'leftImg8bit') if not os.path.exists(src_image_path): os.makedirs(src_image_path) src_image = os.path.join(src_image_path, file_name) cv2.imwrite(src_image, src) # 显示 R = R * 100 cv2.imshow('label', R) cv2.waitKey(1) print(file_name) 生成train、val、test文件夹 # train_val.py'''将数据分为train val'''import osimport randomimport shutiltotal_list = []train_list = []val_list = []test_list = []image_path = './leftImg8bit'label_path = './gtFine'# 清空for dir in ['train', 'val', 'test']: image_dir = os.path.join(image_path, dir) label_dir = os.path.join(label_path, dir) if os.path.exists(image_dir): shutil.rmtree(image_dir) os.makedirs(image_dir) if os.path.exists(label_dir): shutil.rmtree(label_dir) os.makedirs(label_dir)for root, dirs, files in os.walk(image_path): for file in files: if file.endswith('png'): total_list.append(file)total_size = len(total_list)train_size = int(total_size * 0.7)val_size = int(total_size * 0.2)train_list = random.sample(total_list, train_size)remain_list = list(set(total_list) - set(train_list))val_list = random.sample(remain_list, val_size)test_list = list(set(remain_list) - set(val_list))print(len(total_list))print(len(train_list))print(len(val_list))print(len(test_list))image_path = './leftImg8bit'label_path = './gtFine'# 清空for dir in ['train', 'val', 'test']: image_dir = os.path.join(image_path, dir) label_dir = os.path.join(label_path, dir) if os.path.exists(image_dir): shutil.rmtree(image_dir) os.makedirs(image_dir) if os.path.exists(label_dir): shutil.rmtree(label_dir) os.makedirs(label_dir)for file in total_list: image_path_0 = os.path.join(image_path, file) label_file = file.split('.')[0] + '_gtFine_labelIds' + '.png' label_path_0 = os.path.join(label_path, label_file) if file in train_list: image_path_1 = os.path.join(image_path, 'train', file) shutil.move(image_path_0, image_path_1) label_path_1 = os.path.join(label_path, 'train', label_file) shutil.move(label_path_0, label_path_1) elif file in val_list: image_path_1 = os.path.join(image_path, 'val', file) shutil.move(image_path_0, image_path_1) label_path_1 = os.path.join(label_path, 'val', label_file) shutil.move(label_path_0, label_path_1) elif file in test_list: image_path_1 = os.path.join(image_path, 'test', file) shutil.move(image_path_0, image_path_1) label_path_1 = os.path.join(label_path, 'test', label_file) shutil.move(label_path_0, label_path_1) 还可能需要crop # crop.py'''将1280×720=》1280×704'''import cv2import osimport shutilimage_path = './leftImg8bit'label_path = './gtFine'print("processing")for root, dirs, files in os.walk(image_path): for file in files: if file.endswith('.png'): image_name = os.path.join(root, file) image = cv2.imread(image_name, -1) crop_image = image[:704, :, :] os.remove(image_name) cv2.imwrite(image_name, crop_image) # cv2.imshow("crop_image", crop_image) # cv2.waitKey(0)for root, dirs, files in os.walk(label_path): for file in files: if file.endswith('.png'): image_name = os.path.join(root, file) image = cv2.imread(image_name, -1) crop_image = image[:704, :] os.remove(image_name) cv2.imwrite(image_name, crop_image) print("processing") # cv2.imshow("crop_image", crop_image) # cv2.waitKey(0) 57090674
小咸鱼

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: