Skip to content

Commit 4096d3d

Browse files
authored
Replace import random by import random as rnd
1 parent 13b22cc commit 4096d3d

7 files changed

Lines changed: 38 additions & 38 deletions

File tree

TextRecognitionDataGenerator/background_generator.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cv2
22
import math
33
import os
4-
import random
4+
import random as rnd
55
import numpy as np
66

77
from PIL import Image, ImageDraw, ImageFilter
@@ -34,9 +34,9 @@ def quasicrystal(height, width):
3434
image = Image.new("L", (width, height))
3535
pixels = image.load()
3636

37-
frequency = random.random() * 30 + 20 # frequency
38-
phase = random.random() * 2 * math.pi # phase
39-
rotation_count = random.randint(10, 20) # of rotations
37+
frequency = rnd.random() * 30 + 20 # frequency
38+
phase = rnd.random() * 2 * math.pi # phase
39+
rotation_count = rnd.randint(10, 20) # of rotations
4040

4141
for kw in range(width):
4242
y = float(kw) / (width - 1) * 4 * math.pi - 2 * math.pi
@@ -59,23 +59,23 @@ def picture(height, width):
5959
pictures = os.listdir('./pictures')
6060

6161
if len(pictures) > 0:
62-
picture = Image.open('./pictures/' + pictures[random.randint(0, len(pictures) - 1)])
62+
pic = Image.open('./pictures/' + pictures[rnd.randint(0, len(pictures) - 1)])
6363

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)
64+
if pic.size[0] < width:
65+
pic = pic.resize([width, int(pic.size[1] * (width / pic.size[0]))], Image.ANTIALIAS)
66+
elif pic.size[1] < height:
67+
pic.thumbnail([int(pic.size[0] * (height / pic.size[1])), height], Image.ANTIALIAS)
6868

69-
if (picture.size[0] == width):
69+
if (pic.size[0] == width):
7070
x = 0
7171
else:
72-
x = random.randint(0, picture.size[0] - width)
73-
if (picture.size[1] == height):
72+
x = rnd.randint(0, pic.size[0] - width)
73+
if (pic.size[1] == height):
7474
y = 0
7575
else:
76-
y = random.randint(0, picture.size[1] - height)
76+
y = rnd.randint(0, pic.size[1] - height)
7777

78-
return picture.crop(
78+
return pic.crop(
7979
(
8080
x,
8181
y,

TextRecognitionDataGenerator/computer_text_generator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import random
1+
import random as rnd
22

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

@@ -27,9 +27,9 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, fi
2727
c1, c2 = colors[0], colors[-1]
2828

2929
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]))
30+
rnd.randint(min(c1[0], c2[0]), max(c1[0], c2[0])),
31+
rnd.randint(min(c1[1], c2[1]), max(c1[1], c2[1])),
32+
rnd.randint(min(c1[2], c2[2]), max(c1[2], c2[2]))
3333
)
3434

3535
for i, w in enumerate(words):
@@ -57,9 +57,9 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
5757
c1, c2 = colors[0], colors[-1]
5858

5959
fill = (
60-
random.randint(c1[0], c2[0]),
61-
random.randint(c1[1], c2[1]),
62-
random.randint(c1[2], c2[2])
60+
rnd.randint(c1[0], c2[0]),
61+
rnd.randint(c1[1], c2[1]),
62+
rnd.randint(c1[2], c2[2])
6363
)
6464

6565
for i, c in enumerate(text):

TextRecognitionDataGenerator/data_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
import random
2+
import random as rnd
33

44
from PIL import Image, ImageFilter
55

@@ -39,7 +39,7 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
3939
else:
4040
image = computer_text_generator.generate(text, font, text_color, size, orientation, space_width, fit)
4141

42-
random_angle = random.randint(0-skewing_angle, skewing_angle)
42+
random_angle = rnd.randint(0-skewing_angle, skewing_angle)
4343

4444
rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)
4545

@@ -117,7 +117,7 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
117117

118118
final_image = background.filter(
119119
ImageFilter.GaussianBlur(
120-
radius=(blur if not random_blur else random.randint(0, blur))
120+
radius=(blur if not random_blur else rnd.randint(0, blur))
121121
)
122122
)
123123

TextRecognitionDataGenerator/distorsion_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cv2
22
import math
33
import os
4-
import random
4+
import random as rnd
55
import numpy as np
66

77
from PIL import Image, ImageDraw, ImageFilter
@@ -78,4 +78,4 @@ def random(image, vertical=False, horizontal=False):
7878

7979
max_offset = int(image.height ** 0.4)
8080

81-
return _apply_func_distorsion(image, vertical, horizontal, max_offset, (lambda x: random.randint(0, max_offset)))
81+
return _apply_func_distorsion(image, vertical, horizontal, max_offset, (lambda x: rnd.randint(0, max_offset)))

TextRecognitionDataGenerator/handwritten_text_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import pickle
33
import numpy as np
4-
import random
4+
import random as rnd
55
import tensorflow as tf
66
import matplotlib.pyplot as plt
77
import matplotlib.cm as cm
@@ -124,9 +124,9 @@ def generate(text, text_color):
124124
c1, c2 = colors[0], colors[-1]
125125

126126
color = '#{:02x}{:02x}{:02x}'.format(
127-
random.randint(min(c1[0], c2[0]), max(c1[0], c2[0])),
128-
random.randint(min(c1[1], c2[1]), max(c1[1], c2[1])),
129-
random.randint(min(c1[2], c2[2]), max(c1[2], c2[2]))
127+
rnd.randint(min(c1[0], c2[0]), max(c1[0], c2[0])),
128+
rnd.randint(min(c1[1], c2[1]), max(c1[1], c2[1])),
129+
rnd.randint(min(c1[2], c2[2]), max(c1[2], c2[2]))
130130
)
131131

132132
for word in text.split(' '):

TextRecognitionDataGenerator/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import os, errno
3-
import random
3+
import random as rnd
44
import string
55
import sys
66

@@ -336,7 +336,7 @@ def main():
336336
zip(
337337
[i for i in range(0, string_count)],
338338
strings,
339-
[fonts[random.randrange(0, len(fonts))] for _ in range(0, string_count)],
339+
[fonts[rnd.randrange(0, len(fonts))] for _ in range(0, string_count)],
340340
[args.output_dir] * string_count,
341341
[args.format] * string_count,
342342
[args.extension] * string_count,

TextRecognitionDataGenerator/string_generator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import random
1+
import random as rnd
22
import re
33
import string
44
import requests
@@ -33,8 +33,8 @@ def create_strings_from_dict(length, allow_variable, count, lang_dict):
3333
strings = []
3434
for _ in range(0, count):
3535
current_string = ""
36-
for _ in range(0, random.randint(1, length) if allow_variable else length):
37-
current_string += lang_dict[random.randrange(dict_len)][:-1]
36+
for _ in range(0, rnd.randint(1, length) if allow_variable else length):
37+
current_string += lang_dict[rnd.randrange(dict_len)][:-1]
3838
current_string += ' '
3939
strings.append(current_string[:-1])
4040
return strings
@@ -100,9 +100,9 @@ def create_strings_randomly(length, allow_variable, count, let, num, sym, lang):
100100
strings = []
101101
for _ in range(0, count):
102102
current_string = ""
103-
for _ in range(0, random.randint(1, length) if allow_variable else length):
104-
seq_len = random.randint(min_seq_len, max_seq_len)
105-
current_string += ''.join([random.choice(pool) for _ in range(seq_len)])
103+
for _ in range(0, rnd.randint(1, length) if allow_variable else length):
104+
seq_len = rnd.randint(min_seq_len, max_seq_len)
105+
current_string += ''.join([rnd.choice(pool) for _ in range(seq_len)])
106106
current_string += ' '
107107
strings.append(current_string[:-1])
108108
return strings

0 commit comments

Comments
 (0)