Skip to content

Commit

Permalink
Bump requirements (Version 1.4.0)
Browse files Browse the repository at this point in the history
* Bump requirements

* Bump OpenCV version

* Update jpeg samples
  • Loading branch information
Belval authored Mar 17, 2020
1 parent 54ce195 commit f28e1dc
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 33 deletions.
4 changes: 2 additions & 2 deletions requirements-hw.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.
tensorflow>=1.13.1
tensorflow>=1.13.1,<1.14
matplotlib>=3.0.2
seaborn>=0.9.0
seaborn>=0.9.0
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
],
Expand Down
Binary file modified tests/expected_results/TEST TEST TEST_13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected_results/TEST TEST TEST_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected_results/TEST TEST TEST_7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion trdg/background_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion trdg/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
9 changes: 7 additions & 2 deletions trdg/distorsion_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)


Expand Down Expand Up @@ -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)),
)
2 changes: 1 addition & 1 deletion trdg/generators/from_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ def next(self):
self.output_mask,
),
self.strings[(self.generated_count - 1) % len(self.strings)],
)
)
55 changes: 37 additions & 18 deletions trdg/handwritten_text_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions trdg/run.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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()

Expand Down

0 comments on commit f28e1dc

Please sign in to comment.