Skip to content

Commit

Permalink
Merge pull request #410 from kwcckw/rel
Browse files Browse the repository at this point in the history
Update to version 8.2.5.
  • Loading branch information
kwcckw authored Dec 1, 2023
2 parents 65f2ca2 + db9a44a commit 1d38764
Show file tree
Hide file tree
Showing 156 changed files with 8,638 additions and 1,065 deletions.
22 changes: 22 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the version of Python and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.11"

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: conf.py

# We recommend specifying your dependencies to enable reproducible builds:
# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: requirements-dev.txt
181 changes: 130 additions & 51 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion augraphy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from augraphy.default import *
from augraphy.utilities import *

__version__ = "8.2.4"
__version__ = "8.2.5"
66 changes: 44 additions & 22 deletions augraphy/augmentations/badphotocopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
class BadPhotoCopy(Augmentation):
"""Uses added noise to generate an effect of dirty copier.
:param mask: Mask of noise to generate badphotocopy effect.
:type mask: uint8, optional
:param noise_mask: Mask of noise to generate badphotocopy effect.
:type noise_mask: uint8, optional
:param noise_type: Types of noises to generate different mask patterns. Use -1 to select randomly.
1 = sklearn.datasets' make_blobs noise
2 = gaussian noise
Expand Down Expand Up @@ -53,7 +53,7 @@ class BadPhotoCopy(Augmentation):

def __init__(
self,
mask=None,
noise_mask=None,
noise_type=-1,
noise_side="random",
noise_iteration=(1, 2),
Expand All @@ -70,7 +70,7 @@ def __init__(
):
"""Constructor method"""
super().__init__(p=p, numba_jit=numba_jit)
self.mask = mask
self.noise_mask = noise_mask
self.noise_type = noise_type
self.noise_side = noise_side
self.noise_iteration = noise_iteration
Expand Down Expand Up @@ -103,7 +103,7 @@ def __init__(

# Constructs a string representation of this Augmentation.
def __repr__(self):
return f"BadPhotoCopy(mask={self.mask}, noise_type={self.noise_type}, noise_side={self.noise_side}, noise_iteration={self.noise_iteration}, noise_size={self.noise_size}, noise_value={self.noise_value}, noise_sparsity={self.noise_sparsity}, noise_concentration={self.noise_concentration}, blur_noise={self.blur_noise}, blur_noise_kernel={self.blur_noise_kernel}, wave_pattern={self.wave_pattern}, edge_effect={self.edge_effect}, numba_jit={self.numba_jit}, p={self.p})"
return f"BadPhotoCopy(noise_mask={self.noise_mask}, noise_type={self.noise_type}, noise_side={self.noise_side}, noise_iteration={self.noise_iteration}, noise_size={self.noise_size}, noise_value={self.noise_value}, noise_sparsity={self.noise_sparsity}, noise_concentration={self.noise_concentration}, blur_noise={self.blur_noise}, blur_noise_kernel={self.blur_noise_kernel}, wave_pattern={self.wave_pattern}, edge_effect={self.edge_effect}, numba_jit={self.numba_jit}, p={self.p})"

@staticmethod
@jit(nopython=True, cache=True, parallel=True)
Expand Down Expand Up @@ -206,16 +206,26 @@ def apply_wave(self, mask, noise_side):

return mask.astype("uint8")

def apply_augmentation(self, image):
def apply_augmentation(self, image, mask, keypoints, bounding_boxes):
"""applies augmentation to the input image.
:param image: The image to apply the augmentation.
:type image: numpy.array (numpy.uint8)
:param mask: The mask of labels for each pixel. Mask value should be in range of 0 to 255.
:type mask: numpy array (uint8), optional
:param keypoints: A dictionary of single or multiple labels where each label is a nested list of points coordinate (x, y).
:type keypoints: dictionary, optional
:param bounding_boxes: A nested list where each nested list contains box location (x1, y1, x2, y2).
:type bounding_boxes: list, optional
"""

# convert and make sure image is color image
# check and convert image into BGR format
has_alpha = 0
if len(image.shape) > 2:
is_gray = 0
if image.shape[2] == 4:
has_alpha = 1
image, image_alpha = image[:, :, :3], image[:, :, 3]
else:
is_gray = 1
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
Expand All @@ -242,11 +252,11 @@ def apply_augmentation(self, image):
noise_side = self.noise_side

# check if provided mask is numpy array
if isinstance(self.mask, np.ndarray):
mask = self.mask
if isinstance(self.noise_mask, np.ndarray):
noise_mask = self.noise_mask
# noise mask needs to be in grayscale form
if len(mask.shape) > 2:
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
if len(noise_mask.shape) > 2:
noise_mask = cv2.cvtColor(noise_mask, cv2.COLOR_BGR2GRAY)

# generate mask of noise
else:
Expand All @@ -255,7 +265,7 @@ def apply_augmentation(self, image):
noise_side=noise_side,
numba_jit=self.numba_jit,
)
mask = noise_generator.generate_noise(
noise_mask = noise_generator.generate_noise(
noise_value=self.noise_value,
noise_iteration=self.noise_iteration,
noise_size=self.noise_size,
Expand All @@ -266,29 +276,29 @@ def apply_augmentation(self, image):
)

# resize back to original size
mask = cv2.resize(mask, (xsize, ysize)).astype("uint8")
noise_mask = cv2.resize(noise_mask, (xsize, ysize)).astype("uint8")

# apply blur to mask of noise
if self.blur_noise == -1:
blur_noise = random.choice((0, 1))
else:
blur_noise = self.blur_noise
if blur_noise:
mask = cv2.GaussianBlur(mask, self.blur_noise_kernel, 0)
noise_mask = cv2.GaussianBlur(noise_mask, self.blur_noise_kernel, 0)

# apply wave pattern to mask
if self.wave_pattern == -1:
wave_pattern = random.choice((0, 1))
else:
wave_pattern = self.wave_pattern
if wave_pattern:
mask = self.apply_wave(mask, noise_side)
noise_mask = self.apply_wave(noise_mask, noise_side)

# add dotted noise effect to mask (unsmoothen)
if not blur_noise:
noise_mask = np.random.randint(0, 255, (ysize, xsize))
mask[mask > noise_mask] = 255
noise_img = mask
random_mask = np.random.randint(0, 255, (ysize, xsize))
noise_mask[noise_mask > random_mask] = 255
noise_img = noise_mask

# blend noise into image
result = image.copy()
Expand Down Expand Up @@ -336,11 +346,23 @@ def apply_augmentation(self, image):
# return image follows the input image color channel
if is_gray:
result = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)

return result
if has_alpha:
result = np.dstack((result, image_alpha))

# check for additional output of mask, keypoints and bounding boxes
outputs_extra = []
if mask is not None or keypoints is not None or bounding_boxes is not None:
outputs_extra = [mask, keypoints, bounding_boxes]

# returns additional mask, keypoints and bounding boxes if there is additional input
if outputs_extra:
# returns in the format of [image, mask, keypoints, bounding_boxes]
return [result] + outputs_extra
else:
return result

# Applies the Augmentation to input data.
def __call__(self, image, layer=None, force=False):
def __call__(self, image, layer=None, mask=None, keypoints=None, bounding_boxes=None, force=False):
if force or self.should_run():
result = self.apply_augmentation(image)
result = self.apply_augmentation(image, mask, keypoints, bounding_boxes)
return result
27 changes: 25 additions & 2 deletions augraphy/augmentations/bindingsandfasteners.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ def retrieve_foreground(self):
self.foreground = cv2.imread(foreground_path)

# Applies the Augmentation to input data.
def __call__(self, image, layer=None, force=False):
def __call__(self, image, layer=None, mask=None, keypoints=None, bounding_boxes=None, force=False):
if force or self.should_run():

# reset foreground when the same class instance called twice
Expand All @@ -786,6 +786,13 @@ def __call__(self, image, layer=None, force=False):
image = image.copy()
ysize, xsize = image.shape[:2]

# check for alpha layer
has_alpha = 0
if len(image.shape) > 2:
if image.shape[2] == 4:
has_alpha = 1
image, image_alpha = image[:, :, :3], image[:, :, 3]

# generate randomized overlay types
if self.overlay_types == "random":
overlay_types = random.choice(
Expand Down Expand Up @@ -888,4 +895,20 @@ def __call__(self, image, layer=None, force=False):

image_output = ob.build_overlay()

return image_output
if has_alpha:
ysize, xsize = image_output.shape[:2]
if ysize != image_alpha.shape[0] or xsize != image_alpha.shape[1]:
image_alpha = cv2.resize(image_alpha, (xsize, ysize), interpolation=cv2.INTER_AREA)
image_output = np.dstack((image_output, image_alpha))

# check for additional output of mask, keypoints and bounding boxes
outputs_extra = []
if mask is not None or keypoints is not None or bounding_boxes is not None:
outputs_extra = [mask, keypoints, bounding_boxes]

# returns additional mask, keypoints and bounding boxes if there is additional input
if outputs_extra:
# returns in the format of [image, mask, keypoints, bounding_boxes]
return [image_output] + outputs_extra
else:
return image_output
24 changes: 22 additions & 2 deletions augraphy/augmentations/bleedthrough.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,17 @@ def create_bleedthrough_foreground(self, image: np.ndarray):
return image_bleedthrough_foreground

# Applies the Augmentation to input data.
def __call__(self, image, layer=None, force=False):
def __call__(self, image, layer=None, mask=None, keypoints=None, bounding_boxes=None, force=False):
if force or self.should_run():
image = image.copy()

# check for alpha layer
has_alpha = 0
if len(image.shape) > 2:
if image.shape[2] == 4:
has_alpha = 1
image, image_alpha = image[:, :, :3], image[:, :, 3]

image_bleedthrough_foreground = self.create_bleedthrough_foreground(image)

image_bleed = self.generate_bleeding_ink(
Expand All @@ -185,4 +192,17 @@ def __call__(self, image, layer=None, force=False):
image_bleed_offset = self.generate_offset(image_bleed, self.offsets)
image_bleedthrough = self.blend(image, image_bleed_offset, self.alpha)

return image_bleedthrough
if has_alpha:
image_bleedthrough = np.dstack((image_bleedthrough, image_alpha))

# check for additional output of mask, keypoints and bounding boxes
outputs_extra = []
if mask is not None or keypoints is not None or bounding_boxes is not None:
outputs_extra = [mask, keypoints, bounding_boxes]

# returns additional mask, keypoints and bounding boxes if there is additional input
if outputs_extra:
# returns in the format of [image, mask, keypoints, bounding_boxes]
return [image_bleedthrough] + outputs_extra
else:
return image_bleedthrough
Loading

0 comments on commit 1d38764

Please sign in to comment.