Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export labels as image #812

Closed
shubham-scisar opened this issue Dec 16, 2020 · 2 comments
Closed

Export labels as image #812

shubham-scisar opened this issue Dec 16, 2020 · 2 comments

Comments

@shubham-scisar
Copy link

Is there any functionality to export the final image as mask image having different classes. Like we are using the labels to be exported as json file. Instead of json file, it should be exported as a single image having different labels. Kindly guide.

@Liu-ZhenJi
Copy link

The script below are the workable codes to translate the json file to mask image. Wish this is helpful for you. : )

def json2seg_label(json_root_dir, image_root_dir, save_dir):

for file_name in os.listdir(json_root_dir):
    print('The current json_name: ', file_name)

    label_dict = parse_json(os.path.join(json_root_dir, file_name))

    image_tmp = cv2.imread(os.path.join(image_root_dir, file_name.replace('.json', '.png')))
    mask_1 = np.zeros(image_tmp[:, :, 0].shape, np.uint8)
    mask_3 = np.zeros(image_tmp[:, :, 0].shape, np.uint8)

    cv2.fillPoly(mask_1, label_dict['1'], 1)
    cv2.fillPoly(mask_3, label_dict['3'], 3)

    mask_4 = copy.deepcopy(mask_3)
    mask_4[mask_1==1] = 1

    cv2.imwrite(os.path.join(save_dir, file_name.replace('.json', '.png')), mask_4)

def parse_json(json_dir):

with open(json_dir, 'r') as f:
    label_json = json.load(f)

label_dict = {}
for i in range(len(label_json['shapes'])):

    temp_label = label_json['shapes'][i]['label']
    # if temp_label == '3':
    #     continue
    if temp_label not in label_dict.keys():
        label_dict[temp_label] = []

    temp_list = label_json['shapes'][i]['points']
    tmp_label_list = []
    for pair in temp_list:
        # print('The pair: ', pair)
        tmp_label_list.append([round(pair[0]), round(pair[1])])  # The type must be integer.
    tmp_label_array = np.array([tmp_label_list])
    label_dict[temp_label].append(tmp_label_array)

return label_dict

@wkentaro
Copy link
Owner

labelme2voc.py saves labels as PNG file.

@wkentaro wkentaro removed the question label Jun 25, 2022
Repository owner locked and limited conversation to collaborators Jun 25, 2022
@wkentaro wkentaro converted this issue into discussion #1100 Jun 25, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants