<jupyter_start><jupyter_text>Postprocessing code- Construct hierarchy: associate centroids of detected things first to parent plates, then to parent tray<jupyter_code>tray_id = class_map['Tray']
plate_id = class_map['Plate']<jupyter_output><empty_output><jupyter_text>Export predictions of validation set<jupyter_code>results_list = []
for ind in dataset_val.image_ids:
    image, image_meta, gt_class_id, gt_bbox, gt_mask =\
    modellib.load_image_gt(dataset_val, config, ind, use_mini_mask=False)
    
    info = dataset_val.image_info[ind]
    print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], ind, 
                                           dataset_val.image_reference(ind)))
    
    # Run object detection
    results = model.detect([image], verbose=1);
    results_list.append(results[0]);<jupyter_output>image ID: healthhack.426 (0) ./prepared_data/val/imgs/hh_4comp_427_ud.jpg
Processing 1 images
image                    shape: (1024, 1024, 3)       min:    0.00000  max:  255.00000  uint8
molded_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  151.10000  float64
image_metas              shape: (1, 27)               min:    0.00000  max: 1024.00000  int64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32
image ID: healthhack.427 (1) ./prepared_data/val/imgs/hh_4comp_428_ud.jpg
Processing 1 images
image                    shape: (1024, 1024, 3)       min:    0.00000  max:  255.00000  uint8
molded_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  151.10000  float64
image_metas              shape: (1, 27)               min:    0.00000  max: 1024.00000  int64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32
image ID: healthhack.428 (2) ./prepared_data/val/img[...]<jupyter_text>Creating COCO JSON<jupyter_code>det_ind = 0
det_list = []
for image_id, result in enumerate(results_list):
    for inst_id, inst in enumerate(result['class_ids']):
        mask = result['masks'][:,:,inst_id]
        
        # Detect contours from binary mask, then take first contour
        image, contours, hierarchy = cv2.findContours(mask.astype(np.uint8) ,cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        contour = contours[0].flatten('C')
        
        d = {'segmentation': [contour.tolist()],
             'iscrowd': 0,
             'image_id': int(image_id),
             'category_id': int(result['class_ids'][inst_id]), 
             'id': int(det_ind), 
             'bbox': result['rois'][inst_id].tolist(),
             'area': int(np.sum((mask==True).astype(np.int)))}
        
        det_ind +=1
        det_list.append(d)
    
with open('./coco_json/annotations.json', 'w') as f:
    json.dump(det_list, f)
cat_list = [{"id": val, "name": key, "supercategory": "None"} for key,val in class_map.items()]
with open('./coco_json/categories.json', 'w') as f:
    json.dump(cat_list, f)
# Let's also move the images
#12
for ind in dataset_val.image_ids:    
    info = dataset_val.image_info[ind]
    shutil.copy(dataset_val.image_reference(ind), os.path.join('./coco_json/imgs/%012d.jpg'%ind))
    
pwd
image_id = random.choice(dataset_val.image_ids)
image, image_meta, gt_class_id, gt_bbox, gt_mask =\
    modellib.load_image_gt(dataset_val, config, image_id, use_mini_mask=False)
info = dataset_val.image_info[image_id]
print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], image_id, 
                                       dataset_val.image_reference(image_id)))

# Run object detection
results = model.detect([image], verbose=1)

# Display results
ax = get_ax(1)
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], 
                            dataset_val.class_names, r['scores'], ax=ax,
                            title="")
log("gt_class_id", gt_class_id)
log("gt_bbox", gt_bbox)
log("gt_mask", gt_mask)<jupyter_output><empty_output><jupyter_text>Make video<jupyter_code>pwd
video_img_path = './excerpt/eximg_ud/'
video_img_path_out = './excerpt/eximg_ud_inf/'

files  = os.listdir(video_img_path)

for i, file in tqdm(enumerate(files)):
    image = skimage.io.imread(os.path.join(video_img_path, file))
    
    results = model.detect([image], verbose=1)
    r = results[0]
    fig, ax = get_ax(1)
    display_instances(image, r['rois'], r['masks'], r['class_ids'], 
                            dataset_val.class_names, r['scores'], ax=ax,
                            title="", colors=colors)
    extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
    fig.savefig(os.path.join(video_img_path_out, file), bbox_inches=extent)
image_id = random.choice(dataset_val.image_ids)
image, image_meta, gt_class_id, gt_bbox, gt_mask =\
    modellib.load_image_gt(dataset_val, config, image_id, use_mini_mask=False)
info = dataset_val.image_info[image_id]
print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], image_id, 
                                       dataset_val.image_reference(image_id)))

# Run object detection
results = model.detect([image], verbose=1)

# Display results

r = results[0]

log("gt_class_id", gt_class_id)
log("gt_bbox", gt_bbox)
log("gt_mask", gt_mask)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup