diff --git a/requirements-hw.txt b/requirements-hw.txt index 18a41468db..42b3dc6a77 100644 --- a/requirements-hw.txt +++ b/requirements-hw.txt @@ -1,4 +1,4 @@ . -tensorflow>=1.13.1 +tensorflow>=1.13.1,<1.14 matplotlib>=3.0.2 -seaborn>=0.9.0 \ No newline at end of file +seaborn>=0.9.0 diff --git a/setup.py b/setup.py index 86b014731a..bba6dfb5c8 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name="trdg", - version="1.3.2", + version="1.4.0", description="TextRecognitionDataGenerator: A synthetic data generator for text recognition", long_description=long_description, long_description_content_type="text/markdown", @@ -26,7 +26,7 @@ # 3 - Alpha # 4 - Beta # 5 - Production/Stable - "Development Status :: 3 - Alpha", + "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2", @@ -41,10 +41,10 @@ packages=find_packages(exclude=["contrib", "docs", "tests"]), include_package_data=True, install_requires=[ - "pillow==5.1.0", - "numpy>=1.15.1,<1.17", + "pillow==7.0.0", + "numpy>=1.16.4,<1.17", "requests>=2.20.0", - "opencv-python>=4.0.0.21", + "opencv-python>=4.2.0.32", "tqdm>=4.23.0", "beautifulsoup4>=4.6.0" ], diff --git a/tests/expected_results/TEST TEST TEST_13.jpg b/tests/expected_results/TEST TEST TEST_13.jpg index d2752abba8..c8c59e5cd5 100644 Binary files a/tests/expected_results/TEST TEST TEST_13.jpg and b/tests/expected_results/TEST TEST TEST_13.jpg differ diff --git a/tests/expected_results/TEST TEST TEST_4.jpg b/tests/expected_results/TEST TEST TEST_4.jpg index b9e499febe..440eb34fcc 100644 Binary files a/tests/expected_results/TEST TEST TEST_4.jpg and b/tests/expected_results/TEST TEST TEST_4.jpg differ diff --git a/tests/expected_results/TEST TEST TEST_7.jpg b/tests/expected_results/TEST TEST TEST_7.jpg index 631cab6f60..0cdf976fc6 100644 Binary files a/tests/expected_results/TEST TEST TEST_7.jpg and b/tests/expected_results/TEST TEST TEST_7.jpg differ diff --git a/trdg/background_generator.py b/trdg/background_generator.py index 4335a8e9fb..90f332c56a 100644 --- a/trdg/background_generator.py +++ b/trdg/background_generator.py @@ -63,7 +63,11 @@ def picture(height, width): pictures = os.listdir(os.path.join(script_path, "pictures")) if len(pictures) > 0: - pic = Image.open(os.path.join(script_path, "pictures", pictures[rnd.randint(0, len(pictures) - 1)])) + pic = Image.open( + os.path.join( + script_path, "pictures", pictures[rnd.randint(0, len(pictures) - 1)] + ) + ) if pic.size[0] < width: pic = pic.resize( diff --git a/trdg/data_generator.py b/trdg/data_generator.py index cb6ae3b89a..88566cbb8d 100644 --- a/trdg/data_generator.py +++ b/trdg/data_generator.py @@ -162,7 +162,9 @@ def generate( background_img = background_generator.picture( background_height, background_width ) - background_mask = Image.new("RGB", (background_width, background_height), (0, 0, 0)) + background_mask = Image.new( + "RGB", (background_width, background_height), (0, 0, 0) + ) ############################# # Place text with alignment # diff --git a/trdg/distorsion_generator.py b/trdg/distorsion_generator.py index a3a99e2ed6..979c4b7d8b 100644 --- a/trdg/distorsion_generator.py +++ b/trdg/distorsion_generator.py @@ -93,7 +93,7 @@ def _apply_func_distorsion(image, mask, vertical, horizontal, max_offset, func): ).convert("RGBA"), Image.fromarray( np.uint8(new_mask_arr_copy if horizontal and vertical else new_mask_arr) - ).convert("RGB") + ).convert("RGB"), ) @@ -139,5 +139,10 @@ def random(image, mask, vertical=False, horizontal=False): max_offset = int(image.height ** 0.4) return _apply_func_distorsion( - image, mask, vertical, horizontal, max_offset, (lambda x: rnd.randint(0, max_offset)) + image, + mask, + vertical, + horizontal, + max_offset, + (lambda x: rnd.randint(0, max_offset)), ) diff --git a/trdg/generators/from_strings.py b/trdg/generators/from_strings.py index ad39664bae..55d97001eb 100644 --- a/trdg/generators/from_strings.py +++ b/trdg/generators/from_strings.py @@ -94,4 +94,4 @@ def next(self): self.output_mask, ), self.strings[(self.generated_count - 1) % len(self.strings)], - ) \ No newline at end of file + ) diff --git a/trdg/handwritten_text_generator.py b/trdg/handwritten_text_generator.py index 4a145edd91..a2c42488e2 100644 --- a/trdg/handwritten_text_generator.py +++ b/trdg/handwritten_text_generator.py @@ -9,22 +9,35 @@ import seaborn from PIL import Image, ImageColor from collections import namedtuple +import warnings + +warnings.filterwarnings("ignore") + def download_model_weights(): from pathlib import Path - import urllib.request + import urllib.request + cwd = os.path.dirname(os.path.abspath(__file__)) - for k in ['model-29.data-00000-of-00001','model-29.index','model-29.meta','translation.pkl']: - download_dir = Path(cwd)/'handwritten_model/' - download_dir.mkdir(exist_ok=True,parents=True) - if (download_dir/f'{k}').exists(): continue - print(f'file {k} not found, downloading from git repo..') + for k in [ + "model-29.data-00000-of-00001", + "model-29.index", + "model-29.meta", + "translation.pkl", + ]: + download_dir = Path(cwd) / "handwritten_model/" + download_dir.mkdir(exist_ok=True, parents=True) + if (download_dir / f"{k}").exists(): + continue + print(f"file {k} not found, downloading from git repo..") urllib.request.urlretrieve( - f'https://raw.github.com/Belval/TextRecognitionDataGenerator/master/trdg/handwritten_model/{k}', - download_dir/f'{k}') - print(f'file {k} saved to disk') + f"https://raw.github.com/Belval/TextRecognitionDataGenerator/master/trdg/handwritten_model/{k}", + download_dir / f"{k}", + ) + print(f"file {k} saved to disk") return cwd + def _sample(e, mu1, mu2, std1, std2, rho): cov = np.array([[std1 * std1, std1 * std2 * rho], [std1 * std2 * rho, std2 * std2]]) mean = np.array([mu1, mu2]) @@ -71,7 +84,9 @@ def _sample_text(sess, args_text, translation): "finish", "zero_states", ] - vs = namedtuple("Params", fields)(*[tf.compat.v1.get_collection(name)[0] for name in fields]) + vs = namedtuple("Params", fields)( + *[tf.compat.v1.get_collection(name)[0] for name in fields] + ) text = np.array([translation.get(c, 0) for c in args_text]) sequence = np.eye(len(translation), dtype=np.float32)[text] @@ -163,14 +178,20 @@ def _join_images(images): def generate(text, text_color): cd = download_model_weights() - with open(os.path.join(cd, os.path.join("handwritten_model", "translation.pkl")), "rb") as file: + with open( + os.path.join(cd, os.path.join("handwritten_model", "translation.pkl")), "rb" + ) as file: translation = pickle.load(file) config = tf.compat.v1.ConfigProto(device_count={"GPU": 0}) tf.compat.v1.reset_default_graph() with tf.compat.v1.Session(config=config) as sess: - saver = tf.compat.v1.train.import_meta_graph(os.path.join(cd,"handwritten_model/model-29.meta")) - saver.restore(sess,os.path.join(cd,os.path.join("handwritten_model/model-29"))) + saver = tf.compat.v1.train.import_meta_graph( + os.path.join(cd, "handwritten_model/model-29.meta") + ) + saver.restore( + sess, os.path.join(cd, os.path.join("handwritten_model/model-29")) + ) images = [] colors = [ImageColor.getrgb(c) for c in text_color.split(",")] c1, c2 = colors[0], colors[-1] @@ -203,13 +224,11 @@ def generate(text, text_color): canvas = plt.get_current_fig_manager().canvas canvas.draw() - + s, (width, height) = canvas.print_to_buffer() - image = Image.frombytes( - "RGBA", (width, height), s - ) + image = Image.frombytes("RGBA", (width, height), s) mask = Image.new("RGB", (width, height), (0, 0, 0)) - + images.append(_crop_white_borders(image)) plt.close() diff --git a/trdg/run.py b/trdg/run.py index 78128572f0..b9dc74fcb2 100755 --- a/trdg/run.py +++ b/trdg/run.py @@ -1,7 +1,8 @@ import argparse import os, errno import sys -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + +sys.path.append(os.path.join(os.path.dirname(__file__), "..")) import random as rnd import string @@ -276,7 +277,11 @@ def parse_arguments(): "-ft", "--font", type=str, nargs="?", help="Define font to be used" ) parser.add_argument( - "-fd", "--font_dir", type=str, nargs="?", help="Define a font directory to be used" + "-fd", + "--font_dir", + type=str, + nargs="?", + help="Define a font directory to be used", ) parser.add_argument( "-ca", @@ -286,7 +291,7 @@ def parse_arguments(): help="Generate upper or lowercase only. arguments: upper or lower. Example: --case upper", ) parser.add_argument( - "-dt", "--dict", type=str, nargs="?", help="Define dictionary to be used" + "-dt", "--dict", type=str, nargs="?", help="Define the dictionary to be used" ) return parser.parse_args()