-
Notifications
You must be signed in to change notification settings - Fork 284
/
dataset_util.py
77 lines (60 loc) · 2.3 KB
/
dataset_util.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import ujson
import json
import pathlib
__all__ = ['load', 'save', 'show_bbox_on_image']
def load(file_path: str):
file_path = pathlib.Path(file_path)
func_dict = {'.txt': load_txt, '.json': load_json, '.list': load_txt}
assert file_path.suffix in func_dict
return func_dict[file_path.suffix](file_path)
def load_txt(file_path: str):
with open(file_path, 'r', encoding='utf8') as f:
content = [x.strip().strip('\ufeff').strip('\xef\xbb\xbf') for x in f.readlines()]
return content
def load_json(file_path: str):
with open(file_path, 'rb') as f:
content = f.read()
return ujson.loads(content)
def save(data, file_path):
file_path = pathlib.Path(file_path)
func_dict = {'.txt': save_txt, '.json': save_json}
assert file_path.suffix in func_dict
return func_dict[file_path.suffix](data, file_path)
def save_txt(data, file_path):
if not isinstance(data, list):
data = [data]
with open(file_path, mode='w', encoding='utf8') as f:
f.write('\n'.join(data))
def save_json(data, file_path):
with open(file_path, 'w', encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
def show_bbox_on_image(image, polygons=None, txt=None, color=None, font_path='./font/Arial_Unicode.ttf'):
from PIL import ImageDraw, ImageFont
image = image.convert('RGB')
draw = ImageDraw.Draw(image)
if len(txt) == 0:
txt = None
if color is None:
color = (255, 0, 0)
if txt is not None:
font = ImageFont.truetype(font_path, 20)
for i, box in enumerate(polygons):
box = box[0]
if txt is not None:
draw.text((int(box[0][0]) + 20, int(box[0][1]) - 20), str(txt[i]), fill='red', font=font)
for j in range(len(box) - 1):
draw.line((box[j][0], box[j][1], box[j + 1][0], box[j + 1][1]), fill=color, width=2)
draw.line((box[-1][0], box[-1][1], box[0][0], box[0][1]), fill=color, width=2)
return image
def show_glyphs(glyphs, name):
import numpy as np
import cv2
size = 64
gap = 5
n_char = 20
canvas = np.ones((size, size*n_char + gap*(n_char-1), 1))*0.5
x = 0
for i in range(glyphs.shape[-1]):
canvas[:, x:x + size, :] = glyphs[..., i:i+1]
x += size+gap
cv2.imwrite(name, canvas*255)