1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
from xml.etree.ElementTree import ElementTree, Element import xml.etree.ElementTree as ET import os from tqdm import tqdm
def change_box(update_path, save_path, filename, box_n, new_value): update_tree = ET.parse(update_path + filename) root = update_tree.getroot() size = root.find('size') object = root.find('object') bndbox = object.find("bndbox") _box_n = bndbox.find(box_n) _box_n.text = new_value update_tree.write(save_path + filename, encoding='utf-8')
def change_name(update_path, save_path, filename, new_name): update_tree = ET.parse(update_path + filename) root = update_tree.getroot() size = root.find('size') object = root.find('object') filename = root.find("filename") filename.text = new_name update_tree.write(save_path + new_name, encoding='utf-8')
def rotate_90(update_path, save_path, filename, new_name): """generate rotate 90° new label""" update_tree = ET.parse(update_path + filename) root = update_tree.getroot() size = root.find('size') object = root.find('object') bndbox = root.find('object').find("bndbox") width = int(size.find('width').text) height = int(size.find('height').text) xmin, ymin, xmax, ymax = bndbox.find("xmin"), bndbox.find("ymin"), bndbox.find( "xmax"), bndbox.find("ymax") _xmin, _ymin, _xmax, _ymax = int(xmin.text), int(ymin.text), int(xmax.text), int(ymax.text) xmin.text = str(_ymin) ymin.text = str(width - _xmax) xmax.text = str(_ymax) ymax.text = str(height - _xmin) update_tree.write(save_path + new_name, encoding='utf-8')
def rotate_180(update_path, save_path, filename, new_name): """generate rotate 180° new label""" update_tree = ET.parse(update_path + filename) root = update_tree.getroot() size = root.find('size') bndbox = root.find('object').find("bndbox") width = int(size.find('width').text) height = int(size.find('height').text) xmin, ymin, xmax, ymax = bndbox.find("xmin"), bndbox.find("ymin"), bndbox.find( "xmax"), bndbox.find("ymax") _xmin, _ymin, _xmax, _ymax = int(xmin.text), int(ymin.text), int(xmax.text), int(ymax.text) xmin.text = str(height - _xmax) ymin.text = str(width - _ymax) xmax.text = str(height - _xmin) ymax.text = str(width - _ymin) update_tree.write(save_path + new_name, encoding='utf-8')
def gen_fli_label(update_path, save_path, filename, new_name): """generate fli new label""" update_tree = ET.parse(update_path + filename) root = update_tree.getroot() size = root.find('size') bndbox = root.find('object').find("bndbox") width = int(size.find('width').text) height = int(size.find('height').text) xmin, ymin, xmax, ymax = bndbox.find("xmin"), bndbox.find("ymin"), bndbox.find("xmax"), bndbox.find("ymax") _xmin, _ymin, _xmax, _ymax = int(xmin.text), int(ymin.text), int(xmax.text), int(ymax.text) xmin.text = str(width - _xmax) ymin.text = str(_ymin) xmax.text = str(height - _xmin) ymax.text = str(_ymax) update_tree.write(save_path + new_name, encoding='utf-8')
def modify_quality(update_path, save_path, filename, new_name): """generate rotate 180° new label""" change_name(update_path, save_path, filename, new_name)
def gen_label_square(update_path, save_path, filename): update_tree = ET.parse(update_path + filename) root = update_tree.getroot() bndbox = root.find('object').find("bndbox") width = root.find('size').find('width') height = root.find('size').find('height') xmin, ymin, xmax, ymax = bndbox.find("xmin"), bndbox.find("ymin"), bndbox.find("xmax"), bndbox.find("ymax") _xmin, _ymin, _xmax, _ymax = int(xmin.text), int(ymin.text), int(xmax.text), int(ymax.text) _width, _height = int(width.text), int(height.text) max_w = max(_width, _height) width.text, height.text = str(max_w), str(max_w) if _height > _width: xmin.text = str(_xmin + (_height - _width) // 2) xmax.text = str(_xmax + (_height - _width) // 2) elif _width > _height: ymin.text = str(_ymin + (_width - _height) // 2) ymax.text = str(_ymax + (_width - _height) // 2) update_tree.write(save_path + filename, encoding='utf-8')
def gen_rotate_label(update_path, save_path, filename): new_name_90 = filename.split('.')[0] + "_r90" + ".xml" new_name_180 = filename.split('.')[0] + "_r180" + ".xml" modify_fli = filename.split('.')[0] + "_fli" + ".xml" rotate_90(update_path, save_path, filename, new_name=new_name_90) rotate_180(update_path, save_path, filename, new_name=new_name_180) gen_fli_label(update_path, save_path, filename, new_name=modify_fli)
def gen_modify_label(update_path, save_path, filename): modify_blur = filename.split('.')[0] + "_blur" + ".xml" modify_brighter = filename.split('.')[0] + "_brighter" + ".xml" modify_darker = filename.split('.')[0] + "_darker" + ".xml" modify_nosie = filename.split('.')[0] + "_nosie" + ".xml" modify_quality(update_path, save_path, filename, new_name=modify_blur) modify_quality(update_path, save_path, filename, new_name=modify_brighter) modify_quality(update_path, save_path, filename, new_name=modify_darker) modify_quality(update_path, save_path, filename, new_name=modify_nosie)
if __name__ == "__main__": update_path = "../data/picture/label/" for label_name in tqdm(os.listdir(update_path)): update_tree = ET.parse(update_path + label_name) root = update_tree.getroot() bndbox = root.find('object').find("bndbox") width = root.find('size').find('width') height = root.find('size').find('height') _width, _height = int(width.text), int(height.text) if _width != _height: gen_label_square(update_path, update_path, label_name) for label_name in tqdm(os.listdir(update_path)): gen_rotate_label(update_path, update_path, label_name) for label_name in tqdm(os.listdir(update_path)): gen_modify_label(update_path, update_path, label_name)
|