Skip to content

Commit

Permalink
Bugfix img not generated due to missing trailling '/' in dist folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Jianjie Liu (MAIDAP) committed Jan 27, 2021
1 parent d738bbb commit 741c61c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions genalog/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def generate_img(self, full_text_path, template, target_folder=None):

generator = self.doc_generator.create_generator(content, [template])
# Generate the image
doc = next(generator)
doc = next(generator) # TODO: this does not exhaust all of the style combinations in the generator
src = doc.render_array(resolution=self.resolution, channel="GRAYSCALE")
# Degrade the image
dst = self.degrader.apply_effects(src)
Expand All @@ -73,7 +73,7 @@ def generate_img(self, full_text_path, template, target_folder=None):
# save it onto disk
text_filename = os.path.basename(full_text_path)
img_filename = text_filename.replace(".txt", ".png")
img_dst_path = target_folder + "img/" + img_filename
img_dst_path = os.path.join(target_folder, "img", img_filename)
cv2.imwrite(img_dst_path, dst)
return

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test_document_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
UNDEFINED_TEMPLATE_NAME = "not a valid template"

TEST_OUTPUT_DIR = "test_out"
FILE_DESTINATION = TEST_OUTPUT_DIR + "/save.png"
FILE_DESTINATION = os.path.join(TEST_OUTPUT_DIR, "save.png")

CUSTOM_STYLE = {
"font_family": ["Calibri", "Times"],
Expand Down
39 changes: 30 additions & 9 deletions tests/e2e/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import os
import glob

import pytest

from genalog import pipeline
from genalog.generation.document import DocumentGenerator

EXAMPLE_TEXT_FILE = "tests/unit/text/data/gt_1.txt"
INPUT_TEXT_FILENAMES = glob.glob("tests/unit/text/data/gt_*.txt")

STYLES = {"font_size": ["5px"]}
STYLES_COMBINATION = {"font_size": ["5px", "6px"]} # Multiple values per style are not supported right now
DEGRATIONS = [
("blur", {"radius": 3}),
("morphology", {"operation": "close"})
]


@pytest.fixture
Expand All @@ -14,30 +24,41 @@ def default_analog_generator():

@pytest.fixture
def custom_analog_generator():
custom_styles = {"font_size": ["5px"]}
custom_degradation = [("blur", {"radius": 3})]
return pipeline.AnalogDocumentGeneration(
styles=custom_styles, degradations=custom_degradation, resolution=300
styles=STYLES, degradations=DEGRATIONS, resolution=300
)


def test_default_generate_img(default_analog_generator):
assert len(default_analog_generator.list_templates()) > 0
example_template = default_analog_generator.list_templates()[0]
default_analog_generator.generate_img(
EXAMPLE_TEXT_FILE, example_template, target_folder=None
)


def test_custom_generate_img(custom_analog_generator):
assert len(custom_analog_generator.list_templates()) > 0
example_template = custom_analog_generator.list_templates()[0]
custom_analog_generator.generate_img(
EXAMPLE_TEXT_FILE, example_template, target_folder=None
)


def test_generate_dataset_multiprocess():
INPUT_TEXT_FILENAMES = glob.glob("tests/unit/text/data/gt_*.txt")
with pytest.deprecated_call():
pipeline.generate_dataset_multiprocess(
INPUT_TEXT_FILENAMES, "test_out", {}, [], "text_block.html.jinja"
)
@pytest.mark.parametrize("styles", [
STYLES,
pytest.param(
STYLES_COMBINATION, marks=pytest.mark.xfail(
reason="Style combinations are not supported. Only one value per style", strict=True)
)
])
@pytest.mark.parametrize("folder_name", ["result", "result/"])
def test_generate_dataset_multiprocess(tmpdir, folder_name, styles):
assert len(INPUT_TEXT_FILENAMES) > 0
output_folder = os.path.join(tmpdir, folder_name)
pipeline.generate_dataset_multiprocess(
INPUT_TEXT_FILENAMES, output_folder, styles, DEGRATIONS, "text_block.html.jinja"
)
num_generated_img = glob.glob(os.path.join(output_folder, "**/*.png"))
assert len(num_generated_img) > 0
assert len(num_generated_img) == len(INPUT_TEXT_FILENAMES) * len(DocumentGenerator.expand_style_combinations(styles))

0 comments on commit 741c61c

Please sign in to comment.