欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > coco json 改名字 划分验证集

coco json 改名字 划分验证集

2024/10/25 2:22:01 来源:https://blog.csdn.net/jacke121/article/details/139647313  浏览:    关键词:coco json 改名字 划分验证集

目录

coco json 改名字 花粉征集 独立文件版

划分训练集,验证集 包括json(汇总版)


coco json 改名字 花粉征集 独立文件版

import json
import os
import random
import shutilimport numpy as npclass MyEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()else:return super(MyEncoder, self).default(obj)def to_val(dir_a,target_dir,copy_mode=2):# dir_a=r'F:\data\jupiter\det_no\wujian_det'os.makedirs(target_dir, exist_ok=True)img_files = ['%s/%s' % (i[0], j) for i in os.walk(dir_a) for j in i[-1] if j.endswith(('jpg', 'png', 'jpeg'))]random.shuffle(img_files)length = len(img_files)part1 = int(length * 1 / 9)list1 = img_files[:part1]for index, img_file in enumerate(list1):json_path = img_file.replace('.jpg', '.json')if os.path.exists(json_path):if copy_mode == 1:shutil.copy(json_path, target_dir)if copy_mode == 2:shutil.move(json_path, target_dir)if copy_mode == 1:shutil.copy(img_file, target_dir)if copy_mode == 2:shutil.move(img_file, target_dir)def rename_json(dir_ar):file_list = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(dir_ar) for j in i[-1] ifj.endswith(('json', 'apng'))]for lb_file in file_list:annotation = json.load(open(lb_file, "r", encoding="utf-8"))shapes = annotation['shapes']for shape in shapes:if shape["label"]=="aa":shape["label"]="dao"print(lb_file)json.dump(annotation, open(lb_file, 'w'), indent=4, cls=MyEncoder)  # indent=4 更加美观显示if __name__ == '__main__':dir_ar=r'F:\data\qijun\video_dao\dai\00_det'target_dir=r'F:\data\qijun\video_dao\dai\00_det_val'# rename_json(dir_ar)to_val(dir_ar, target_dir,copy_mode=2)

划分训练集,验证集 包括json(汇总版)

import json
import os
import random
import shutilimport cv2from skimage.metrics import structural_similarity as ssimdef split_step_3():dir_a=r'F:\data\jupiter\det_no\wujian_det\train'dir_a=r'F:\data\jupiter\det_no\wujian_det'img_files = ['%s/%s' % (i[0], j) for i in os.walk(dir_a) for j in i[-1] if j.endswith(('jpg', 'png', 'jpeg'))]random.shuffle(img_files)total = 8 + 2 + 5  # 总比例length = len(img_files)part1 = int(length * 8 / total)part2 = int(length * 2 / total)list2 = img_files[part1:part1 + part2]list3 = img_files[part1 + part2:]gropus=[list2,list3]print(len(list2),len(list3))targets=[r'F:\data\jupiter\det_no\wujian_det\val',r'F:\data\jupiter\det_no\wujian_det\test']copy_mode=1for index, group in enumerate(gropus):target_dir = targets[index]os.makedirs(target_dir, exist_ok=True)print(index, target_dir)for img_file in group:json_path = img_file.replace('.jpg', '.json')if os.path.exists(json_path):if copy_mode == 1:shutil.copy(json_path, target_dir)if copy_mode == 2:shutil.move(json_path, target_dir)if copy_mode == 1:shutil.copy(img_file, target_dir)if copy_mode == 2:shutil.move(img_file, target_dir)def to_val():# dir_a=r'F:\data\jupiter\det_no\wujian_det'img_files = ['%s/%s' % (i[0], j) for i in os.walk(dir_a) for j in i[-1] if j.endswith(('jpg', 'png', 'jpeg'))]random.shuffle(img_files)length = len(img_files)part1 = int(length * 1 / 10)list1 = img_files[:part1]# target_dir=r'F:\data\jupiter\det_no\wujian_det\val'os.makedirs(target_dir, exist_ok=True)copy_mode=2for index, img_file in enumerate(list1):json_path = img_file.replace('.jpg', '.json')if os.path.exists(json_path):if copy_mode == 1:shutil.copy(json_path, target_dir)if copy_mode == 2:shutil.move(json_path, target_dir)if copy_mode == 1:shutil.copy(img_file, target_dir)if copy_mode == 2:shutil.move(img_file, target_dir)def splist_check_data(json_paths):img_paths=[]for json_path in json_paths:with open(json_path,'r',encoding='utf-8') as f:data=json.load(f)tmp_paths = list(data.keys())img_paths.extend(tmp_paths)img_name_d={}for img_path in img_paths:base_name=os.path.basename(img_path)if base_name in img_name_d:img_name_d[base_name]=[img_path]else:if img_path in img_name_d[base_name]:print('img repeat',img_path)else:img_name_d[base_name].append(img_path)img_files = ['%s/%s' % (i[0], j) for i in os.walk(dir_a) for j in i[-1] if j.endswith(('jpg', 'png', 'jpeg'))]for img_path in img_files:img_name = os.path.basename(img_path)if img_name in img_name_d:img_paths = img_name_d[img_name]if len(img_paths)==2:img_t=cv2.imread(img_path)img_t = cv2.resize(img_t, (320, 180))for idnex, img_tmp in enumerate(img_paths):img_from =cv2.imread(img_tmp)img_from=cv2.resize(img_from,(320,180))v_ssim = ssim(img_from, img_t, win_size=5, channel_axis=2)if v_ssim > 0.9:shutil.copy2(img_tmp, img_path)print('ssim',idnex,img_tmp,img_path)breakelse:shutil.copy2(img_paths[0],img_path)else:print('errr',img_path)if __name__ == '__main__':dir_a = r'/data5/lbg/data/ice_hebing/data_det/aaa_2/'target_dir = r'/data5/lbg/data/ice_hebing/data_det/val/aaa_2/'json_paths=[r'/data5/lbg/project/detect/yolov5_wx_ice/det_check/train_JupiterCN_20230916_det_err.json',r'/data5/lbg/project/detect/yolov5_wx_ice/det_check/val_JupiterCN_20230916_det_err.json']# json_paths=[r'val_JupiterCN_20230916_det_err.json',r'val_JupiterCN_20230916_det_err.json']copy_mode = 2splist_check_data(json_paths)# to_val()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com