# 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.importjsonimportosfrompathlibimportPathfromtypingimportAnyfrom.datasetsimportAbstractDataset__all__=["RecognitionDataset"]
[docs]classRecognitionDataset(AbstractDataset):"""Dataset implementation for text recognition tasks >>> from doctr.datasets import RecognitionDataset >>> train_set = RecognitionDataset(img_folder="/path/to/images", >>> labels_path="/path/to/labels.json") >>> img, target = train_set[0] Args: img_folder: path to the images folder labels_path: path to the json file containing all labels (character sequences) **kwargs: keyword arguments from `AbstractDataset`. """def__init__(self,img_folder:str,labels_path:str,**kwargs:Any,)->None:super().__init__(img_folder,**kwargs)self.data:list[tuple[str,str]]=[]withopen(labels_path,encoding="utf-8")asf:labels=json.load(f)forimg_name,labelinlabels.items():ifnotos.path.exists(os.path.join(self.root,img_name)):raiseFileNotFoundError(f"unable to locate {os.path.join(self.root,img_name)}")self.data.append((img_name,label))defmerge_dataset(self,ds:AbstractDataset)->None:# Update data with new root for selfself.data=[(str(Path(self.root).joinpath(img_path)),label)forimg_path,labelinself.data]# Define new rootself.root=Path("/")# Merge with ds dataforimg_path,labelinds.data:self.data.append((str(Path(ds.root).joinpath(img_path)),label))