def get_born_digital_recognizer_dataset(split='train', cache_dir=None): """Get a list of (filepath, box, word) tuples from the BornDigital dataset. This dataset comes pre-cropped so `box` is always `None`. Args: split: Which split to get (train, test, or traintest) cache_dir: The directory in which to cache the file. The default is `~/.keras-ocr`. Returns: A recognition dataset as a list of (filepath, box, word) tuples """ data = [] if cache_dir is None: cache_dir = os.path.expanduser(os.path.join('~', '.keras-ocr')) main_dir = os.path.join(cache_dir, 'borndigital') assert split in ['train', 'traintest', 'test'], f'Unsupported split: {split}' if split in ['train', 'traintest']: train_dir = os.path.join(main_dir, 'train') training_zip_path = tools.download_and_verify( url= 'https://www.mediafire.com/file/ybj0uo196rushhn/Challenge1_Training_Task3_Images_GT.zip/file', # pylint: disable=line-too-long filename='Challenge1_Training_Task3_Images_GT.zip', cache_dir=main_dir, sha256= '8ede0639f5a8031d584afd98cee893d1c5275d7f17863afc2cba24b13c932b07') if len( glob.glob(os.path.join(train_dir, '*.png')) + glob.glob(os.path.join(train_dir, '*.txt'))) != 3568: with zipfile.ZipFile(training_zip_path) as zfile: zfile.extractall(train_dir) data.extend( _read_born_digital_labels_file(labels_filepath=os.path.join( train_dir, 'gt.txt'), image_folder=train_dir)) if split in ['test', 'traintest']: test_dir = os.path.join(main_dir, 'test') test_zip_path = tools.download_and_verify( url= 'https://www.mediafire.com/file/nesckvjulvzpb2i/Challenge1_Test_Task3_Images.zip/file', filename='Challenge1_Test_Task3_Images.zip', cache_dir=main_dir, sha256= '8f781b0140fd0bac3750530f0924bce5db3341fd314a2fcbe9e0b6ca409a77f0') if len(glob.glob(os.path.join(test_dir, '*.png'))) != 1439: with zipfile.ZipFile(test_zip_path) as zfile: zfile.extractall(test_dir) test_gt_path = tools.download_and_verify( url= 'https://www.mediafire.com/file/euuuwsgg7z4pcb2/Challenge1_Test_Task3_GT.txt/file', cache_dir=test_dir, filename='Challenge1_Test_Task3_GT.txt', sha256= 'fce7f1228b7c4c26a59f13f562085148acf063d6690ce51afc395e0a1aabf8be') data.extend( _read_born_digital_labels_file(labels_filepath=test_gt_path, image_folder=test_dir)) return data
def __init__(self, alphabet=None, weights='kurapan', build_params=None): assert alphabet or weights, 'At least one of alphabet or weights must be provided.' if weights is not None: build_params = PRETRAINED_WEIGHTS[weights]['build_params'] alphabet = alphabet or PRETRAINED_WEIGHTS[weights]['alphabet'] else: build_params = DEFAULT_BUILD_PARAMS if alphabet is None: alphabet = DEFAULT_ALPHABET self.alphabet = alphabet self.blank_label_idx = len(alphabet) self.backbone, self.model, self.training_model, self.prediction_model = build_model( alphabet=alphabet, **build_params) if weights is not None: weights_dict = PRETRAINED_WEIGHTS[weights] if alphabet == weights_dict['alphabet']: self.model.load_weights( tools.download_and_verify(url=weights_dict['weights']['top']['url'], filename=weights_dict['weights']['top']['filename'], sha256=weights_dict['weights']['top']['sha256'])) else: print('Provided alphabet does not match pretrained alphabet. ' 'Using backbone weights only.') self.backbone.load_weights( tools.download_and_verify(url=weights_dict['weights']['notop']['url'], filename=weights_dict['weights']['notop']['filename'], sha256=weights_dict['weights']['notop']['sha256']))
def get_icdar_2019_semisupervised_dataset(cache_dir=None): """EXPERIMENTAL. Get a semisupervised labeled version of the ICDAR 2019 dataset. Only images with Latin-only scripts are available at this time. Args: cache_dir: The cache directory to use. """ if cache_dir is None: cache_dir = tools.get_default_cache_dir() main_dir = os.path.join(cache_dir, 'icdar2019') training_dir_1 = os.path.join(main_dir, 'ImagesPart1') training_dir_2 = os.path.join(main_dir, 'ImagesPart2') if len(glob.glob(os.path.join(training_dir_1, '*'))) != 5000: training_zip_1 = tools.download_and_verify( url='https://www.mediafire.com/file/snekaezeextc3ee/ImagesPart1.zip/file', # pylint: disable=line-too-long cache_dir=main_dir, filename='ImagesPart1.zip', sha256='1968894ef93b97f3ef4c97880b6dce85b1851f4d778e253f4e7265b152a4986f') with zipfile.ZipFile(training_zip_1) as zfile: zfile.extractall(main_dir) if len(glob.glob(os.path.join(training_dir_2, '*'))) != 5000: training_zip_2 = tools.download_and_verify( url='https://www.mediafire.com/file/i2snljkfm4t2ojm/ImagesPart2.zip/file', # pylint: disable=line-too-long cache_dir=main_dir, filename='ImagesPart2.zip', sha256='5651b9137e877f731bfebb2a8b75042e26baa389d2fb1cfdbb9e3da343757241') with zipfile.ZipFile(training_zip_2) as zfile: zfile.extractall(main_dir) ground_truth = tools.download_and_verify( url='http://www.mediafire.com/file/jshjv9kntxjzhva/mlt2019_dataset.json/file', # pylint: disable=line-too-long cache_dir=main_dir, filename='mlt2019_dataset.json', sha256='179452117a6a4afe519fa2f90ee7c2cddeb18e35c1df3036ae231cd280057684') with open(ground_truth, 'r') as f: character_level_dataset = json.loads(f.read())['dataset'] for gif_filepath in glob.glob(os.path.join(main_dir, '**', '*.gif')): # We need to do this because we cannot easily read GIFs. PIL.Image.open(gif_filepath).convert('RGB').save(os.path.splitext(gif_filepath)[0] + '.jpg') os.remove(gif_filepath) return [(os.path.join(main_dir, entry['filepath']), [[(np.array(box).clip(0, np.inf), None) for box in line['line']] for line in entry['lines'] if line['line']], entry['percent_complete']) for entry in character_level_dataset if entry['percent_complete'] > 0.5]
def __init__(self, weights='clovaai_general', load_from_torch=False, optimizer='adam', backbone_name='vgg'): if weights is not None: pretrained_key = (weights, load_from_torch) assert backbone_name == 'vgg', 'Pretrained weights available only for VGG.' assert pretrained_key in PRETRAINED_WEIGHTS, \ 'Selected weights configuration not found.' weights_config = PRETRAINED_WEIGHTS[pretrained_key] weights_path = tools.download_and_verify(url=weights_config['url'], filename=weights_config['filename'], sha256=weights_config['sha256']) else: weights_path = None self.model = build_keras_model(weights_path=weights_path, backbone_name=backbone_name) self.model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(lr=0.0001))
def get_cocotext_recognizer_dataset(split='train', cache_dir=None, limit=None, legible_only=False, english_only=False, return_raw_labels=False): """Get a list of (filepath, box, word) tuples from the COCO-Text dataset. Args: split: Which split to get (train, val, or trainval) limit: Limit the number of files included in the download cache_dir: The directory in which to cache the file. The default is `~/.keras-ocr`. return_raw_labels: Whether to return the raw labels object Returns: A recognition dataset as a list of (filepath, box, word) tuples. If return_raw_labels is True, you will also get a (labels, images_dir) tuple containing the raw COCO data and the directory in which you can find the images. """ assert split in ['train', 'val', 'trainval'], f'Unsupported split: {split}' if cache_dir is None: cache_dir = os.path.expanduser(os.path.join('~', '.keras-ocr')) main_dir = os.path.join(cache_dir, 'coco-text') images_dir = os.path.join(main_dir, 'images') labels_zip = tools.download_and_verify( url= 'https://github.com/bgshih/cocotext/releases/download/dl/cocotext.v2.zip', cache_dir=main_dir, sha256= '1444893ce7dbcd8419b2ec9be6beb0dba9cf8a43bf36cab4293d5ba6cecb7fb1') with zipfile.ZipFile(labels_zip) as z: with z.open('cocotext.v2.json') as f: labels = json.loads(f.read()) selected_ids = [ cocoid for cocoid, data in labels['imgs'].items() if data['set'] in split ] if limit: flatten = lambda l: [item for sublist in l for item in sublist] selected_ids = selected_ids[:limit] labels['imgToAnns'] = { k: v for k, v in labels['imgToAnns'].items() if k in selected_ids } labels['imgs'] = { k: v for k, v in labels['imgs'].items() if k in selected_ids } anns = set(flatten(list(labels.values()))) labels['anns'] = {k: v for k, v in labels['anns'].items() if k in anns} selected_filenames = [ labels['imgs'][cocoid]['file_name'] for cocoid in selected_ids ] with concurrent.futures.ThreadPoolExecutor() as executor: for future in tqdm.tqdm(concurrent.futures.as_completed([ executor.submit( tools.download_and_verify, url=f'http://images.cocodataset.org/train2014/{filename}', cache_dir=images_dir, verbose=False) for filename in selected_filenames ]), total=len(selected_filenames), desc='Downloading images'): _ = future.result() dataset = [] for selected_id in selected_ids: filepath = os.path.join( images_dir, selected_filenames[selected_ids.index(selected_id)]) for annIdx in labels['imgToAnns'][selected_id]: ann = labels['anns'][str(annIdx)] if english_only and ann['language'] != 'english': continue if legible_only and ann['legibility'] != 'legible': continue dataset.append((filepath, np.array(ann['mask']).reshape(-1, 2), ann['utf8_string'])) if return_raw_labels: return dataset, (labels, images_dir) return dataset
def get_icdar_2013_detector_dataset(cache_dir=None, skip_illegible=False): """Get the ICDAR 2013 text segmentation dataset for detector training. Only the training set has the necessary annotations. For the test set, only segmentation maps are provided, which do not provide the necessary information for affinity scores. Args: cache_dir: The directory in which to store the data. skip_illegible: Whether to skip illegible characters. Returns: Lists of (image_path, lines, confidence) tuples. Confidence is always 1 for this dataset. We record confidence to allow for future support for weakly supervised cases. """ if cache_dir is None: cache_dir = tools.get_default_cache_dir() main_dir = os.path.join(cache_dir, 'icdar2013') training_images_dir = os.path.join(main_dir, 'images') training_zip_images_path = tools.download_and_verify( url= 'https://www.mediafire.com/file/l8ct7ckudg12ln6/Challenge2_Training_Task12_Images.zip/file', # pylint: disable=line-too-long cache_dir=main_dir, filename='images.zip', sha256='7a57d1699fbb92db3ad82c930202938562edaf72e1c422ddd923860d8ace8ded') if len(glob.glob(os.path.join(training_images_dir, '*.jpg'))) != 229: with zipfile.ZipFile(training_zip_images_path) as zfile: zfile.extractall(training_images_dir) training_gt_dir = os.path.join(main_dir, 'loc_gt') training_zip_gt_path = tools.download_and_verify( url='https://www.mediafire.com/file/rpfphmxvudn5v3y/Challenge2_Training_Task2_GT.zip/file', # pylint: disable=line-too-long cache_dir=main_dir, filename='loc_gt.zip', sha256='4cedd5b1e33dc4354058f5967221ac85dbdf91a99b30f3ab1ecdf42786a9d027') if len(glob.glob(os.path.join(training_gt_dir, '*.txt'))) != 229: with zipfile.ZipFile(training_zip_gt_path) as zfile: zfile.extractall(training_gt_dir) dataset = [] for gt_filepath in glob.glob(os.path.join(training_gt_dir, '*.txt')): image_id = os.path.split(gt_filepath)[1].split('_')[0] image_path = os.path.join(training_images_dir, image_id + '.jpg') lines = [] with open(gt_filepath, 'r') as f: current_line = [] for row in f.read().split('\n'): if row == '': lines.append(current_line) current_line = [] else: row = row.split(' ')[5:] character = row[-1][1:-1] if character == '' and skip_illegible: continue x1, y1, x2, y2 = map(int, row[:4]) current_line.append((np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]]), character)) # Some lines only have illegible characters and if skip_illegible is True, # then these lines will be blank. lines = [line for line in lines if line] dataset.append((image_path, lines, 1)) return dataset