# Copyright (C) 2021-2025, Mindee.# This program is licensed under the Apache License 2.0.# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.importosfromrandomimportsamplefromtypingimportAnyfromtqdmimporttqdmfrom.datasetsimportAbstractDataset__all__=["IIITHWS"]
[docs]classIIITHWS(AbstractDataset):"""IIITHWS dataset from `"Generating Synthetic Data for Text Recognition" <https://arxiv.org/pdf/1608.04224.pdf>`_ | `"repository" <https://github.com/kris314/hwnet>`_ | `"website" <https://cvit.iiit.ac.in/research/projects/cvit-projects/matchdocimgs>`_. >>> # NOTE: This is a pure recognition dataset without bounding box labels. >>> # NOTE: You need to download the dataset. >>> from doctr.datasets import IIITHWS >>> train_set = IIITHWS(img_folder="/path/to/iiit-hws/Images_90K_Normalized", >>> label_path="/path/to/IIIT-HWS-90K.txt", >>> train=True) >>> img, target = train_set[0] >>> test_set = IIITHWS(img_folder="/path/to/iiit-hws/Images_90K_Normalized", >>> label_path="/path/to/IIIT-HWS-90K.txt") >>> train=False) >>> img, target = test_set[0] Args: img_folder: folder with all the images of the dataset label_path: path to the file with the labels train: whether the subset should be the training one **kwargs: keyword arguments from `AbstractDataset`. """def__init__(self,img_folder:str,label_path:str,train:bool=True,**kwargs:Any,)->None:super().__init__(img_folder,**kwargs)# File existence checkifnotos.path.exists(label_path)ornotos.path.exists(img_folder):raiseFileNotFoundError(f"unable to locate {label_pathifnotos.path.exists(label_path)elseimg_folder}")self.data:list[tuple[str,str]]=[]self.train=trainwithopen(label_path)asf:annotations=f.readlines()# Shuffle the dataset otherwise the test set will contain the same labels n timesannotations=sample(annotations,len(annotations))train_samples=int(len(annotations)*0.9)set_slice=slice(train_samples)ifself.trainelseslice(train_samples,None)forannotationintqdm(iterable=annotations[set_slice],desc="Preparing and Loading IIITHWS",total=len(annotations[set_slice])):img_path,label=annotation.split()[0:2]img_path=os.path.join(img_folder,img_path)self.data.append((img_path,label))defextra_repr(self)->str:returnf"train={self.train}"