def main(): dets = [] # Get img_ids from lvis, instead. img_ids = coco.getImgIds() num_images = len(img_ids) # import pdb; pdb.set_trace() prog_bar = mmcv.ProgressBar(len(img_ids)) for i, img_id in enumerate(img_ids): # ipdb.set_trace() if DEBUG_ is True: if i > 100: break img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img.copy() for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id'] gt_img = add_box(gt_img, bbox, 1.00, cat_id) img_name = '{}_gt.jpg'.format(str(img_id).zfill(12)) cv2.imwrite(os.path.join(OUT_PATH, img_name), gt_img) prog_bar.update()
def show_json_angle(): """ according json file to visualization :return: """ img_path = '/Dataset/DOTA_TEST/images/' json_path = '/Dataset/DOTA_TEST/DOTA_DOTA_TEST_angle.json' save_path = '/Dataset/DOTA_TEST/show/' import pycocotools.coco as coco coco = coco.COCO(json_path) images = coco.getImgIds() image_num = len(os.listdir(img_path)) for idx in range(image_num): img_id = images[idx] file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] pics_path = os.path.join(img_path, file_name) ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) num_objs = len(anns) img = cv2.imread(pics_path) for k in range(num_objs): bbox = np.array(anns[k]['bbox']) # print((bbox[0], bbox[1]), (bbox[2], bbox[3]), bbox[4]/math.pi*180) bbox8 = cv2.boxPoints(((bbox[0], bbox[1]), (bbox[2], bbox[3]), bbox[4] / math.pi * 180)) # cat = cate[count] img = add_bbox(img, bbox8, anns[k]['category_id']) cv2.imwrite(save_path + file_name, img) print('\r{}/{}'.format(idx, image_num), end='')
def main(): print("\nDATA:{}\nOUT_TYPE:{}\n".format(DATA, OUT_TYPE)) dets = [] img_ids = coco.getImgIds() num_images = len(img_ids) # ipdb.set_trace() for i, img_id in enumerate(img_ids): if i % 10 == 0: print("{}/{}".format(i, len(img_ids)), end="\r") # ipdb.set_trace() if DEBUG_ and i > DEBUG_: break img_info = coco.loadImgs(ids=[img_id])[0] if DATA == "lvis": img_name = img_info['file_name'] elif DATA == "coco": img_name = "COCO_val2014_" + img_info['file_name'] img_path = IMG_PATH + img_name img = cv2.imread(img_path) gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img.copy() for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) gt_img = add_box(gt_img, bbox, 1.00) img_name = '{}_{}.jpg'.format(str(img_id).zfill(12), OUT_NAME_SUFFIX) cv2.imwrite(os.path.join(OUT_PATH, img_name), gt_img)
def get_coco_dicts(data_dir): if 'train' in data_dir: file_path = '/media/tangyp/Data/coco/train2014' elif 'val' in data_dir: file_path = '/media/tangyp/Data/coco/val2014' json_file = data_dir coco = COCO(json_file) catIds = coco.getCatIds(catNms=['person']) imgIds = coco.getImgIds(catIds=catIds) imgs = coco.loadImgs(imgIds) dataset_dicts = [] for img in imgs: dataset_dict = {} new_img = {'file_name': os.path.join(file_path, img['file_name']), 'height': img['height'], 'width': img['width'], 'image_id': img['id']} annId = coco.getAnnIds(imgIds=img['id']) anns = coco.loadAnns(ids=annId) annotation = {} annotation['annotations'] = [] for ann in anns: new_ann = {'iscrowd': ann['iscrowd'], 'bbox': ann['bbox'], 'category_id': ann['category_id'], 'segmentation': ann['segmentation'], 'bbox_mode': BoxMode(1)} annotation['annotations'].append(new_ann) dataset_dict.update(new_img) dataset_dict.update(annotation) dataset_dicts.append(dataset_dict) return dataset_dicts
def main(): img_seq = [] # Get img_ids from lvis, instead. img_ids = coco.getImgIds() num_images = len(img_ids) # import ipdb; ipdb.set_trace() dets = [] dets.append(coco.loadRes(PRE_PATH)) prog_bar = mmcv.ProgressBar(len(img_ids)) for i, img_id in enumerate(img_ids): # ipdb.set_trace() if DEBUG_ is True: if i == DEBUG_FRAMES: break img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) # Create a gt labeled img. gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id'] gt_img = add_box(gt_img, bbox, 1.00, cat_id) gt_img = add_to_canvas(gt_img) # Create a predictions labeled img. for k in range(len(dets)): pred_ids = dets[k].getAnnIds(imgIds=[img_id]) preds = dets[k].loadAnns(pred_ids) pred_img = img.copy() for j, pred in enumerate(preds): bbox = _coco_box_to_bbox(pred['bbox']) sc = pred['score'] cat_id = pred['category_id'] if sc > VIS_THR: pred_img = add_box(pred_img, bbox, sc, cat_id) # ipdb.set_trace() pred_img = add_to_canvas(pred_img) # Create a super image and save it. sup_img = np.concatenate((gt_img, pred_img), axis=1) sup_img_name = 'pre_{}.jpg'.format(str(img_id).zfill(12)) cv2.imwrite(os.path.join(OUT_PATH, sup_img_name), sup_img) img_seq.append(sup_img) prog_bar.update()
def coco(writer, name_index, profile, row, verify=False): root = os.path.expanduser(os.path.expandvars(row['root'])) year = str(row['year']) name = profile + year path = os.path.join(root, 'annotations', 'instances_%s.json' % name) if not os.path.exists(path): tf.logging.warn(path + ' not exists') return False import pycocotools.coco coco = pycocotools.coco.COCO(path) catIds = coco.getCatIds(catNms=list(name_index.keys())) cats = coco.loadCats(catIds) id_index = dict((cat['id'], name_index[cat['name']]) for cat in cats) imgIds = coco.getImgIds() path = os.path.join(root, name) imgs = coco.loadImgs(imgIds) _imgs = list(filter(lambda img: os.path.exists(os.path.join(path, img['file_name'])), imgs)) if len(imgs) > len(_imgs): tf.logging.warn('%d of %d images not exists' % (len(imgs) - len(_imgs), len(imgs))) cnt_noobj = 0 for img in tqdm.tqdm(_imgs): annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None) anns = coco.loadAnns(annIds) if len(anns) <= 0: cnt_noobj += 1 continue imagepath = os.path.join(path, img['file_name']) width, height = img['width'], img['height'] imageshape = [height, width, 3] objects_class = np.array([id_index[ann['category_id']] for ann in anns], dtype=np.int64) objects_coord = [ann['bbox'] for ann in anns] objects_coord = [(x, y, x + w, y + h) for x, y, w, h in objects_coord] objects_coord = np.array(objects_coord, dtype=np.float32) if verify: if not verify_coords(objects_coord, imageshape): tf.logging.error('failed to verify coordinates of ' + imagepath) continue if not verify_image_jpeg(imagepath, imageshape): tf.logging.error('failed to decode ' + imagepath) continue assert len(objects_class) == len(objects_coord) example = tf.train.Example(features=tf.train.Features(feature={ 'imagepath': tf.train.Feature(bytes_list=tf.train.BytesList(value=[tf.compat.as_bytes(imagepath)])), 'imageshape': tf.train.Feature(int64_list=tf.train.Int64List(value=imageshape)), 'objects': tf.train.Feature(bytes_list=tf.train.BytesList(value=[objects_class.tostring(), objects_coord.tostring()])), })) writer.write(example.SerializeToString()) if cnt_noobj > 0: tf.logging.warn('%d of %d images have no object' % (cnt_noobj, len(_imgs))) return True
def load_ss_data(coco_dir, data_name, cache_dir, input_size=None): """セマンティックセグメンテーションのデータの読み込み。""" from pycocotools import coco, mask as cocomask coco_dir = pathlib.Path(coco_dir) cache_dir = pathlib.Path(cache_dir) if isinstance(input_size, int): input_size = (input_size, input_size) coco = coco.COCO( str(coco_dir / "annotations" / f"instances_{data_name}.json")) class_names = [c["name"] for c in coco.loadCats(coco.getCatIds())] jsonclassid_to_index = { c["id"]: class_names.index(c["name"]) for c in coco.loadCats(coco.getCatIds()) } X, y = [], [] for entry in utils.tqdm(coco.loadImgs(coco.getImgIds()), desc="load_ss_data"): dirname, filename = entry["coco_url"].split("/")[-2:] save_path = cache_dir / dirname / (filename + ".npy") X.append(coco_dir / dirname / filename) y.append(save_path) if not save_path.exists(): # 読み込み objs = coco.loadAnns( coco.getAnnIds(imgIds=entry["id"], iscrowd=None)) mask = np.zeros( (entry["height"], entry["width"], len(class_names)), dtype=np.uint8) for obj in objs: if obj.get("ignore", 0) == 1: continue rle = cocomask.frPyObjects(obj["segmentation"], entry["height"], entry["width"]) m = cocomask.decode(rle) class_id = jsonclassid_to_index[obj["category_id"]] mask[:, :, class_id] |= m mask = np.where(mask, np.uint8(255), np.uint8(0)) # リサイズ if input_size is not None: mask = ndimage.resize(mask, input_size[1], input_size[0]) # 保存 save_path.parent.mkdir(parents=True, exist_ok=True) np.save(str(save_path), mask) return np.array(X), np.array(y), class_names
def load_od_data(coco_dir, data_name, use_crowded): """物体検出のデータの読み込み。""" import pycocotools.coco coco_dir = pathlib.Path(coco_dir) coco = pycocotools.coco.COCO( str(coco_dir / "annotations" / f"instances_{data_name}.json")) class_names = [c["name"] for c in coco.loadCats(coco.getCatIds())] jsonclassid_to_index = { c["id"]: class_names.index(c["name"]) for c in coco.loadCats(coco.getCatIds()) } labels = [] for entry in coco.loadImgs(coco.getImgIds()): dirname, filename = entry["coco_url"].split("/")[-2:] objs = coco.loadAnns( coco.getAnnIds(imgIds=entry["id"], iscrowd=None if use_crowded else False)) bboxes, classes, areas, crowdeds = [], [], [], [] width, height = entry["width"], entry["height"] for obj in objs: if obj.get("ignore", 0) == 1: continue x, y, w, h = obj["bbox"] bbox = np.array([x, y, x + w, y + h]) / np.array( [width, height, width, height]) bbox = np.clip(bbox, 0, 1) if (bbox[:2] < bbox[2:]).all(): bboxes.append(bbox) classes.append(jsonclassid_to_index[obj["category_id"]]) areas.append(obj["area"]) crowdeds.append(obj["iscrowd"]) labels.append( tk.od.ObjectsAnnotation( path=coco_dir / dirname / filename, width=width, height=height, classes=classes, bboxes=bboxes, areas=areas, crowdeds=crowdeds, )) return tk.od.ObjectsAnnotation.create_dataset(labels, class_names=class_names)
def main(): img_seq = [] # Get img_ids from lvis, instead. img_ids = coco.getImgIds() num_images = len(img_ids) # import pdb; pdb.set_trace() prog_bar = mmcv.ProgressBar(len(img_ids)) for i, img_id in enumerate(img_ids): print(i) # ipdb.set_trace() if DEBUG_ is True: if i==DEBUG_FRAMES: break img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) #gt_ids = coco.getAnnIds(imgIds=[img_id]) #gts = coco.loadAnns(gt_ids) prop_img = img.copy() img_props = props[i] # img_props = img_props[img_props[:,-1]>VIS_THR, :4] for j, prop in enumerate(img_props): bbox = _coco_box_to_bbox(prop) # bbox = np.asarray(prop, dtype=np.int32) cat_id = 1 prop_img = add_box(prop_img, bbox, VIS_THR, cat_id) # ipdb.set_trace() img_name = 'prop_{}.jpg'.format(str(img_id).zfill(12)) prop_img = add_to_canvas(prop_img) cv2.imwrite(os.path.join(OUT_PATH, img_name), prop_img) img_seq.append(prop_img) prog_bar.update() make_video(img_seq, VID_NAME)
def main(): img_seq = [] # Get img_ids from lvis, instead. img_ids = coco.getImgIds() num_images = len(img_ids) # import pdb; pdb.set_trace() prog_bar = mmcv.ProgressBar(len(img_ids)) ipdb.set_trace() if DEBUG_FOR_VIS: img_ids = random.sample(img_ids, 600) for i, img_id in enumerate(img_ids): # ipdb.set_trace() if DEBUG_ is True: if i == DEBUG_FRAMES: break img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img.copy() for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id'] gt_img = add_box(gt_img, bbox, 1.00, cat_id) # ipdb.set_trace() img_name = 'gt_{}.jpg'.format(str(img_id).zfill(12)) gt_img = add_to_canvas(gt_img) cv2.imwrite(os.path.join(OUT_PATH, img_name), gt_img) img_seq.append(gt_img) prog_bar.update() ipdb.set_trace() make_video(img_seq, VID_NAME)
def get_category_info(category_names, coco): """ Get the coco category info and image ids for given category names. :param category_names: List of category names. :param coco: Pycocotools COCO instance. :return: Dict with: category_info['person'] = (id, [image_ids, ...]) """ category_ids = zip(category_names, coco.getCatIds(catNms=category_names)) info = {} for category_name, category_id in category_ids: image_ids = coco.getImgIds(catIds=category_id) info[category_name] = (category_id, image_ids) return info
thickness=1) else: cv2.rectangle(image, (bbox[0], bbox[1] - cat_size[1] - 2), (bbox[0] + cat_size[0], bbox[1] - 2), color, -1) cv2.putText(image, txt, (bbox[0], bbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), thickness=1) cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) return image if __name__ == '__main__': dets = [] img_ids = coco.getImgIds() num_images = len(img_ids) for k in range(1, len(sys.argv)): pred_path = sys.argv[k] dets.append(coco.loadRes(pred_path)) # import pdb; pdb.set_trace() for i, img_id in enumerate(img_ids): img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img.copy() for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id']
if not os.path.isdir(imgDir): raise Exception('Cant find directory with MS-COCO images [%s]' % dataDir) # coco = COCO(annFile) # listCatsFoodIdx = coco.getCatIds(supNms=['food']) assert (set(listCatsFoodIdx) == set(listSortedFoodIds)) for ii, idx in enumerate(listCatsFoodIdx): tmpCat = coco.loadCats(ids=idx)[0] print('%d [%d] : %s (%s)' % (ii, idx, tmpCat['name'], tmpCat['supercategory'])) # tmpDictFoodImgIds = {} for ii, idx in enumerate(listSortedFoodIds): tmpImgIds = coco.getImgIds(catIds=idx) for timgId in tmpImgIds: if tmpDictFoodImgIds.has_key(timgId): tmpDictFoodImgIds[timgId].append(idx) else: tmpDictFoodImgIds[timgId] = [idx] setAllFoodImgIds = sorted(tmpDictFoodImgIds.keys()) print('#list/#set = %d' % len(tmpDictFoodImgIds.keys())) numImages = len(setAllFoodImgIds) for ii, kk in enumerate(setAllFoodImgIds): print('[%d/%d]' % (ii, numImages)) timgInfo = coco.loadImgs(kk)[0] foutImg = '%s/%s' % (dirOut, timgInfo['file_name']) foutMsk = '%s/%s-mskfood.png' % (dirOut, timgInfo['file_name']) foutPrv = '%s/%s-preview.jpg' % (dirOut, timgInfo['file_name']) if os.path.isfile(foutPrv):
def eval(model_arch, model_path, img_dir, gt_annot_path): output = "output" if os.path.exists(output): shutil.rmtree(output) os.mkdir(output) os.environ['CUDA_VISIBLE_DEVICES'] = "0" if LoadImagesAndLabels.num_classes <= 5: colors = [(55, 55, 250), (255, 155, 50), (128, 0, 0), (255, 0, 255), (128, 255, 128), (255, 0, 0)] else: colors = [(v // 32 * 64 + 64, (v // 8) % 4 * 64, v % 8 * 32) for v in range(1, LoadImagesAndLabels.num_classes + 1)][::-1] detector = CtdetDetector(model_arch, model_path) print('\n/****************** Eval ****************/\n') import tqdm import pycocotools.coco as coco from pycocotools.cocoeval import COCOeval print("gt path: {}".format(gt_annot_path)) result_file = '../evaluation/instances_det.json' coco = coco.COCO(gt_annot_path) images = coco.getImgIds() num_samples = len(images) print('find {} samples in {}'.format(num_samples, gt_annot_path)) #------------------------------------------------ coco_res = [] for index in tqdm.tqdm(range(num_samples)): img_id = images[index] file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] image_path = os.path.join(img_dir, file_name) img = cv2.imread(image_path) results, hm = detector.work(img) # 返回检测结果和置信度图 class_num = {} for res in results: cls, conf, bbox = res[0], res[1], res[2] coco_res.append({ 'bbox': [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]], 'category_id': LoadImagesAndLabels.class_name.index(cls), 'image_id': img_id, 'score': conf }) if cls in class_num: class_num[cls] += 1 else: class_num[cls] = 1 color = colors[LoadImagesAndLabels.class_name.index(cls)] # 绘制标签&置信度 label_ = '{}:{:.1f}'.format(cls, conf) plot_one_box(bbox, img, color=color, label=label_, line_thickness=2) cv2.imwrite(output + "/" + os.path.basename(image_path), img) cv2.namedWindow("heatmap", 0) cv2.imshow("heatmap", np.hstack(hm[0].cpu().numpy())) cv2.namedWindow("img", 0) cv2.imshow("img", img) key = cv2.waitKey(1) #------------------------------------------------- with open(result_file, 'w') as f_dump: json.dump(coco_res, f_dump, cls=NpEncoder) cocoDt = coco.loadRes(result_file) cocoEval = COCOeval(coco, cocoDt, 'bbox') # cocoEval.params.imgIds = imgIds cocoEval.params.catIds = [1] # 1代表’Hand’类,你可以根据需要增减类别 cocoEval.evaluate() print('\n/***************************/\n') cocoEval.accumulate() print('\n/***************************/\n') cocoEval.summarize() draw_pr(cocoEval)
MEAN = np.array([0.408, 0.447, 0.470]).astype(np.float32) STD = np.array([0.289, 0.274, 0.278]).astype(np.float32) max_objs = 32 _valid_ids = [1] class_name = ['__background__', 'person'] _data_rng = np.random.RandomState(123) _eig_val = np.array([0.2141788, 0.01817699, 0.00341571], dtype=np.float32) _eig_vec = np.array([[-0.58752847, -0.69563484, 0.41340352], [-0.5832747, 0.00994535, -0.81221408], [-0.56089297, 0.71832671, 0.41158938]], dtype=np.float32) coco = coco.COCO(anno_path) images = coco.getImgIds() catIds = coco.getCatIds(class_name[-1]) assert catIds == _valid_ids images = coco.getImgIds(images, catIds) num_samples = len(images) index = np.random.randint(num_samples) img_id = images[index] file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] img_path = os.path.join(img_dir, file_name) ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) anns = list( filter(lambda x: x['category_id'] in _valid_ids and x['iscrowd'] != 1,
from pycocotools import coco import matplotlib.pyplot as plt import skimage.io as io import time time1 = time.time() annFile = '/media/zhuzhu/ec114170-f406-444f-bee7-a3dc0a86cfa2/dataset/coco/annotations/person_keypoints_val2017.json' dataDir = '/media/zhuzhu/ec114170-f406-444f-bee7-a3dc0a86cfa2/dataset/coco/images/val2017' saveDir = '/media/zhuzhu/ec114170-f406-444f-bee7-a3dc0a86cfa2/coco_ground/val2017' coco = coco.COCO(annFile) catIds = coco.getCatIds(catNms=['person']) imgIds = coco.getImgIds(catIds=catIds) for idx in imgIds: img = coco.loadImgs(ids=idx)[0] annIds = coco.getAnnIds(imgIds=idx, catIds=catIds) anns = coco.loadAnns(ids=annIds) plt.figure(idx) I = io.imread('%s/%s' % (dataDir, img['file_name'])) plt.imshow(I) plt.axis('off') coco.showAnns(anns=anns) plt.savefig('%s/%s' % (saveDir, img['file_name'])) plt.close() time2 = time.time() print('spent t = %.2f min' % ((time2 - time1) / 60))
def load_coco(self, dataset_dir, subset, year=DEFAULT_DATASET_YEAR, class_ids=None, class_map=None, return_coco=False, auto_download=False): """Load a subset of the COCO dataset. dataset_dir: The root directory of the COCO dataset. subset: What to load (train, val, minival, valminusminival) year: What dataset year to load (2014, 2017) as a string, not an integer class_ids: If provided, only loads images that have the given classes. class_map: TODO: Not implemented yet. Supports maping classes from different datasets to the same class ID. return_coco: If True, returns the COCO object. auto_download: Automatically download and unzip MS-COCO images and annotations """ if auto_download is True: self.auto_download(dataset_dir, subset, year) coco = COCO("{}/annotations/instances_{}{}.json".format( dataset_dir, subset, year)) if subset == "minival" or subset == "valminusminival": subset = "val" image_dir = "{}/{}{}".format(dataset_dir, subset, year) # Load all classes or a subset? if not class_ids: # All classes class_ids = sorted(coco.getCatIds()) # All images or a subset? if class_ids: image_ids = [] for id in class_ids: image_ids.extend(list(coco.getImgIds(catIds=[id]))) # Remove duplicates image_ids = list(set(image_ids)) else: # All images image_ids = list(coco.imgs.keys()) # Add classes for i in class_ids: self.add_class("coco", i, coco.loadCats(i)[0]["name"]) # Add images for i in image_ids: self.add_image("coco", image_id=i, path=os.path.join(image_dir, coco.imgs[i]['file_name']), width=coco.imgs[i]["width"], height=coco.imgs[i]["height"], annotations=coco.loadAnns( coco.getAnnIds(imgIds=[i], catIds=class_ids, iscrowd=None))) if return_coco: return coco
def cache(config, path, category_index): phase = os.path.splitext(os.path.basename(path))[0] data = [] for i, row in pd.read_csv(os.path.splitext(__file__)[0] + '.tsv', sep='\t').iterrows(): logging.info('loading data %d (%s)' % (i, ', '.join([k + '=' + str(v) for k, v in row.items()]))) root = os.path.expanduser(os.path.expandvars(row['root'])) year = str(row['year']) suffix = phase + year path = os.path.join(root, 'annotations', 'instances_%s.json' % suffix) if not os.path.exists(path): logging.warning(path + ' not exists') continue coco = pycocotools.coco.COCO(path) catIds = coco.getCatIds(catNms=list(category_index.keys())) cats = coco.loadCats(catIds) id_index = dict( (cat['id'], category_index[cat['name']]) for cat in cats) imgIds = coco.getImgIds() path = os.path.join(root, suffix) imgs = coco.loadImgs(imgIds) _imgs = list( filter( lambda img: os.path.exists(os.path.join( path, img['file_name'])), imgs)) if len(imgs) > len(_imgs): logging.warning('%d of %d images not exists' % (len(imgs) - len(_imgs), len(imgs))) for img in tqdm.tqdm(_imgs): annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None) anns = coco.loadAnns(annIds) if len(anns) <= 0: continue path = os.path.join(path, img['file_name']) width, height = img['width'], img['height'] bbox = np.array([ann['bbox'] for ann in anns], dtype=np.float32) yx_min = bbox[:, 1::-1] hw = bbox[:, -1:1:-1] yx_max = yx_min + hw cls = np.array([id_index[ann['category_id']] for ann in anns], dtype=np.int) difficult = np.zeros(cls.shape, dtype=np.uint8) try: if config.getboolean('cache', 'verify'): size = (height, width) image = cv2.imread(path) assert image is not None assert image.shape[:2] == size[:2] utils.cache.verify_coords(yx_min, yx_max, size[:2]) except configparser.NoOptionError: pass assert len(yx_min) == len(cls) assert yx_min.shape == yx_max.shape assert len(yx_min.shape) == 2 and yx_min.shape[-1] == 2 data.append( dict(path=path, yx_min=yx_min, yx_max=yx_max, cls=cls, difficult=difficult)) logging.warning('%d of %d images are saved' % (len(data), len(_imgs))) return data