def generate_segmented_data(xml_list, drawing_dir, drawing_segment_dir, segment_params, text_xml_dir, symbol_dict, include_text_as_class, include_text_orientation_as_class, drawing_resize_scale, prefix): """ 폴더 내 원본 이미지 도면들을 분할하고 분할 정보를 리스트로 저장 Arguments: xml_list (list): xml 파일 리스트 drawing_dir (string): 원본 도면 이미지가 존재하는 폴더 drawing_segment_dir (string): 분할된 이미지를 저장할 폴더 segment_params (list): 분할 파라메터 [가로 크기, 세로 크기, 가로 stride, 세로 stride] text_xml_dir (string): text xml 파일의 폴더 (include_text_as_calss가 False면 사용하지 않음) symbol_dict (dict): symbol 이름을 key로, id를 value로 갖는 dict include_text_as_class (bool): text 데이터를 class로 추가할 것인지 drawing_resize_scale (float): 전체 도면 조정 스케일 prefix (string): train/val/test 중 하나. 이미지 저장 폴더명 생성에 필요 Return: xml에 있는 전체 도면에서 분할된 도면의 전체 정보 [sub_img_name, symbol_name, xmin, ymin, xmax, ymax] """ entire_segmented_info = [] for xmlPath in xml_list: print(f"Proceccing {xmlPath}...") fname, ext = os.path.splitext(xmlPath) if ext.lower() != ".xml": continue xmlReader = symbol_xml_reader(xmlPath) img_filename, width, height, depth, object_list = xmlReader.getInfo() for i in range(len(object_list)): object_list[i][0] = symbol_dict[object_list[i][0].split("-")[0]] img_file_path = os.path.join(drawing_dir, img_filename) if include_text_as_class == True and os.path.exists( os.path.join(text_xml_dir, os.path.basename(xmlPath))): text_xml_reader_obj = text_xml_reader( os.path.join(text_xml_dir, os.path.basename(xmlPath))) _, _, _, _, txt_object_list = text_xml_reader_obj.getInfo() segmented_objects_info = segment_images( img_file_path, drawing_segment_dir, object_list, txt_object_list, include_text_orientation_as_class, symbol_dict, segment_params, drawing_resize_scale, prefix) else: segmented_objects_info = segment_images( img_file_path, drawing_segment_dir, object_list, None, None, symbol_dict, segment_params, drawing_resize_scale, prefix) entire_segmented_info.extend(segmented_objects_info) return entire_segmented_info
xml_dir = "D:/Test_Models/PNID/HyundaiEng/210518_Data/Symbol_XML" drawing_img_dir = "D:/Test_Models/PNID/HyundaiEng/210518_Data/Drawing/JPG" is_text_xml = False xml_filenames = os.listdir(xml_dir) entire_objects = [] for xml_filename in xml_filenames: print(xml_filename) name_only = xml_filename.split(".")[0] xml_path = os.path.join(xml_dir, name_only + ".xml") drawing_path = os.path.join(drawing_img_dir, name_only + ".jpg") if is_text_xml == True: text_xml_reader_obj = text_xml_reader(xml_path) filename, width, height, depth, object_list = text_xml_reader_obj.getInfo( ) else: symbol_xml_reader_obj = symbol_xml_reader(xml_path) filename, width, height, depth, object_list = symbol_xml_reader_obj.getInfo( ) bbox = [[x[0], x[1], x[2], x[3] - x[1], x[4] - x[2]] for x in object_list] entire_objects.extend(bbox) unique_labels = set([x[0] for x in entire_objects]) occurences = {} mean_diagonal_lengths = {} for unique_label in unique_labels: current_lables = [x[0] for x in entire_objects if x[0] == unique_label]
import os import random import pickle from Common.pnid_xml import text_xml_reader # Text XML에 존재하는 오류(앞뒤공백, multiline 박스 분할, 박스 크기 조정, 오류가 있는 object 제거)를 수정하여 다시 XML로 출력 text_xml_dir = "D:/Test_Models/PNID/HyundaiEng/210520_Data/_Text_XML_before_correction" drawing_img_dir = "D:/Test_Models/PNID/HyundaiEng/210520_Data/Drawing/JPG" processed_text_xml_dir = "D:/Test_Models/PNID/HyundaiEng/210520_Data/Text_XML/" text_xmls = [os.path.join(text_xml_dir, x) for x in os.listdir(text_xml_dir)] for text_xml in text_xmls: print(text_xml) text_xml_obj = text_xml_reader(text_xml) text_xml_obj.error_correction(drawing_img_dir, remove_spacing=True, newline_separation=True, remove_blank_pixel=False, remove_blank_threshold=0.7, margin=5, remove_none_string=True, remove_object_out_of_img=True) filename = os.path.basename(text_xml) text_xml_obj.write_xml(os.path.join(processed_text_xml_dir, filename))
def parse_test_gt_xmls(self): """ Grount Truth를 저장하고 있는 xml들을 읽어서 coco json 형식과 dict 형식으로 반환 Return: gt_json (dict): 모든 test 도면의 GT 정보가 coco 형식으로 저장된 dict gt_result (dict): 모든 test 도면의 GT 정보가 image_name을 key로, bbox들이 value로 저장된 dict """ gt_json = {} gt_result_json = {} test_image_filenames = self.dt_result.keys() images = [] annotations = [] categories = [] for sym_name, sym_id in self.symbol_dict.items(): categories.append({"id": sym_id, "name": sym_name}) image_id = 1 object_id = 1 for test_image_filename in test_image_filenames: symbol_xml = symbol_xml_reader( os.path.join(self.symbol_xml_dir, f"{test_image_filename}.xml")) filename, width, height, depth, object_list = symbol_xml.getInfo() if self.include_text_as_class == True: text_xml_path = os.path.join(self.text_xml_dir, f"{test_image_filename}.xml") if os.path.exists(text_xml_path) == True: text_xml = text_xml_reader(text_xml_path) _, _, _, _, text_object_list = text_xml.getInfo() if self.include_text_orientation_as_class == True: converted_text_object_list = [[ "text", x[1], x[2], x[3], x[4] ] for x in text_object_list if x[5] == 0] converted_text_object_list += [[ "text_rotated", x[1], x[2], x[3], x[4] ] for x in text_object_list if x[5] == 90] converted_text_object_list += [[ "text_rotated_45", x[1], x[2], x[3], x[4] ] for x in text_object_list if x[5] == 45] else: converted_text_object_list = [[ "text", x[1], x[2], x[3], x[4] ] for x in text_object_list] object_list = object_list + converted_text_object_list image = { "file_name": f"{test_image_filename}.jpg", "id": image_id, "width": width, "height": height # url, date 등 사용하지 않는 데이터 삭제 } images.append(image) annotations_per_image = [] for object in object_list: # [name, xmin, ymin, xmax, ymax] x = object[1] y = object[2] width = object[3] - object[1] height = object[4] - object[2] name = object[0].split(sep='-', maxsplit=1)[0] class_index = self.symbol_dict[name] obj = { "bbox": [x, y, width, height], "category_id": class_index, "image_id": image_id, 'id': object_id, 'area': width * height, 'segmentation': [], "iscrowd": 0, "ignore": 0, } annotations.append(obj) annotations_per_image.append(obj) object_id += 1 gt_result_json[test_image_filename] = annotations_per_image image_id += 1 gt_json['annotations'] = annotations gt_json['images'] = images gt_json['categories'] = categories return gt_json, gt_result_json