Skip to content

Commit e7217db

Browse files
Add -obb parameter to output bounding boxes
1 parent ee0bae4 commit e7217db

13 files changed

Lines changed: 199 additions & 10 deletions

tests.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,92 @@ def test_generate_data_with_hindi_text(self):
10731073

10741074
os.remove("tests/out/22_परीक्षा परीक्षा परीक्षा.png")
10751075

1076+
def test_generate_data_with_output_bounding_box(self):
1077+
FakeTextDataGenerator.generate(
1078+
21,
1079+
"TEST TEST TEST",
1080+
"tests/font.ttf",
1081+
"tests/out/",
1082+
64,
1083+
"jpg",
1084+
0,
1085+
False,
1086+
0,
1087+
False,
1088+
1,
1089+
0,
1090+
0,
1091+
False,
1092+
0,
1093+
-1,
1094+
0,
1095+
"#010101",
1096+
0,
1097+
1,
1098+
0,
1099+
(5, 5, 5, 5),
1100+
0,
1101+
0,
1102+
False,
1103+
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
1104+
output_bboxes=1,
1105+
)
1106+
1107+
self.assertLess(
1108+
diff(
1109+
"tests/out/TEST TEST TEST_21.jpg",
1110+
"tests/expected_results/TEST TEST TEST_21.jpg",
1111+
delete_diff_file=True,
1112+
),
1113+
0.11
1114+
)
1115+
1116+
os.remove("tests/out/TEST TEST TEST_21.jpg")
1117+
os.remove("tests/out/TEST TEST TEST_21_boxes.txt")
1118+
1119+
def test_generate_data_with_tesseract_output_bounding_box(self):
1120+
FakeTextDataGenerator.generate(
1121+
22,
1122+
"TEST TEST TEST",
1123+
"tests/font.ttf",
1124+
"tests/out/",
1125+
64,
1126+
"jpg",
1127+
0,
1128+
False,
1129+
0,
1130+
False,
1131+
1,
1132+
0,
1133+
0,
1134+
False,
1135+
0,
1136+
-1,
1137+
0,
1138+
"#010101",
1139+
0,
1140+
1,
1141+
0,
1142+
(5, 5, 5, 5),
1143+
0,
1144+
0,
1145+
False,
1146+
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
1147+
output_bboxes=2,
1148+
)
1149+
1150+
self.assertLess(
1151+
diff(
1152+
"tests/out/TEST TEST TEST_22.jpg",
1153+
"tests/expected_results/TEST TEST TEST_22.jpg",
1154+
delete_diff_file=True,
1155+
),
1156+
0.11
1157+
)
1158+
1159+
os.remove("tests/out/TEST TEST TEST_22.jpg")
1160+
os.remove("tests/out/TEST TEST TEST_22.box")
1161+
10761162
def test_generate_string_with_letters(self):
10771163
s = create_strings_randomly(1, False, 1, True, False, False, "en")[0]
10781164

5.1 KB
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
5 22 31 58
2+
36 22 57 58
3+
62 22 85 59
4+
88 22 114 58
5+
115 22 128 59
6+
129 22 155 58
7+
160 22 181 58
8+
186 22 209 59
9+
212 22 238 58
10+
239 22 252 59
11+
253 22 279 58
12+
284 22 305 58
13+
310 22 333 59
14+
336 22 362 58
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
T 5 6 31 42 0
2+
E 36 6 57 42 0
3+
S 62 5 85 42 0
4+
T 88 6 114 42 0
5+
115 5 128 43 0
6+
T 129 6 155 42 0
7+
E 160 6 181 42 0
8+
S 186 5 209 42 0
9+
T 212 6 238 42 0
10+
239 5 252 43 0
11+
T 253 6 279 42 0
12+
E 284 6 305 42 0
13+
S 310 5 333 42 0
14+
T 336 6 362 42 0
5.1 KB
Loading

trdg/computer_text_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def _generate_vertical_text(
132132

133133
txt_img_draw = ImageDraw.Draw(txt_img)
134134
txt_mask_draw = ImageDraw.Draw(txt_mask)
135+
txt_mask_draw.fontmode = "1"
135136

136137
colors = [ImageColor.getrgb(c) for c in text_color.split(",")]
137138
c1, c2 = colors[0], colors[-1]
@@ -163,7 +164,7 @@ def _generate_vertical_text(
163164
txt_mask_draw.text(
164165
(0, sum(char_heights[0:i]) + i * character_spacing),
165166
c,
166-
fill=(i // (255 * 255), i // 255, i % 255),
167+
fill=((i + 1) // (255 * 255), (i + 1) // 255, (i + 1) % 255),
167168
font=image_font,
168169
stroke_width=stroke_width,
169170
stroke_fill=stroke_fill,

trdg/data_generator.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from PIL import Image, ImageFilter, ImageStat
55

66
from trdg import computer_text_generator, background_generator, distorsion_generator
7+
from trdg.utils import mask_to_bboxes
78

89
try:
910
from trdg import handwritten_text_generator
@@ -52,6 +53,7 @@ def generate(
5253
stroke_width=0,
5354
stroke_fill="#282828",
5455
image_mode="RGB",
56+
output_bboxes=0,
5557
):
5658
image = None
5759

@@ -248,24 +250,36 @@ def generate(
248250
if space_width == 0:
249251
text = text.replace(" ", "")
250252
if name_format == 0:
251-
image_name = "{}_{}.{}".format(text, str(index), extension)
252-
mask_name = "{}_{}_mask.png".format(text, str(index))
253+
name = "{}_{}".format(text, str(index))
253254
elif name_format == 1:
254-
image_name = "{}_{}.{}".format(str(index), text, extension)
255-
mask_name = "{}_{}_mask.png".format(str(index), text)
255+
name = "{}_{}".format(str(index), text)
256256
elif name_format == 2:
257-
image_name = "{}.{}".format(str(index), extension)
258-
mask_name = "{}_mask.png".format(str(index))
257+
name = str(index)
259258
else:
260259
print("{} is not a valid name format. Using default.".format(name_format))
261-
image_name = "{}_{}.{}".format(text, str(index), extension)
262-
mask_name = "{}_{}_mask.png".format(text, str(index))
260+
name = "{}_{}".format(text, str(index))
261+
262+
image_name = "{}.{}".format(name, extension)
263+
mask_name = "{}_mask.png".format(name)
264+
box_name = "{}_boxes.txt".format(name)
265+
tess_box_name = "{}.box".format(name)
266+
263267

264268
# Save the image
265269
if out_dir is not None:
266270
final_image.save(os.path.join(out_dir, image_name))
267271
if output_mask == 1:
268272
final_mask.save(os.path.join(out_dir, mask_name))
273+
if output_bboxes == 1:
274+
bboxes = mask_to_bboxes(final_mask)
275+
with open(os.path.join(out_dir, box_name), "w") as f:
276+
for bbox in bboxes:
277+
f.write(" ".join([str(v) for v in bbox]) + "\n")
278+
if output_bboxes == 2:
279+
bboxes = mask_to_bboxes(final_mask, tess=True)
280+
with open(os.path.join(out_dir, tess_box_name), "w") as f:
281+
for bbox, char in zip(bboxes, text):
282+
f.write(" ".join([char] + [str(v) for v in bbox] + ['0']) + "\n")
269283
else:
270284
if output_mask == 1:
271285
return final_image, final_mask

trdg/generators/from_dict.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(
4141
stroke_width=0,
4242
stroke_fill="#282828",
4343
image_mode="RGB",
44+
output_bboxes=0,
4445
):
4546
self.count = count
4647
self.length = length
@@ -74,6 +75,7 @@ def __init__(
7475
stroke_width,
7576
stroke_fill,
7677
image_mode,
78+
output_bboxes,
7779
)
7880

7981
def __iter__(self):

trdg/generators/from_random.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(
4444
stroke_width=0,
4545
stroke_fill="#282828",
4646
image_mode="RGB",
47+
output_bboxes=0,
4748
):
4849
self.generated_count = 0
4950
self.count = count
@@ -89,6 +90,7 @@ def __init__(
8990
stroke_width,
9091
stroke_fill,
9192
image_mode,
93+
output_bboxes,
9294
)
9395

9496
def __iter__(self):

trdg/generators/from_strings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(
4242
stroke_width=0,
4343
stroke_fill="#282828",
4444
image_mode="RGB",
45+
output_bboxes=0,
4546
):
4647
self.count = count
4748
self.strings = strings
@@ -77,6 +78,7 @@ def __init__(
7778
self.output_mask = output_mask
7879
self.word_split = word_split
7980
self.image_dir = image_dir
81+
self.output_bboxes = output_bboxes
8082
self.generated_count = 0
8183
self.stroke_width = stroke_width
8284
self.stroke_fill = stroke_fill
@@ -123,6 +125,7 @@ def next(self):
123125
self.stroke_width,
124126
self.stroke_fill,
125127
self.image_mode,
128+
self.output_bboxes,
126129
),
127130
self.orig_strings[(self.generated_count - 1) % len(self.orig_strings)] if self.rtl else self.strings[(self.generated_count - 1) % len(self.strings)],
128131
)

0 commit comments

Comments
 (0)