Skip to content

Commit 720a216

Browse files
authored
Fix non-transparent background in handwritten
1 parent 6da7159 commit 720a216

5 files changed

Lines changed: 30 additions & 12 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: python
22
python:
33
- "3.5"
44
install:
5-
- pip install -r requirements.txt
5+
- pip install -r requirements-hw.txt
66
- pip install codecov
77
script:
88
- python3 tests.py

TextRecognitionDataGenerator/background_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ def gaussian_noise(cls, height, width):
1919
# We add gaussian noise
2020
cv2.randn(image, 235, 10)
2121

22-
return Image.fromarray(image).convert('RGB')
22+
return Image.fromarray(image).convert('RGBA')
2323

2424
@classmethod
2525
def plain_white(cls, height, width):
2626
"""
2727
Create a plain white background
2828
"""
2929

30-
return Image.new("L", (width, height), 255).convert('RGB')
30+
return Image.new("L", (width, height), 255).convert('RGBA')
3131

3232
@classmethod
3333
def quasicrystal(cls, height, width):
@@ -53,7 +53,7 @@ def quasicrystal(cls, height, width):
5353
z += math.cos(r * math.sin(a) * frequency + phase)
5454
c = int(255 - round(255 * z / rotation_count))
5555
pixels[kw, kh] = c # grayscale
56-
return image.convert('RGB')
56+
return image.convert('RGBA')
5757

5858
@classmethod
5959
def picture(cls, height, width):

TextRecognitionDataGenerator/handwritten_text_generator.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ def __sample_text(cls, sess, args_text, translation):
9090
@classmethod
9191
def __crop_white_borders(cls, image):
9292
image_data = np.asarray(image)
93-
non_empty_columns = np.where(image_data.min(axis=0)<255)[0]
94-
non_empty_rows = np.where(image_data.min(axis=1)<255)[0]
93+
grey_image_data = np.asarray(image.convert('L'))
94+
non_empty_columns = np.where(grey_image_data.min(axis=0) < 255)[0]
95+
non_empty_rows = np.where(grey_image_data.min(axis=1) < 255)[0]
9596
cropBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
96-
97-
image_data_new = image_data[cropBox[0]:cropBox[1]+1, cropBox[2]:cropBox[3]+1]
97+
image_data_new = image_data[cropBox[0]:cropBox[1]+1, cropBox[2]:cropBox[3]+1, :]
9898

9999
return Image.fromarray(image_data_new)
100100

@@ -105,7 +105,7 @@ def __join_images(cls, images):
105105
total_width = sum(widths) - 35 * len(images)
106106
max_height = max(heights)
107107

108-
compound_image = Image.new('L', (total_width, max_height))
108+
compound_image = Image.new('RGBA', (total_width, max_height))
109109

110110
x_offset = 0
111111
for im in images:
@@ -142,10 +142,13 @@ def generate(cls, text):
142142
for stroke in cls.__split_strokes(cls.__cumsum(np.array(coords))):
143143
plt.plot(stroke[:, 0], -stroke[:, 1], color='#080808')
144144

145+
fig.patch.set_alpha(0)
146+
fig.patch.set_facecolor('none')
147+
145148
canvas = plt.get_current_fig_manager().canvas
146149
canvas.draw()
147150

148-
image = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb()).convert('L')
151+
image = Image.frombytes('RGBA', canvas.get_width_height(), canvas.buffer_rgba())
149152
images.append(cls.__crop_white_borders(image))
150153

151154
plt.close()

requirements-hw.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
beautifulsoup4==4.6.0
2+
numpy==1.15.1
3+
opencv-python==4.0.0.21
4+
tqdm==4.23.4
5+
Pillow==5.1.0
6+
requests==2.20.0
7+
tensorflow==1.13.1
8+
matplotlib==3.0.2
9+
seaborn==0.9.0

tests.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def test_generate_chinese_string(self):
512512
)
513513

514514
def test_generate_data_with_white_background(self):
515-
BackgroundGenerator.plain_white(64, 128).save('tests/out/white_background.jpg')
515+
BackgroundGenerator.plain_white(64, 128).convert('RGB').save('tests/out/white_background.jpg')
516516

517517
self.assertTrue(
518518
md5('tests/out/white_background.jpg') == md5('tests/expected_results/white_background.jpg')
@@ -521,7 +521,7 @@ def test_generate_data_with_white_background(self):
521521
os.remove('tests/out/white_background.jpg')
522522

523523
def test_generate_data_with_gaussian_background(self):
524-
BackgroundGenerator.gaussian_noise(64, 128).save('tests/out/gaussian_background.jpg')
524+
BackgroundGenerator.gaussian_noise(64, 128).convert('RGB').save('tests/out/gaussian_background.jpg')
525525

526526
self.assertTrue(
527527
md5('tests/out/gaussian_background.jpg') == md5('tests/expected_results/gaussian_background.jpg')
@@ -598,6 +598,12 @@ def test_random_sequences_symbols_only(self):
598598
self.assertTrue(all([c in "!\"#$%&'()*+,-./:;?@[\\]^_`{|}~" for c in f.readline().split(' ')[1][:-1]]))
599599
empty_directory('tests/out/')
600600

601+
def test_handwritten(self):
602+
args = ['python3', 'run.py', '-c', '1', '--output_dir', '../tests/out/']
603+
subprocess.Popen(args, cwd="TextRecognitionDataGenerator/").wait()
604+
self.assertTrue(len(os.listdir('tests/out/')) == 1)
605+
empty_directory('tests/out/')
606+
601607
# def test_word_count(self):
602608
# args = ['python3', 'run.py', '-c', '1', '-w', '5']
603609
# subprocess.Popen(args, cwd="TextRecognitionDataGenerator/").wait()

0 commit comments

Comments
 (0)