def read_labelme_kp_data(file_path, label_text_to_id=lambda x: int(x)): ''' Args: file_path: json file path label_text_to_id: int f(string) Returns: labels:[N] points:[N,2] ''' labels = [] points = [] image_info = {} with open(file_path, "r") as f: data = json.load(f) for d in data['shapes']: label = d['label'] point = d['points'][0] if label_text_to_id is not None: label = label_text_to_id(label) labels.append(label) points.append(point) image_info['width'] = int(data['imageWidth']) image_info['height'] = int(data["imageHeight"]) image_info['file_name'] = wmlu.base_name(data["imagePath"]) return image_info, labels, points
def read_json(file_path): annotations_list = [] image = {} with open(file_path, "r", encoding="gb18030") as f: print(file_path) data_str = f.read() try: json_data = json.loads(data_str) img_width = int(json_data["imgWidth"]) img_height = int(json_data["imgHeight"]) image["height"] = int(img_height) image["width"] = int(img_width) image["file_name"] = wmlu.base_name(file_path) for shape in json_data["objects"]: mask = np.zeros(shape=[img_height, img_width], dtype=np.uint8) all_points = np.array([shape["polygon"]]).astype(np.int32) if len(all_points) < 1: continue points = np.transpose(all_points[0]) print(points.shape) x, y = np.vsplit(points, 2) x = np.reshape(x, [-1]) y = np.reshape(y, [-1]) x = np.minimum(np.maximum(0, x), img_width - 1) y = np.minimum(np.maximum(0, y), img_height - 1) xmin = np.min(x) xmax = np.max(x) ymin = np.min(y) ymax = np.max(y) segmentation = cv.drawContours(mask, all_points, -1, color=(1), thickness=cv.FILLED) label = shape["label"] annotations_list.append({ "bbox": (xmin, ymin, xmax - xmin + 1, ymax - ymin + 1), "segmentation": segmentation, "category_id": label, "points_x": x, "points_y": y }) except: print(f"Read file {os.path.basename(file_path)} faild.") pass if len(annotations_list) > 2: mask = 1 - annotations_list[-1]['segmentation'] for i in reversed(range(len(annotations_list) - 1)): annotations_list[i]['segmentation'] = np.logical_and( annotations_list[i]['segmentation'], mask) mask = np.logical_and(mask, 1 - annotations_list[i]['segmentation']) return image, annotations_list
def filter(full_path,_): base_name = wmlu.base_name(full_path)+".png" save_path = os.path.join(save_dir,base_name) if os.path.exists(save_path): print(f"File {save_path} exists.") return False print(f"File {save_path} not exists.") return True
def filterVOCFilesByName(voc_files, file_names): res = [] for img_file, xml_file in voc_files: base_name = wmlu.base_name(img_file) if base_name not in file_names: continue res.append((img_file, xml_file)) return res
def copy_imgfiles(xml_dir, img_dir, img_suffix=".jpg"): xml_files = glob.glob(osp.join(xml_dir, "*.xml")) for xf in xml_files: base_name = wmlu.base_name(xf) img_name = base_name + img_suffix img_path = osp.join(img_dir, img_name) dst_img_path = osp.join(xml_dir, img_name) if osp.exists(img_path): wmlu.try_link(img_path, dst_img_path)
def add_foler_for_images(dir_path, save_dir): files = wmlu.recurse_get_filepath_in_dir(dir_path, suffix=".jpg;;..png;;.jpeg") for file in files: base_name = wmlu.base_name(file) cur_save_dir = os.path.join(save_dir, base_name) if not os.path.exists(cur_save_dir): os.makedirs(cur_save_dir) os.link(file, os.path.join(cur_save_dir, os.path.basename(file)))
def save_data(data, save_dir): for k, v in data.items(): tsd = osp.join(save_dir, str(k)) wmlu.create_empty_dir(tsd, False) for f in v: dir_name = wmlu.base_name(osp.dirname(f)) if dir_name == "": print(f"Get dir name faild {f}.") name = dir_name + "_" + osp.join(osp.basename(f)) os.link(f, osp.join(tsd, name))
def removeUnmatchVOCFiles(dir_path, image_sub_dir="JPEGImages", xml_sub_dir="Annotations", img_suffix=".jpg", shuffe=False): if image_sub_dir is not None: jpeg_dir = os.path.join(dir_path, image_sub_dir) else: jpeg_dir = dir_path if xml_sub_dir is not None: xml_dir = os.path.join(dir_path, xml_sub_dir) else: xml_dir = dir_path inputfilenames = wml_utils.recurse_get_filepath_in_dir(jpeg_dir, suffix=img_suffix) total_removed_jpgs = 0 total_removed_xmls = 0 good_xml_names = [] for file in inputfilenames: base_name = wmlu.base_name(file) xml_path = wmlu.change_suffix(file, "xml") if os.path.exists(xml_path): good_xml_names.append(base_name) else: print(f"remove {file}") total_removed_jpgs += 1 os.remove(file) for file in wml_utils.recurse_get_filepath_in_dir(xml_dir, suffix="xml"): base_name = wmlu.base_name(file) if base_name not in good_xml_names: total_removed_xmls += 1 print(f"remove {file}") os.remove(file) print( f"Total remove {total_removed_jpgs} images, total remove {total_removed_xmls} xmls." )
def get_files(dir_path, sub_dir_name): img_dir = os.path.join(dir_path, sub_dir_name,'images') label_dir = os.path.join(dir_path, sub_dir_name,"v2.0","polygons") res = [] json_files = wmlu.recurse_get_filepath_in_dir(label_dir,suffix=".json") for jf in json_files: base_name = wmlu.base_name(jf) igf = os.path.join(img_dir, base_name + ".jpg") if os.path.exists(igf): res.append((igf, jf)) else: print(f"ERROR: Find {igf} faild, json file is {jf}") return res
def trans_one(src_data, out_dir, fps, beg_idx=0, total_nr=-1, step=1): reader = wmli.VideoReader(src_data) if total_nr > 1: frames = [] for i in range(beg_idx, beg_idx + total_nr, step): frames.append(reader[i]) else: frames = [x for x in reader] save_name = wmlu.base_name(src_data, process_suffix=False) + ".gif" save_path = osp.join(out_dir, save_name) if osp.exists(save_path): print(f"Error {save_path} exists.") print(f"Save {save_path}") imageio.mimsave(save_path, frames, fps=fps)
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_loader = DataLoader(cfg=cfg, is_training=is_training) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.MODEL.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) model = PredictModel(cfg=cfg,is_remove_batch=True) model.restoreVariables() data_path = args.test_data_dir if os.path.isdir(data_path): files = wmlu.recurse_get_filepath_in_dir(data_path,suffix=".jpg") else: files = [data_path] save_path = args.save_data_dir wmlu.create_empty_dir(save_path,remove_if_exists=True) for file in files: img = wmli.imread(file) imgs = np.expand_dims(img,axis=0) res = model.predictImages(imgs) if RD_MASKS in res: r_img = odv.draw_bboxes_and_mask(img,res[RD_LABELS],res[RD_PROBABILITY],res[RD_BOXES], res[RD_MASKS], show_text=True) else: r_img = odv.bboxes_draw_on_imgv2(img,res[RD_LABELS],res[RD_PROBABILITY],res[RD_BOXES], text_fn=text_fn, show_text=True) name = wmlu.base_name(file) img_save_path = os.path.join(save_path,name+".png") wmli.imwrite(img_save_path,r_img)
def extract_data(self, data_path): if os.path.exists(self.tmp_dir): shutil.rmtree(self.tmp_dir) os.makedirs(self.tmp_dir) os.system(f"tar xvf {data_path} -C {self.tmp_dir}") files = wmlu.recurse_get_filepath_in_dir(self.tmp_dir, suffix=".index") if len(files) == 0: print(f"Error data {data_path}") return None file_name = wmlu.base_name(files[0]) '''ckpt_file = os.path.join(self.tmp_dir,"checkpoint") with open(ckpt_file,"w") as f: f.write(f"model_checkpoint_path: \"{file_name}\"\n") f.write(f"all_model_checkpoint_paths: \"{file_name}\"\n")''' return os.path.join(self.tmp_dir, file_name)
def read_data(self, dir_path): img_dir = dir_path if self.img_sub_dir is None else os.path.join( dir_path, self.img_sub_dir) label_dir = dir_path if self.label_sub_dir is None else os.path.join( dir_path, self.label_sub_dir) image_files = wmlu.recurse_get_filepath_in_dir(img_dir, suffix=self.img_suffix) self.files = [] for ifn in image_files: base_name = wmlu.base_name(ifn) label_path = os.path.join(label_dir, base_name + self.label_suffix) if not os.path.exists(label_path): print( f"ERROR: Find label file {label_path} for image {ifn} faild." ) continue else: self.files.append([ifn, label_path])
def get_files(dir_path, sub_dir_name): img_dir = os.path.join(dir_path, 'leftImg8bit', sub_dir_name) label_dir = os.path.join(dir_path, 'gtFine', sub_dir_name) dir_names = wmlu.get_subdir_in_dir(label_dir) res = [] for dir_name in dir_names: l_label_dir = os.path.join(label_dir, dir_name) l_img_dir = os.path.join(img_dir, dir_name) json_files = wmlu.recurse_get_filepath_in_dir(l_label_dir, suffix=".json") for jf in json_files: base_name = wmlu.base_name(jf)[:-15] + "leftImg8bit" igf = os.path.join(l_img_dir, base_name + ".png") if os.path.exists(igf): res.append((igf, jf)) else: print(f"ERROR: Find {igf} faild, json file is {jf}") return res
def trans_data(data_dir,save_dir,beg,end): global name_to_id_dict wmlu.show_dict(name_to_id_dict) wmlu.create_empty_dir(save_dir,remove_if_exists=False) def name_to_id(x): return name_to_id_dict[x] ignored_labels = ["construction--barrier--ambiguous","construction--barrier--separator"] data = MapillaryVistasData(label_text2id=name_to_id, shuffle=False, ignored_labels=ignored_labels, label_map=None, sub_dir_name="validation", #sub_dir_name="training", allowed_labels_fn=list(name_to_id_dict.keys())) data.read_data(data_dir) def filter(full_path,_): base_name = wmlu.base_name(full_path)+".png" save_path = os.path.join(save_dir,base_name) if os.path.exists(save_path): print(f"File {save_path} exists.") return False print(f"File {save_path} not exists.") return True for i,x in enumerate(data.get_items(beg,end,filter=filter)): full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x if len(category_ids) == 0: print(f"Skip {full_path}") continue new_mask = odm.dense_mask_to_sparse_mask(binary_mask,category_ids,default_label=255) base_name = wmlu.base_name(full_path)+".png" save_path = os.path.join(save_dir,base_name) new_mask = new_mask.astype(np.uint8) if os.path.exists(save_path): print(f"WARNING: File {save_path} exists.") cv2.imwrite(save_path,new_mask) sys.stdout.write(f"\r{i}")
max_file_nr = 20 input_size = (256, 480) if dir_path_in_target_file is None: dir_path_in_target_file = save_dir wmlu.create_empty_dir(save_dir) images = wmlu.recurse_get_filepath_in_dir(src_dir, suffix=".jpg") if max_file_nr is not None: images = images[:max_file_nr] #output_layers = [] output_layers = [ "shared_head/l2_normalize/Square", "shared_head/hw_regr/Conv_1/Conv2D", "shared_head/ct_regr/Conv_1/Conv2D", "shared_head/heat_ct/Conv_1/Conv2D" ], input_file_list = os.path.join(save_dir, "input.txt") with open(input_file_list, "w") as f: if output_layers is not None and len(output_layers) > 0: v = f"#{output_layers[0]}" for x in output_layers[1:]: v += f" {x}" v += "\n" f.write(v) for file in images: raw_name = wmlu.base_name(file) + ".raw" raw_path = os.path.join(save_dir, raw_name) save_raw_path = os.path.join(dir_path_in_target_file, raw_name) file_to_snpe_raw(file, raw_path, input_size=input_size) f.write(save_raw_path + "\n")
help='output rawframe directory') parser.add_argument('--ext', default=".gif", type=str, help='file ext') parser.add_argument("--reverse", default=False, action="store_true", help="resume training") args = parser.parse_args() return args if __name__ == "__main__": args = parse_args() files = wmlu.recurse_get_filepath_in_dir(args.src_file, suffix=args.ext) base_names = [wmlu.base_name(x) for x in files] subdirs = wmlu.get_subdir_in_dir(args.src_dir, append_self=False, absolute_path=False) dirs_need_to_move = [] move_data = [] dirs_dont_move = [] for sd in subdirs: rdir = osp.join(args.src_dir, sd) ddir = args.out_dir tdir = wmlu.base_name(sd, process_suffix=False) move = False if args.reverse: if tdir not in base_names: move = True
dataset = SemanticData(img_suffix=".jpg",label_suffix=".png",img_sub_dir="boe_labels",label_sub_dir="boe_labels") dataset.read_data("/home/wj/ai/mldata/boesemantic") save_dir = wmlu.home_dir("ai/tmp/boe_images2") wmlu.create_empty_dir(save_dir,remove_if_exists=False) color_map = fill_colormap_and_names("/home/wj/ai/mldata/mapillary_vistas/config_v2.0.json") def text_fn(l): if l in ID_TO_READABLE_NAME: return ID_TO_READABLE_NAME[l] else: return "NA" def color_fn(l): return color_map[l*3:l*3+3] legend_img = draw_legend(list(ID_TO_NAME.keys()),text_fn,img_size=(2448,300),color_fn=color_fn) for ifn,img,mask in dataset.get_items(): base_name = wmlu.base_name(ifn) wmlu.safe_copy(ifn,save_dir) rgb_mask = convert_semantic_to_rgb(mask,color_map,True) if rgb_mask.shape[0] != legend_img.shape[0]: legend_img = wmli.resize_img(legend_img,(legend_img.shape[1],rgb_mask.shape[0])) rgb_mask = np.concatenate([rgb_mask,legend_img],axis=1) mask_path = os.path.join(save_dir,base_name+".png") wmli.imwrite(mask_path,rgb_mask) mask_image = draw_semantic_on_image(img,mask,color_map,ignored_label=255) mask_image = np.concatenate([mask_image,legend_img],axis=1) mask_image_path = os.path.join(save_dir,base_name+"1.png") wmli.imwrite(mask_image_path,mask_image)
help='resize image short side length keeping ratio') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() if not osp.isdir(args.out_dir): print(f'Creating folder: {args.out_dir}') os.makedirs(args.out_dir) _sub_dirs = wmlu.recurse_get_subdir_in_dir(args.src_dir,append_self=True) datas = [] for sd in _sub_dirs: rsd = osp.join(args.src_dir,sd) files = glob.glob(osp.join(rsd,"*."+args.ext)) if len(files)>0: for f in files: v_save_dir = osp.join(args.out_dir,sd) v_save_path = osp.join(v_save_dir,wmlu.base_name(f)) wmlu.create_empty_dir(v_save_dir,remove_if_exists=False) datas.append([f,sd,v_save_path]) print(f"Total find {len(datas)} files.") sys.stdout.flush() pool = Pool(args.num_worker) pool.map( extract_frame, datas)
def read_labelme_data(file_path, label_text_to_id=lambda x: int(x), use_semantic=True): annotations_list = [] image = {} with open(file_path, "r", encoding="gb18030") as f: print(file_path) data_str = f.read() try: json_data = json.loads(data_str) img_width = int(json_data["imageWidth"]) img_height = int(json_data["imageHeight"]) image["height"] = int(img_height) image["width"] = int(img_width) image["file_name"] = wmlu.base_name(file_path) for shape in json_data["shapes"]: mask = np.zeros(shape=[img_height, img_width], dtype=np.uint8) all_points = np.array([shape["points"]]).astype(np.int32) if len(all_points) < 1: continue points = np.transpose(all_points[0]) x, y = np.vsplit(points, 2) x = np.reshape(x, [-1]) y = np.reshape(y, [-1]) x = np.minimum(np.maximum(0, x), img_width - 1) y = np.minimum(np.maximum(0, y), img_height - 1) xmin = np.min(x) xmax = np.max(x) ymin = np.min(y) ymax = np.max(y) segmentation = cv.drawContours(mask, all_points, -1, color=(1), thickness=cv.FILLED) if label_text_to_id is not None: label = label_text_to_id(shape["label"]) else: label = shape["label"] annotations_list.append({ "bbox": (xmin, ymin, xmax - xmin + 1, ymax - ymin + 1), "segmentation": segmentation, "category_id": label, "points_x": x, "points_y": y }) except: print(f"Read file {os.path.basename(file_path)} faild.") pass if use_semantic: ''' Each pixel only belong to one classes, and the latter annotation will overwrite the previous ''' if len(annotations_list) > 2: mask = 1 - annotations_list[-1]['segmentation'] for i in reversed(range(len(annotations_list) - 1)): annotations_list[i]['segmentation'] = np.logical_and( annotations_list[i]['segmentation'], mask) mask = np.logical_and(mask, 1 - annotations_list[i]['segmentation']) return image, annotations_list
help="path to test data dir") parser.add_argument("--save_dir", default="/2_data/wj/mldata/coco/coco_results", type=str, help="path to save data dir") return parser if __name__ == "__main__": args = default_argument_parser().parse_args() save_path = args.save_dir dataset = PascalVOCData() # data_path0 = get_data_dir2("annotationed_data/verify_p03_1020_1_proc_m") data_path = args.test_dir if not dataset.read_data(data_path): exit(0) wmlu.create_empty_dir(save_path, remove_if_exists=True, yes_to_all=True) for data in dataset.get_items(): full_path, shape, category_ids, category_names, boxes, binary_masks, area, is_crowd, num_annotations_skipped = data print(f"Process {full_path}") img = wmli.imread(full_path) base_name = wmlu.base_name(full_path) + "_a.jpg" save_file_path = os.path.join(save_path, base_name) img = odv.draw_bboxes(img, classes=category_names, bboxes=boxes, show_text=True, text_color=(255., 0., 0.)) wmli.imsave(save_file_path, img)
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_loader = DataLoader(cfg=cfg, is_training=is_training) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) model = PredictModel(cfg=cfg, is_remove_batch=True) model.restoreVariables() save_path = args.save_data_dir wmlu.create_empty_dir(save_path, remove_if_exists=True) metrics = COCOEvaluation(num_classes=90) items = eval_dataset() for data in items: full_path, shape, gt_labels, category_names, gt_boxes, binary_masks, area, is_crowd, num_annotations_skipped = data img = wmli.imread(full_path) imgs = np.expand_dims(img, axis=0) res = model.predictImages(imgs) if RD_MASKS in res: r_img = odv.draw_bboxes_and_mask(img, res[RD_LABELS], res[RD_PROBABILITY], res[RD_BOXES], res[RD_MASKS], show_text=True) else: r_img = odv.bboxes_draw_on_imgv2(img, res[RD_LABELS], res[RD_PROBABILITY], res[RD_BOXES], text_fn=text_fn, show_text=True) kwargs = {} kwargs['gtboxes'] = gt_boxes kwargs['gtlabels'] = gt_labels kwargs['boxes'] = res[RD_BOXES] kwargs['labels'] = res[RD_LABELS] kwargs['probability'] = res[RD_PROBABILITY] kwargs['img_size'] = shape metrics(**kwargs) if model.step % 100 == 0: metrics.show() name = wmlu.base_name(full_path) img_save_path = os.path.join(save_path, name + ".png") wmli.imwrite(img_save_path, r_img) metrics.show()