Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add textimage #1848

Merged
merged 25 commits into from
Jul 26, 2024
Merged

Add textimage #1848

merged 25 commits into from
Jul 26, 2024

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Jul 20, 2024

Summary by Sourcery

This pull request introduces a new 'TextImage' augmentation for rendering text on images, refactors test cases to include this new feature, and updates the build and documentation to support the new functionality.

  • New Features:
    • Introduced a new 'TextImage' augmentation for rendering text on images with various configurations such as custom fonts, font sizes, colors, and augmentation methods.
  • Enhancements:
    • Refactored test cases to include the new 'TextImage' augmentation, ensuring compatibility and proper functionality.
    • Moved augmentation class parameters to a new file 'tests/aug_definitions.py' for better organization and maintainability.
  • Build:
    • Updated 'setup.py' to include new dependencies 'pillow' and 'nltk' for text augmentation features.
  • Documentation:
    • Added 'TextImage' to the list of available pixel-level transforms in the README.md.
  • Tests:
    • Added comprehensive test cases for the 'TextImage' augmentation in 'tests/test_text.py' to validate text rendering and augmentation functionalities.
    • Updated existing test files to include 'TextImage' in various test scenarios, ensuring it is tested alongside other augmentations.

Copy link
Contributor

sourcery-ai bot commented Jul 20, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new TextImage augmentation for rendering text on images, refactors augmentation class parameters into a new file for better organization, and updates tests to ensure compatibility and functionality. Additionally, the pre-commit configuration is updated to use ruff version v0.5.5.

File-Level Changes

Files Changes
tests/test_serialization.py
tests/test_augmentations.py
tests/test_transforms.py
tests/test_core.py
tests/test_targets.py
Updated test cases to include the new TextImage augmentation and refactored augmentation class parameters.
albumentations/augmentations/text/transforms.py
albumentations/augmentations/text/functional.py
tests/test_text.py
Introduced new TextImage augmentation and its corresponding functional and test files.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@ternaus ternaus marked this pull request as draft July 20, 2024 01:40
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ternaus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 7 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

tests/test_text.py Show resolved Hide resolved
tests/test_text.py Show resolved Hide resolved
tests/test_transforms.py Show resolved Hide resolved
albumentations/augmentations/text/functional.py Outdated Show resolved Hide resolved
albumentations/augmentations/text/transforms.py Outdated Show resolved Hide resolved
@ternaus ternaus marked this pull request as ready for review July 26, 2024 02:38
@ternaus ternaus merged commit 8d5c775 into main Jul 26, 2024
17 checks passed
@ternaus ternaus deleted the add_textimage branch July 26, 2024 02:38
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ternaus - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Hardcoded file path for font detected. (link)
  • Hardcoded file path for font detected. (link)
  • Hardcoded file path for font detected. (link)
  • Hardcoded file path for font detected. (link)
  • Hardcoded file path for font detected. (link)
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🔴 Security: 5 blocking issues
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

albumentations/augmentations/transforms.py Show resolved Hide resolved
tests/test_text.py Show resolved Hide resolved
tests/aug_definitions.py Show resolved Hide resolved
tests/test_augmentations.py Show resolved Hide resolved
tests/test_text.py Show resolved Hide resolved
Comment on lines +50 to +55
if len(words_in_sentence) == 1:
assert result == sentence, "Single word input should remain unchanged"
else:
assert words_in_result != words_in_sentence or num_words == 0, f"Result should be different from input for n={num_words}"
assert len(words_in_result) == len(words_in_sentence), "Result should have the same number of words as input"
assert sorted(words_in_result) == sorted(words_in_sentence), "Result should contain the same words as input"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Avoid conditionals in tests. (no-conditionals-in-tests)

ExplanationAvoid complex code, like conditionals, in test functions.

Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:

  • loops
  • conditionals

Some ways to fix this:

  • Use parametrized tests to get rid of the loop.
  • Move the complex logic into helpers.
  • Move the complex part into pytest fixtures.

Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.

Software Engineering at Google / Don't Put Logic in Tests

Comment on lines +82 to +83
if stopwords is None:
stopwords = ["and", "the", "is", "in", "at", "of"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Avoid conditionals in tests. (no-conditionals-in-tests)

ExplanationAvoid complex code, like conditionals, in test functions.

Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:

  • loops
  • conditionals

Some ways to fix this:

  • Use parametrized tests to get rid of the loop.
  • Move the complex logic into helpers.
  • Move the complex part into pytest fixtures.

Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.

Software Engineering at Google / Don't Put Logic in Tests

Comment on lines +144 to +150
if get_num_channels(image) in {1, 3}:
pil_image = ftext.convert_image_to_pil(image)
result = ftext.draw_text_on_pil_image(pil_image, metadata_list)
assert isinstance(result, Image.Image)
else:
result = ftext.draw_text_on_multi_channel_image(image, metadata_list)
assert isinstance(result, np.ndarray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Avoid conditionals in tests. (no-conditionals-in-tests)

ExplanationAvoid complex code, like conditionals, in test functions.

Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:

  • loops
  • conditionals

Some ways to fix this:

  • Use parametrized tests to get rid of the loop.
  • Move the complex logic into helpers.
  • Move the complex part into pytest fixtures.

Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.

Software Engineering at Google / Don't Put Logic in Tests

@@ -21,7 +20,7 @@
from albumentations.core.types import ImageCompressionType
from albumentations.random_utils import get_random_seed
from albumentations.augmentations.transforms import RandomSnow
from tests.conftest import IMAGES, RECTANGULAR_FLOAT_IMAGE, SQUARE_FLOAT_IMAGE, SQUARE_MULTI_UINT8_IMAGE, SQUARE_UINT8_IMAGE
from tests.conftest import IMAGES, SQUARE_FLOAT_IMAGE, SQUARE_MULTI_UINT8_IMAGE, SQUARE_UINT8_IMAGE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Don't import test modules. (dont-import-test-modules)

ExplanationDon't import test modules.

Tests should be self-contained and don't depend on each other.

If a helper function is used by multiple tests,
define it in a helper module,
instead of importing one test from the other.

overlay_data: list[dict[str, Any]],
**params: Any,
) -> np.ndarray:
return ftext.render_text(img, overlay_data, clear_bg=self.clear_bg)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here can we do something like, please?:

return ftext.render_text(img, overlay_data, clear_bg=self.clear_bg), overlay_data

Because we need to keep track of the modified text. For example, in document parsing task, it is mandatory to have exactly matching annotations with their corresponding document images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants