Skip to content

Commit

Permalink
Shared im_mode setting in GroundTruthDataset/PolygonGTDataset
Browse files Browse the repository at this point in the history
  • Loading branch information
mittagessen committed Apr 8, 2024
1 parent 46be12c commit 5524d88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
37 changes: 26 additions & 11 deletions kraken/lib/dataset/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
Tuple, Union)

from PIL import Image
from torch.utils.data import Dataset
from ctypes import c_char
from torchvision import transforms
from torch.utils.data import Dataset

from kraken.containers import BaselineLine, BBoxLine, Segmentation
from kraken.lib import functional_im_transforms as F_t
Expand Down Expand Up @@ -333,7 +334,7 @@ def __init__(self,
if augmentation:
self.aug = DefaultAugmenter()

self.im_mode = mp.Value(c_char, b'1')
self._im_mode = mp.Value(c_char, b'1')

def add(self,
line: Optional[BaselineLine] = None,
Expand Down Expand Up @@ -445,10 +446,10 @@ def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
if is_bitonal(im):
im_mode = b'1'

with self.im_mode.get_lock():
if im_mode > self.im_mode:
logger.info(f'Upgrading "im_mode" from {self.im_mode} to {im_mode}')
self.im_mode = im_mode
with self._im_mode.get_lock():
if im_mode > self._im_mode.value:
logger.info(f'Upgrading "im_mode" from {self._im_mode.value} to {im_mode}')
self._im_mode.value = im_mode
if self.aug:
im = im.permute((1, 2, 0)).numpy()
o = self.aug(image=im)
Expand All @@ -464,6 +465,12 @@ def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
def __len__(self) -> int:
return len(self._images)

@property
def im_mode(self):
return {b'1': '1',
b'L': 'L',
b'R': 'RGB'}[self._im_mode.value]


class GroundTruthDataset(Dataset):
"""
Expand Down Expand Up @@ -523,7 +530,7 @@ def __init__(self,
if augmentation:
self.aug = DefaultAugmenter()

self.im_mode = mp.Value(c_char, b'1')
self._im_mode = mp.Value(c_char, b'1')

def add(self,
line: Optional[BBoxLine] = None,
Expand Down Expand Up @@ -624,10 +631,10 @@ def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
im_mode = b'L'
if is_bitonal(im):
im_mode = b'1'
with self.im_mode.get_lock():
if im_mode > self.im_mode:
logger.info(f'Upgrading "im_mode" from {self.im_mode} to {im_mode}')
self.im_mode = im_mode
with self._im_mode.get_lock():
if im_mode > self._im_mode.value:
logger.info(f'Upgrading "im_mode" from {self._im_mode.value} to {im_mode}')
self._im_mode.value = im_mode
if self.aug:
im = im.permute((1, 2, 0)).numpy()
o = self.aug(image=im)
Expand All @@ -643,3 +650,11 @@ def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:

def __len__(self) -> int:
return len(self._images)

@property
def im_mode(self):
return {b'1': '1',
b'L': 'L',
b'R': 'RGB'}[self._im_mode.value]


2 changes: 1 addition & 1 deletion kraken/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def is_bitonal(im: Union[Image.Image, torch.Tensor]) -> bool:
if isinstance(im, Image.Image):
return im.getcolors(2) is not None and len(im.getcolors(2)) == 2
elif isinstance(im, torch.Tensor):
return len(im.int().unique()) == 2
return len(im.unique()) == 2


def get_im_str(im: Image.Image) -> str:
Expand Down

0 comments on commit 5524d88

Please sign in to comment.