<jupyter_start><jupyter_text>Create Masks for Binary Classification<jupyter_code>image_list = sorted(os.listdir('images'), key=lambda x: int(x.split('.')[0]))
annot_list = sorted(os.listdir('annotated'), key=lambda x: int(x.split('.')[0]))

for im_fn, ann_fn in zip(image_list, annot_list):
    
    im = cv2.imread(os.path.join('images', im_fn), 0)
    
    ann_path = os.path.join('annotated', ann_fn)
    shape_dicts = get_poly(ann_path)
    im_binary = create_binary_masks(im, shape_dicts)
    
    plot_pair([im, im_binary], gray=True)
    plt.show()
    break<jupyter_output><empty_output><jupyter_text>Create Masks for Multi Classification<jupyter_code>hues = {'star': 30,
        'square': 0,
        'circle': 90,
        'triangle': 60}

labels = sorted(hues.keys())
print(labels)<jupyter_output>['circle', 'square', 'star', 'triangle']<jupyter_text>To set the hues you want, refer to this pi chart. These are the values used by OpenCV. ![hsv](docs/images/hsv.png)<jupyter_code>def create_multi_masks(im, shape_dicts):
    
    channels = []
    cls = [x['label'] for x in shape_dicts]
    poly = [np.array(x['points'], dtype=np.int32) for x in shape_dicts]
    label2poly = dict(zip(cls, poly))
    background = np.zeros(shape=im.shape, dtype=np.float32)

    for i, label in enumerate(labels):
        
        blank = np.zeros(shape=im.shape, dtype=np.float32)
        
        if label in cls:
            cv2.fillPoly(blank, [label2poly[label]], 255)
            cv2.fillPoly(background, [label2poly[label]], 255)
            
        channels.append(blank)
    _, thresh = cv2.threshold(background, 127, 255, cv2.THRESH_BINARY_INV)
    channels.append(thresh)

    Y = np.stack(channels, axis=2)
    return Y
for im_fn, ann_fn in zip(image_list, annot_list):
    
    im = cv2.imread(os.path.join('images', im_fn), 0)
    ann_path = os.path.join('annotated', ann_fn)
    shape_dicts = get_poly(ann_path)
    Y = create_multi_masks(im, shape_dicts)
    break
    
for i in range(5):
    mask = Y[:,:,i]
    plt.imshow(mask, cmap='gray')
    plt.show()<jupyter_output><empty_output><jupyter_text>Hue Masks<jupyter_code>def draw_multi_masks(im, shape_dicts):
    
    blank = np.zeros(shape=im.shape, dtype=np.uint8)
    
    channels = []
    cls = [x['label'] for x in shape_dicts]
    poly = [np.array(x['points'], dtype=np.int32) for x in shape_dicts]
    label2poly = dict(zip(cls, poly))

    for i, label in enumerate(labels):
        
        if label in cls:
            cv2.fillPoly(blank, [label2poly[label]], (hues[label], 255, 255))
            
    return cv2.cvtColor(blank, cv2.COLOR_HSV2RGB)
for i, (im_fn, ann_fn) in enumerate(zip(image_list, annot_list)):
    
    im = cv2.imread(os.path.join('images', im_fn), 1)
    ann_path = os.path.join('annotated', ann_fn)
    shape_dicts = get_poly(ann_path)
    im_color = draw_multi_masks(im, shape_dicts)
    display(Image.fromarray(im_color))
    if i == 4:
        break
def augment_poly(im_path, ann_path):
    
    im = cv2.imread(im_path, 1)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    
    with open(ann_path) as handle:
        data = json.load(handle)
    
    shape_dicts = data['shapes']
    points = []
    aug_shape_dicts = []
    i = 0
    
    for shape in shape_dicts:
        
        for pairs in shape['points']:
            points.append(ia.Keypoint(x=pairs[0], y=pairs[1]))
        
        _d = {}
        _d['label'] = shape['label']
        _d['index'] = (i, i+len(shape['points']))
        aug_shape_dicts.append(_d)

        i += len(shape['points'])
    
    keypoints = ia.KeypointsOnImage(points, shape=(256,256,3))
    
    image_aug = seq_det.augment_images([im])[0]
    keypoints_aug = seq_det.augment_keypoints([keypoints])[0]
    
    for shape in aug_shape_dicts:
        start, end = shape['index']
        aug_points = [[keypoint.x, keypoint.y] for keypoint in keypoints_aug.keypoints[start:end]]
        shape['points'] = aug_points
    
    return image_aug, aug_shape_dicts
ia.seed(1)

seq = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Multiply((1.2, 1.5)),
    iaa.Affine(
        rotate=(-90, 90)
    ),
    iaa.Sometimes(0.5,
        iaa.GaussianBlur(sigma=(0, 10))
    )
], random_order=True)
seq_det = seq.to_deterministic()
im_path = os.path.join('images', '0.jpg')
ann_path = os.path.join('annotated', '0.json')

im_aug, aug_shape_dicts = augment_poly(im_path, ann_path)
aug_mask = create_binary_masks(im_aug, aug_shape_dicts)
plot_pair((im_aug, aug_mask), gray=True)
image_list = sorted(os.listdir('images'), key=lambda x: int(x.split('.')[0]))
annot_list = sorted(os.listdir('annotated'), key=lambda x: int(x.split('.')[0]))

for i, (im_fn, ann_fn) in enumerate(zip(image_list, annot_list)):
    
    seq_det = seq.to_deterministic()
    im_path = os.path.join('images', im_fn)
    ann_path = os.path.join('annotated', ann_fn)
    im_aug, aug_shape_dicts = augment_poly(im_path, ann_path)
    aug_mask = create_binary_masks(im_aug, aug_shape_dicts)
    
    im = cv2.imread(im_path, 1)
    im_color = draw_multi_masks(im, aug_shape_dicts)
    
    plt.imshow(im_aug)
    plt.show()
    plot_pair((aug_mask, im_color), gray=True)
    
    if i == 4:
        break<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14567381316998468, 'fraction_numerical': 0.01875957120980092, 'mean_word_length': 3.3289146644573324, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '393965', 'license': 'MIT', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1982, 'n_words': 361, 'path': 'Image Masks and Augmentation.ipynb', 'repo_name': 'hongluck/shape-custom', 'size': '5224'}