def main(image_id): # whether from the PNG are used or new colors are generated generate_new_colors = True json_file = 'panoptic_val2017.json' segmentations_folder = './panoptic_val2017/' pred_folder = './panoptic_pred2017/' img_folder = '/Users/wuyangxin/Desktop/cv/dataset/coco/val2017' panoptic_coco_categories = './panoptic_coco_categories.json' with open(json_file, 'r') as f: coco_d = json.load(f) # ann = np.random.choice(coco_d['annotations']) ann = None for a in coco_d['annotations']: if a['image_id'] == image_id: ann = a break with open(panoptic_coco_categories, 'r') as f: categories_list = json.load(f) categegories = {category['id']: category for category in categories_list} # find input img that correspond to the annotation img = None pred_img = None for image_info in coco_d['images']: if image_info['id'] == ann['image_id']: try: img = np.array( Image.open( os.path.join(img_folder, image_info['file_name']))) pred_img = Image.open( os.path.join(pred_folder, image_info['file_name'].replace('jpg', 'png'))) except: print("Undable to find correspoding input image.") break segmentation = np.array(Image.open( os.path.join(segmentations_folder, ann['file_name'])), dtype=np.uint8) segmentation_id = rgb2id(segmentation) # find segments boundaries boundaries = find_boundaries(segmentation_id, mode='thick') if generate_new_colors: segmentation[:, :, :] = 0 color_generator = IdGenerator(categegories) for segment_info in ann['segments_info']: color = color_generator.get_color(segment_info['category_id']) mask = segmentation_id == segment_info['id'] segmentation[mask] = color # depict boundaries # segmentation[boundaries] = [0, 0, 0] if img is None: plt.figure() plt.imshow(segmentation) plt.axis('off') else: plt.figure(figsize=(9, 5)) plt.subplot(131) plt.imshow(img) plt.axis('off') plt.subplot(132) plt.imshow(segmentation) plt.axis('off') plt.subplot(133) plt.imshow(pred_img) plt.axis('off') plt.tight_layout() plt.show()
except: print("Undable to find correspoding input image.") break segmentation = np.array(Image.open( os.path.join(segmentations_folder, ann['file_name'])), dtype=np.uint8) segmentation_id = rgb2id(segmentation) # find segments boundaries boundaries = find_boundaries(segmentation_id, mode='thick') if generate_new_colors: segmentation[:, :, :] = 0 color_generator = IdGenerator(categegories) for segment_info in ann['segments_info']: color = color_generator.get_color(segment_info['category_id']) mask = segmentation_id == segment_info['id'] segmentation[mask] = color # depict boundaries segmentation[boundaries] = [0, 0, 0] if img is None: plt.figure() plt.imshow(segmentation) plt.axis('off') else: plt.figure(figsize=(20, 10)) plt.subplot(121) plt.imshow(img) plt.axis('off')