├── README.md └── ann2mask.py /README.md: -------------------------------------------------------------------------------- 1 | # annotation2mask 2 | Converting coco-like annotation json files to png masks 3 | 4 | This repo is read-only and the main code is moved to https://github.com/pooya-mohammadi/deep_utils/blob/master/deep_utils/utils/coco_utils/main.py 5 | -------------------------------------------------------------------------------- /ann2mask.py: -------------------------------------------------------------------------------- 1 | """ 2 | Converting json coco file segmentations to masks for Unet and other methods. 3 | """ 4 | # pip install deep_utils 5 | from deep_utils import remove_create 6 | import os 7 | import cv2 8 | import json 9 | from tqdm import tqdm 10 | import numpy as np 11 | from collections import defaultdict 12 | 13 | 14 | def get_image_id(json_dict, img_name): 15 | images = json_dict['images'] 16 | for d in images: 17 | if d['file_name'] == img_name: 18 | return d['id'] 19 | 20 | 21 | def get_annotation(json_dict, id_): 22 | annotations = json_dict['annotations'] 23 | annotations_list = list() 24 | for ann in annotations: 25 | if ann['image_id'] == id_: 26 | annotations_list.append(ann) 27 | return annotations_list 28 | 29 | 30 | def main(json_path, image_path, mask_path): 31 | remove_create(mask_path) 32 | with open(json_path, 'r', encoding='utf-8') as f: 33 | json_dict = json.load(f) 34 | # get the annotations 35 | annotations_point = json_dict['annotations'] 36 | # make a dictionary from annotations with image_ids as keys for each of use. 37 | # img_annotation_dict = {ann['image_id']: ann for ann in annotations_point} 38 | img_annotation_dict = defaultdict(list) 39 | for ann in annotations_point: 40 | img_annotation_dict[ann['image_id']].append(ann) 41 | img_annotation_dict = {1: [{segmentations: ....} , {segmentations: ....}, ... ]} 42 | # get the images' information 43 | image_file = json_dict['images'] # [{img_name: ... , width, id:... } , {}, ... ] 44 | # make a dictionary of images' information for ease of use. 45 | img_dict = {img_file['id']: img_file for img_file in image_file} 46 | for img_id, img_info in tqdm(img_dict.items(), total=len(img_dict)): 47 | mask = np.zeros((img_info['height'], img_info['width']), dtype=np.uint8) 48 | annotations = img_annotation_dict[img_id] 49 | for ann in annotations: 50 | segmentations = ann['segmentation'] 51 | cat_id = ann['category_id'] 52 | for seg in segmentations: 53 | # getting the points 54 | xs = seg[0::2] 55 | ys = seg[1::2] 56 | pts = np.array([[x, y] for x, y in zip(xs, ys)], dtype=np.int32) 57 | pts = pts.reshape((-1, 1, 2)) 58 | 59 | # draw the points on the mask image. 60 | try: 61 | mask = cv2.fillPoly(mask, [pts], cat_id) 62 | except Exception as e: 63 | print(f"[ERROR] error for {img_info['file_name']}, len={len(pts)}") 64 | cv2.imwrite(os.path.join(mask_path, img_info['file_name']), mask) 65 | print(f'[INFO] img_id: {img_id} is done!') 66 | 67 | 68 | if __name__ == '__main__': 69 | main('cloth_fc.json', "cloth_fc", "cloth_fc_masks") 70 | 71 | --------------------------------------------------------------------------------