-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelme2seg.py
47 lines (43 loc) · 1.42 KB
/
labelme2seg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project :unet
@File :labelme2seg.py
@Author :ChenmingSong
@Date :2022/2/13 16:24
@Description:
'''
from __future__ import print_function
import argparse
import glob
import math
import json
import os
import os.path as osp
import shutil
import numpy as np
import PIL.Image
import PIL.ImageDraw
import cv2
print('hi')
def json2png(json_folder, png_save_folder):
if osp.isdir(png_save_folder):
shutil.rmtree(png_save_folder)
os.makedirs(png_save_folder)
json_files = os.listdir(json_folder)
print(json_files)
for json_file in json_files:
json_path = osp.join(json_folder, json_file)
os.system("labelme_json_to_dataset {}".format(json_path))
label_path = osp.join(json_folder, json_file.split(".")[0] + "_json/label.png")
png_save_path = osp.join(png_save_folder, json_file.split(".")[0] + ".png")
label_png = cv2.imread(label_path, 0)
label_png[label_png > 0] = 255
cv2.imwrite(png_save_path, label_png)
# shutil.copy(label_path, png_save_path)
# break
if __name__ == '__main__':
# !!!!你的json文件夹下只能有json文件不能有其他文件
# json2png(json_folder="testdata/jsons/", png_save_folder="testdata/labels/")
print('================================')
json2png(json_folder="./images/cracks/Train_Labels/", png_save_folder="./images/cracks/Labels/")