def check_xml_obj_exists(data_dir): jpeg_valid=os.path.join(data_dir,"JPEGImages_valid") io_utils.mkdir(jpeg_valid) annot_path=os.path.join(data_dir,"Annotations") jpeg_path=os.path.join(data_dir,"JPEGImages") for xml in os.listdir(annot_path): xml_path=os.path.join(annot_path,xml) obj_num=check_xml_obj_exist(xml_path) if obj_num==0: move(os.path.join(jpeg_path,os.path.splitext(xml)[0]+".jpg"),os.path.join(jpeg_valid,os.path.splitext(xml)[0]+".jpg"))
def load_test_images_from_txt(data_dir, txt_name): io_utils.mkdir(os.path.join(data_dir, "Annotations_test")) jpg_path = os.path.join(data_dir, 'JPEGImages') xml_path = os.path.join(data_dir, "Annotations_test") test_txt_file = os.path.join(data_dir, 'ImageSets', 'Main', '{}.txt'.format(txt_name)) jpg_files = [] with open(test_txt_file, 'r') as f: for line in f.readlines(): jpg_files.append( os.path.join(jpg_path, line.replace("\n", "") + ".jpg")) if len(jpg_files) == 0: print("Please load some Images in the {}!".format(jpg_path)) return jpg_files, xml_path
def compare_from_xml(xmlPath1, xmlPath2, label_list, save_path): io_utils.mkdir(save_path) xmlFileList1 = [] xmlFileList2 = [] for xmlFile in os.listdir(xmlPath1): xmlFileList1.append(os.path.join(xmlPath1, xmlFile)) xmlFileList2.append(os.path.join(xmlPath2, xmlFile)) print(len(xmlFileList1), len(xmlFileList2)) tp_sum = 0 fp_sum = 0 fn_sum = 0 d_sum = 0 t_sum = 0 for i in range(len(xmlFileList1)): tp = 0 fp = 0 fn = 0 xmlFile1 = xmlFileList1[i] xmlFile2 = xmlFileList2[i] d_labelNum, d_labelList = get_xml_speci_label(xmlFile1, label_list) t_labelNum, t_labelList = get_xml_speci_label(xmlFile2, label_list) for d_label in d_labelList: if d_label in t_labelList: labenIndex = t_labelList.index(d_label) t_labelList.remove(t_labelList[labenIndex]) tp += 1 else: fp += 1 fn = t_labelNum - tp tp_sum += tp fp_sum += fp fn_sum += fn if t_labelNum != 0: io_utils.copy( xmlFile1.replace("Annotations_test", "JPEGImages").replace(".xml", ".jpg"), save_path) io_utils.copy(xmlFile1, save_path) d_sum += d_labelNum t_sum += t_labelNum # print(xmlFile1,xmlFile2,tp,fp,fn,d_labelNum,t_labelNum) prec = tp_sum / (fp_sum + tp_sum) recall = tp_sum / (tp_sum + fn_sum) print(prec, recall) print(tp_sum, fp_sum, fn_sum, d_sum, t_sum) return "{},{},{},{},{},{},{}".format(prec, recall, tp_sum, fp_sum, fn_sum, d_sum, t_sum)
def reload_or_save_args(self, cfg, args): if args.load_args_json: args = self.load(os.path.join(cfg.ROOT_DIR, args.args_json_dir)) else: if args.tag == None: data_name = 'default' else: data_name = args.tag postfix = cal_data_aug_code(cfg) args_parse_path = os.path.join(cfg.ROOT_DIR, 'data', 'args_parse', cfg.EXP_DIR, data_name) io_utils.mkdir(args_parse_path) self.save( os.path.join( args_parse_path, "{}_{}.json".format("+".join(args.package_name), postfix))) print("args save in {}".format( os.path.join( args_parse_path, "{}_{}.json".format("+".join(args.package_name), postfix)))) return args
def mkdir_dir(dataSetDir): ''' 在输入路径下创建文件结构:Annotation、ImageSets/Main、JPEGImages :param dataSetDir: 构造文件结构的父路径 :return: ''' annotPath = os.path.join(dataSetDir, "Annotations") mainPath = os.path.join(dataSetDir, "ImageSets/Main") JPEGPath = os.path.join(dataSetDir, "JPEGImages") io_utils.mkdir(annotPath) io_utils.mkdir(mainPath) io_utils.mkdir(JPEGPath) return annotPath, mainPath, JPEGPath
def get_packages_data(package_dir): choice_num = 3000 images_list = [] xml_list = [] for package in package_dir: jpg_path = os.path.join(package, 'JPEGImages') xml_path = os.path.join(package, "Annotations") for image in os.listdir(jpg_path): images_list.append(os.path.join(jpg_path, image)) xml_list.append(os.path.join(xml_path, image[:-4] + ".xml")) index = [i for i in range(len(images_list))] random.shuffle(index) new_package = '/home/hyl/data/ljk/github-pro/zjai-com/data/train_data/random_choice_data_{}'.format( choice_num) new_jpg_path = os.path.join(new_package, "JPEGImages") new_xml_path = os.path.join(new_package, "Annotations") io_utils.mkdir(new_package) io_utils.mkdir(new_jpg_path) io_utils.mkdir(new_xml_path) for i in index[:choice_num]: io_utils.copy(images_list[i], new_jpg_path) io_utils.copy(xml_list[i], new_xml_path)
def save_annotations(save_dir, im_info, data): dom = make_xml(im_info, data) io_utils.mkdir(save_dir) xml_path = os.path.join(save_dir, im_info["name"].replace(".jpg", ".xml")) with open(xml_path, 'w+') as f: dom.writexml(f, addindent='', newl='', encoding='utf-8')
def save(xmlpath, tree): # xml = tostring(element, pretty_print=True) mkdir(osp.dirname(xmlpath.replace("Annotations", "Annotations1"))) tree.write(xmlpath.replace("Annotations", "Annotations1"), encoding="utf-8", xml_declaration=True)