728x90
반응형
import matplotlib.pyplot as plt
from glob import glob
import os, json
import numpy as np
import os.path as osp
from mmcv import Config
from glob import glob
from PIL import Image
import mmcv
from tqdm import tqdm
import torchvision.transforms as transforms
def aihub2labelme(save_dir, jsonfile) :
imgname = jsonfile.replace('.json', '.jpg')
img = plt.imread(imgname)
imgname = os.path.basename(imgname) if os.path.isfile(imgname) else os.path.basename(imgname.replace('.jpg', '.png'))
h,w = img.shape[:2]
with open(jsonfile) as f :
try :
data = json.load(f)
except :
print(jsonfile)
return []
#image_info = data['images']
annotation_info = data['annotations']
box_info = []
for annot in annotation_info :
if not annot['annotation.ttype']=='textType1' :
print('different ttype\n', jsonfile)
break
if not annot['annotation.type']=='rectangle' :
print('different type\n', jsonfile)
break
label = annot['annotation.text']
x,y,w,h = annot['annotation.bbox']
bbox= [[x, y],
[x+w, y],
[x+w, y+h],
[x, y+h]]
box_info.append({'label' : str(label),
'points' : bbox,
'grou_id' : None,
'shape_type':'polygon',
'flags' : {}})
fname = os.path.join(save_dir, os.path.basename(jsonfile))
with open(fname, 'w', encoding='utf-8') as outfile :
data = {'version':'4.5.7', 'flags' : {}, 'shapes' : box_info, 'imagePath': imgname, 'imageData' : None, 'imageHeight' : h, 'imageWidth' : w}
json.dump(data, outfile, ensure_ascii=False, indent = 4)
if __name__ == '__main__':
json_data = sorted(glob("./aihub_public_document_ocr_data/Validation/**/*.json", recursive = True))
for idx in tqdm(range(len(json_data))):
json_file = json_data[idx]
if json_file.split('/')[8] in ['농림.축산지원', '도시개발', '산업진흥', ' 상.하수도관리', '인.허가', '일반행정']:
new_path = './aihub2labelme/'+json_file.split('/')[8:-1]
aihub2labelme(new_path, json_file)
else:
continue
728x90
반응형
'사소한 Tip . 오류 해결법' 카테고리의 다른 글
Tensorflow Warning Entity bound method ... Please report this to the AutoGraph team. 해결 방법 (0) | 2021.12.02 |
---|---|
[python] 일부 겹치는 문장 찾고 합치기 / difflib, SequenceMatcher (0) | 2021.12.01 |
[Tensorflow] TFRecord 형식 데이터 처리 (0) | 2021.11.24 |
CMD출력 Warning 삭제(in.Tensorflow) (0) | 2021.11.24 |
RuntimeError: OrderedDict mutated during iteration (0) | 2021.11.17 |