def gen_xml(img_id): img_desc = coco.loadImgs(img_id)[0] fname = img_desc["file_name"] # copy image shutil.copy(f'{src_img_dir}/{fname}', f'{dst_img_dir}/{fname}') # process annotations bboxs = [] ann_ids = coco.getAnnIds(imgIds=img_desc['id'], catIds=cat_ids) ann_descs = coco.loadAnns(ann_ids) for ann_desc in ann_descs: cat = COCO_CAT_NAMES[ann_desc['category_id']] if cat in cat_names: bbox = ann_desc['bbox'] xmin = int(bbox[0]) ymin = int(bbox[1]) xmax = int(bbox[2]) + int(bbox[0]) ymax = int(bbox[3]) + int(bbox[1]) bboxs.append((cat, xmin, ymin, xmax, ymax)) # get image info img_path = f'{prefix_folder}{fname}' img_w = img_desc["width"] img_h = img_desc["height"] img_d = 3 # save annotation info to xml file dst_ann_path = f'{dst_ann_dir}/{fname[:-4]}.xml' writer = pascal_voc_writer.Writer(img_path, img_w, img_h, img_d) for bbox in bboxs: writer.addObject(*bbox) writer.save(dst_ann_path)
def gen_xml(ann_path): fname = ann_path.stem with open(ann_path, 'r') as f: lines = f.readlines() # calc bbox bboxs = [] for line in lines: obj = line.strip('\n').split(',') if obj[5] in cat_dict: xmin = int(obj[0]) ymin = int(obj[1]) xmax = int(obj[2]) + int(obj[0]) ymax = int(obj[3]) + int(obj[1]) bboxs.append((cat_dict[obj[5]], xmin, ymin, xmax, ymax)) # get image info img = cv2.imread(f'{src_img_dir}/{fname}.jpg') img_path = f'{prefix_folder}{fname}.jpg' img_w = img.shape[1] img_h = img.shape[0] img_d = img.shape[2] # save annotation info to xml file dst_ann_path = f'{dst_ann_dir}/{fname}.xml' writer = pascal_voc_writer.Writer(img_path, img_w, img_h, img_d) for bbox in bboxs: writer.addObject(*bbox) writer.save(dst_ann_path)
def gen_xml(json_path): fname = f'{json_path.parent.stem}/{json_path.name[:-21]}_leftImg8bit' with open(json_path, 'r') as f: data = json.load(f) # calc bbox bboxs = [] for obj in data["objects"]: label = 'person' if obj["label"] == 'rider' else obj["label"] polygon = obj["polygon"] if label in cat_names: x_coords, y_coords = zip(*polygon) xmin = min(x_coords) ymin = min(y_coords) xmax = max(x_coords) ymax = max(y_coords) bboxs.append((label, xmin, ymin, xmax, ymax)) # get image info img_path = f'{prefix_folder}{fname}.png' img_w = data['imgWidth'] img_h = data['imgHeight'] img_d = 3 # save annotation info to xml file dst_ann_path = f'{dst_ann_dir}/{fname}.xml' writer = pascal_voc_writer.Writer(img_path, img_w, img_h, img_d) for bbox in bboxs: writer.addObject(*bbox) writer.save(dst_ann_path)
def save_project_as_pascal_voc_detection(save_path, project: Project): # Create root pascal 'datasets' folders for dataset in project.datasets: pascal_dataset_path = os.path.join(save_path, dataset.name) pascal_dataset_relative_path = os.path.relpath(pascal_dataset_path, save_path) images_dir = os.path.join(pascal_dataset_path, 'JPEGImages') anns_dir = os.path.join(pascal_dataset_path, 'Annotations') lists_dir = os.path.join(pascal_dataset_path, 'ImageSets/Layout') fs_utils.mkdir(pascal_dataset_path) for subdir in [ 'ImageSets', # Train list, Val list, etc. 'ImageSets/Layout', 'Annotations', 'JPEGImages' ]: fs_utils.mkdir(os.path.join(pascal_dataset_path, subdir)) samples_by_tags = defaultdict(list) # TRAIN: [img_1, img2, ..] for item_name in dataset: img_path, ann_path = dataset.get_item_paths(item_name) no_ext_name = fs_utils.get_file_name(item_name) pascal_img_path = os.path.join(images_dir, no_ext_name + OUT_IMG_EXT) pascal_ann_path = os.path.join(anns_dir, no_ext_name + XML_EXT) if item_name.endswith(OUT_IMG_EXT): fs_utils.copy_file(img_path, pascal_img_path) else: img = image_utils.read(img_path) image_utils.write(pascal_img_path, img) ann = Annotation.load_json_file(ann_path, project_meta=project.meta) # Read tags for images lists generation for tag in ann.img_tags: samples_by_tags[tag.name].append( (no_ext_name, len(ann.labels))) writer = pascal_voc_writer.Writer( path=pascal_dataset_relative_path, width=ann.img_size[1], height=ann.img_size[0]) for label in ann.labels: obj_class = label.obj_class rect: Rectangle = label.geometry.to_bbox() writer.addObject(name=obj_class.name, xmin=rect.left, ymin=rect.top, xmax=rect.right, ymax=rect.bottom) writer.save(pascal_ann_path) save_images_lists(lists_dir, samples_by_tags)
def gen_xml(json_line): data = eval(json_line) # calc bboxs bboxs = [] img_id = data['ID'] for gtbox in data['gtboxes']: if gtbox['tag'] == 'person': bbox = gtbox['vbox'] xmin = int(bbox[0]) ymin = int(bbox[1]) xmax = int(bbox[2]) + int(bbox[0]) ymax = int(bbox[3]) + int(bbox[1]) bboxs.append(('person', xmin, ymin, xmax, ymax)) # get image info img = cv2.imread(f'{src_img_dir}/{img_id}.jpg') img_path = f'{prefix_folder}{img_id}.jpg' img_w = img.shape[1] img_h = img.shape[0] img_d = img.shape[2] # save annotation info to xml file dst_ann_path = f'{dst_ann_dir}/{img_id}.xml' writer = pascal_voc_writer.Writer(img_path, img_w, img_h, img_d) for bbox in bboxs: writer.addObject(*bbox) writer.save(dst_ann_path)
def process_detection_intermeddiate(self, frame, orig_box, image_path): minx, miny, maxx, maxy, label, accuracy = orig_box outputFrame = self.tf_detector.DisplayDetection(frame, orig_box) if self.config.tf_od_frame_write: cv2.imwrite(image_path, outputFrame) if self.config.tf_od_annotation_write: imH, imW, _ = frame.shape writer = pascal_voc_writer.Writer(image_path, imW, imH) minX, minY, maxX, maxY, klass, confidence = orig_box writer.addObject(klass, minX, minY, maxX, maxY) writer.save(image_path.replace('.jpg', '.xml')) if self.config.show_fps: cv2.putText(outputFrame, "%.2f fps" % self.fps.fps, (10, outputFrame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 255), 1) self.output_frame.enqueue(outputFrame)
def test(self): writer = pascal_voc_writer.Writer( path='hello.jpg', width=600, height=400, ) writer.addObject( name='category1', xmin=10, ymin=20, xmax=30, ymax=40, ) writer.save('this.xml')
def set_writer(in_image_path, h, w, main_dir = default_dir): global image_path, out_xml, writer image_path = in_image_path writer = pvw.Writer(image_path, h, w) base_name = image_path.split('/')[-1] out_xml = main_dir+'{}.xml'.format(base_name.split('.')[0])
im = Image.open('../dataset/train_images/' + your_list[i, 0] + '.jpg') img_x, img_y = im.size xmin = round(float(your_list[i, 4]) * img_x) xmax = round(float(your_list[i, 5]) * img_x) ymin = round(float(your_list[i, 6]) * img_y) ymax = round(float(your_list[i, 7]) * img_y) # if xmax - xmin > img_x / 2: print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$${}'.format(imgname)) if ymax - ymin > img_y / 2: print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$${}'.format(imgname)) writer = pasc.Writer(your_list[i, 0] + '.jpg', img_x, img_y) writer.addObject(in_person[classname], xmin, ymin, xmax, ymax) print(good_img) if good_img == 1: if your_list[i + 1, 0] != imgname: writer.save('xml_ann/' + imgname + '.xml') # print(' saved : '+imgname+'.xml') good_img = 0 if good_img == 1: writer.save('xml_ann/' + imgname + '.xml') #print(' saved last: '+imgname+'.xml')