Skip to content

Commit 06d987b

Browse files
authored
Remove useless classes and classmethod
1 parent a5ee09d commit 06d987b

6 files changed

Lines changed: 350 additions & 370 deletions

File tree

TextRecognitionDataGenerator/background_generator.py

Lines changed: 78 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,82 @@
66

77
from PIL import Image, ImageDraw, ImageFilter
88

9-
class BackgroundGenerator(object):
10-
@classmethod
11-
def gaussian_noise(cls, height, width):
12-
"""
13-
Create a background with Gaussian noise (to mimic paper)
14-
"""
15-
16-
# We create an all white image
17-
image = np.ones((height, width)) * 255
18-
19-
# We add gaussian noise
20-
cv2.randn(image, 235, 10)
21-
22-
return Image.fromarray(image).convert('RGBA')
23-
24-
@classmethod
25-
def plain_white(cls, height, width):
26-
"""
27-
Create a plain white background
28-
"""
29-
30-
return Image.new("L", (width, height), 255).convert('RGBA')
31-
32-
@classmethod
33-
def quasicrystal(cls, height, width):
34-
"""
35-
Create a background with quasicrystal (https://en.wikipedia.org/wiki/Quasicrystal)
36-
"""
37-
38-
image = Image.new("L", (width, height))
39-
pixels = image.load()
40-
41-
frequency = random.random() * 30 + 20 # frequency
42-
phase = random.random() * 2 * math.pi # phase
43-
rotation_count = random.randint(10, 20) # of rotations
44-
45-
for kw in range(width):
46-
y = float(kw) / (width - 1) * 4 * math.pi - 2 * math.pi
47-
for kh in range(height):
48-
x = float(kh) / (height - 1) * 4 * math.pi - 2 * math.pi
49-
z = 0.0
50-
for i in range(rotation_count):
51-
r = math.hypot(x, y)
52-
a = math.atan2(y, x) + i * math.pi * 2.0 / rotation_count
53-
z += math.cos(r * math.sin(a) * frequency + phase)
54-
c = int(255 - round(255 * z / rotation_count))
55-
pixels[kw, kh] = c # grayscale
56-
return image.convert('RGBA')
57-
58-
@classmethod
59-
def picture(cls, height, width):
60-
"""
61-
Create a background with a picture
62-
"""
63-
64-
pictures = os.listdir('./pictures')
65-
66-
if len(pictures) > 0:
67-
picture = Image.open('./pictures/' + pictures[random.randint(0, len(pictures) - 1)])
68-
69-
if picture.size[0] < width:
70-
picture = picture.resize([width, int(picture.size[1] * (width / picture.size[0]))], Image.ANTIALIAS)
71-
elif picture.size[1] < height:
72-
picture.thumbnail([int(picture.size[0] * (height / picture.size[1])), height], Image.ANTIALIAS)
73-
74-
if (picture.size[0] == width):
75-
x = 0
76-
else:
77-
x = random.randint(0, picture.size[0] - width)
78-
if (picture.size[1] == height):
79-
y = 0
80-
else:
81-
y = random.randint(0, picture.size[1] - height)
82-
83-
return picture.crop(
84-
(
85-
x,
86-
y,
87-
x + width,
88-
y + height,
89-
)
90-
)
9+
def gaussian_noise(height, width):
10+
"""
11+
Create a background with Gaussian noise (to mimic paper)
12+
"""
13+
14+
# We create an all white image
15+
image = np.ones((height, width)) * 255
16+
17+
# We add gaussian noise
18+
cv2.randn(image, 235, 10)
19+
20+
return Image.fromarray(image).convert('RGBA')
21+
22+
def plain_white(height, width):
23+
"""
24+
Create a plain white background
25+
"""
26+
27+
return Image.new("L", (width, height), 255).convert('RGBA')
28+
29+
def quasicrystal(height, width):
30+
"""
31+
Create a background with quasicrystal (https://en.wikipedia.org/wiki/Quasicrystal)
32+
"""
33+
34+
image = Image.new("L", (width, height))
35+
pixels = image.load()
36+
37+
frequency = random.random() * 30 + 20 # frequency
38+
phase = random.random() * 2 * math.pi # phase
39+
rotation_count = random.randint(10, 20) # of rotations
40+
41+
for kw in range(width):
42+
y = float(kw) / (width - 1) * 4 * math.pi - 2 * math.pi
43+
for kh in range(height):
44+
x = float(kh) / (height - 1) * 4 * math.pi - 2 * math.pi
45+
z = 0.0
46+
for i in range(rotation_count):
47+
r = math.hypot(x, y)
48+
a = math.atan2(y, x) + i * math.pi * 2.0 / rotation_count
49+
z += math.cos(r * math.sin(a) * frequency + phase)
50+
c = int(255 - round(255 * z / rotation_count))
51+
pixels[kw, kh] = c # grayscale
52+
return image.convert('RGBA')
53+
54+
def picture(height, width):
55+
"""
56+
Create a background with a picture
57+
"""
58+
59+
pictures = os.listdir('./pictures')
60+
61+
if len(pictures) > 0:
62+
picture = Image.open('./pictures/' + pictures[random.randint(0, len(pictures) - 1)])
63+
64+
if picture.size[0] < width:
65+
picture = picture.resize([width, int(picture.size[1] * (width / picture.size[0]))], Image.ANTIALIAS)
66+
elif picture.size[1] < height:
67+
picture.thumbnail([int(picture.size[0] * (height / picture.size[1])), height], Image.ANTIALIAS)
68+
69+
if (picture.size[0] == width):
70+
x = 0
71+
else:
72+
x = random.randint(0, picture.size[0] - width)
73+
if (picture.size[1] == height):
74+
y = 0
9175
else:
92-
raise Exception('No images where found in the pictures folder!')
76+
y = random.randint(0, picture.size[1] - height)
77+
78+
return picture.crop(
79+
(
80+
x,
81+
y,
82+
x + width,
83+
y + height,
84+
)
85+
)
86+
else:
87+
raise Exception('No images where found in the pictures folder!')

TextRecognitionDataGenerator/computer_text_generator.py

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,70 @@
22

33
from PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter
44

5-
class ComputerTextGenerator(object):
6-
@classmethod
7-
def generate(cls, text, font, text_color, font_size, orientation, space_width, fit):
8-
if orientation == 0:
9-
return cls.__generate_horizontal_text(text, font, text_color, font_size, space_width, fit)
10-
elif orientation == 1:
11-
return cls.__generate_vertical_text(text, font, text_color, font_size, space_width, fit)
12-
else:
13-
raise ValueError("Unknown orientation " + str(orientation))
5+
def generate(text, font, text_color, font_size, orientation, space_width, fit):
6+
if orientation == 0:
7+
return _generate_horizontal_text(text, font, text_color, font_size, space_width, fit)
8+
elif orientation == 1:
9+
return _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
10+
else:
11+
raise ValueError("Unknown orientation " + str(orientation))
12+
13+
def _generate_horizontal_text(text, font, text_color, font_size, space_width, fit):
14+
image_font = ImageFont.truetype(font=font, size=font_size)
15+
words = text.split(' ')
16+
space_width = image_font.getsize(' ')[0] * space_width
17+
18+
words_width = [image_font.getsize(w)[0] for w in words]
19+
text_width = sum(words_width) + int(space_width) * (len(words) - 1)
20+
text_height = max([image_font.getsize(w)[1] for w in words])
21+
22+
txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
23+
24+
txt_draw = ImageDraw.Draw(txt_img)
25+
26+
colors = [ImageColor.getrgb(c) for c in text_color.split(',')]
27+
c1, c2 = colors[0], colors[-1]
28+
29+
fill = (
30+
random.randint(min(c1[0], c2[0]), max(c1[0], c2[0])),
31+
random.randint(min(c1[1], c2[1]), max(c1[1], c2[1])),
32+
random.randint(min(c1[2], c2[2]), max(c1[2], c2[2]))
33+
)
34+
35+
for i, w in enumerate(words):
36+
txt_draw.text((sum(words_width[0:i]) + i * int(space_width), 0), w, fill=fill, font=image_font)
37+
38+
if fit:
39+
return txt_img.crop(txt_img.getbbox())
40+
else:
41+
return txt_img
42+
43+
def _generate_vertical_text(text, font, text_color, font_size, space_width, fit):
44+
image_font = ImageFont.truetype(font=font, size=font_size)
1445

15-
@classmethod
16-
def __generate_horizontal_text(cls, text, font, text_color, font_size, space_width, fit):
17-
image_font = ImageFont.truetype(font=font, size=font_size)
18-
words = text.split(' ')
19-
space_width = image_font.getsize(' ')[0] * space_width
46+
space_height = int(image_font.getsize(' ')[1] * space_width)
2047

21-
words_width = [image_font.getsize(w)[0] for w in words]
22-
text_width = sum(words_width) + int(space_width) * (len(words) - 1)
23-
text_height = max([image_font.getsize(w)[1] for w in words])
48+
char_heights = [image_font.getsize(c)[1] if c != ' ' else space_height for c in text]
49+
text_width = max([image_font.getsize(c)[0] for c in text])
50+
text_height = sum(char_heights)
2451

25-
txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
52+
txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
2653

27-
txt_draw = ImageDraw.Draw(txt_img)
54+
txt_draw = ImageDraw.Draw(txt_img)
2855

29-
colors = [ImageColor.getrgb(c) for c in text_color.split(',')]
30-
c1, c2 = colors[0], colors[-1]
56+
colors = [ImageColor.getrgb(c) for c in text_color.split(',')]
57+
c1, c2 = colors[0], colors[-1]
3158

32-
fill = (
33-
random.randint(min(c1[0], c2[0]), max(c1[0], c2[0])),
34-
random.randint(min(c1[1], c2[1]), max(c1[1], c2[1])),
35-
random.randint(min(c1[2], c2[2]), max(c1[2], c2[2]))
36-
)
59+
fill = (
60+
random.randint(c1[0], c2[0]),
61+
random.randint(c1[1], c2[1]),
62+
random.randint(c1[2], c2[2])
63+
)
3764

38-
for i, w in enumerate(words):
39-
txt_draw.text((sum(words_width[0:i]) + i * int(space_width), 0), w, fill=fill, font=image_font)
65+
for i, c in enumerate(text):
66+
txt_draw.text((0, sum(char_heights[0:i])), c, fill=fill, font=image_font)
4067

41-
if fit:
42-
return txt_img.crop(txt_img.getbbox())
43-
else:
44-
return txt_img
45-
46-
@classmethod
47-
def __generate_vertical_text(cls, text, font, text_color, font_size, space_width, fit):
48-
image_font = ImageFont.truetype(font=font, size=font_size)
49-
50-
space_height = int(image_font.getsize(' ')[1] * space_width)
51-
52-
char_heights = [image_font.getsize(c)[1] if c != ' ' else space_height for c in text]
53-
text_width = max([image_font.getsize(c)[0] for c in text])
54-
text_height = sum(char_heights)
55-
56-
txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
57-
58-
txt_draw = ImageDraw.Draw(txt_img)
59-
60-
colors = [ImageColor.getrgb(c) for c in text_color.split(',')]
61-
c1, c2 = colors[0], colors[-1]
62-
63-
fill = (
64-
random.randint(c1[0], c2[0]),
65-
random.randint(c1[1], c2[1]),
66-
random.randint(c1[2], c2[2])
67-
)
68-
69-
for i, c in enumerate(text):
70-
txt_draw.text((0, sum(char_heights[0:i])), c, fill=fill, font=image_font)
71-
72-
if fit:
73-
return txt_img.crop(txt_img.getbbox())
74-
else:
75-
return txt_img
68+
if fit:
69+
return txt_img.crop(txt_img.getbbox())
70+
else:
71+
return txt_img

TextRecognitionDataGenerator/data_generator.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
from PIL import Image, ImageFilter
55

6-
from computer_text_generator import ComputerTextGenerator
6+
import computer_text_generator
7+
import background_generator
8+
import distorsion_generator
79
try:
8-
from handwritten_text_generator import HandwrittenTextGenerator
10+
import handwritten_text_generator
911
except ImportError as e:
1012
print('Missing modules for handwritten text generation.')
11-
from background_generator import BackgroundGenerator
12-
from distorsion_generator import DistorsionGenerator
13+
1314

1415
class FakeTextDataGenerator(object):
1516
@classmethod
@@ -34,9 +35,9 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
3435
if is_handwritten:
3536
if orientation == 1:
3637
raise ValueError("Vertical handwritten text is unavailable")
37-
image = HandwrittenTextGenerator.generate(text, text_color, fit)
38+
image = handwritten_text_generator.generate(text, text_color, fit)
3839
else:
39-
image = ComputerTextGenerator.generate(text, font, text_color, size, orientation, space_width, fit)
40+
image = computer_text_generator.generate(text, font, text_color, size, orientation, space_width, fit)
4041

4142
random_angle = random.randint(0-skewing_angle, skewing_angle)
4243

@@ -48,19 +49,19 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
4849
if distorsion_type == 0:
4950
distorted_img = rotated_img # Mind = blown
5051
elif distorsion_type == 1:
51-
distorted_img = DistorsionGenerator.sin(
52+
distorted_img = distorsion_generator.sin(
5253
rotated_img,
5354
vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
5455
horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
5556
)
5657
elif distorsion_type == 2:
57-
distorted_img = DistorsionGenerator.cos(
58+
distorted_img = distorsion_generator.cos(
5859
rotated_img,
5960
vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
6061
horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
6162
)
6263
else:
63-
distorted_img = DistorsionGenerator.random(
64+
distorted_img = distorsion_generator.random(
6465
rotated_img,
6566
vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
6667
horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
@@ -89,13 +90,13 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
8990
# Generate background image #
9091
#############################
9192
if background_type == 0:
92-
background = BackgroundGenerator.gaussian_noise(background_height, background_width)
93+
background = background_generator.gaussian_noise(background_height, background_width)
9394
elif background_type == 1:
94-
background = BackgroundGenerator.plain_white(background_height, background_width)
95+
background = background_generator.plain_white(background_height, background_width)
9596
elif background_type == 2:
96-
background = BackgroundGenerator.quasicrystal(background_height, background_width)
97+
background = background_generator.quasicrystal(background_height, background_width)
9798
else:
98-
background = BackgroundGenerator.picture(background_height, background_width)
99+
background = background_generator.picture(background_height, background_width)
99100

100101
#############################
101102
# Place text with alignment #

0 commit comments

Comments
 (0)