| import json | |
| import os | |
| from datasets import DatasetInfo, BuilderConfig, GeneratorBasedBuilder, SplitGenerator, \ | |
| Split, Features, Value, Sequence, Image | |
| import zipfile | |
| class OBIMDConfig(BuilderConfig): | |
| def __init__(self, **kwargs): | |
| super().__init__(version="1.0.0", **kwargs) | |
| class OBIMDDataset(GeneratorBasedBuilder): | |
| BUILDER_CONFIGS = [ | |
| OBIMDConfig(name="default", description="Oracle bone OBIMD dataset."), | |
| ] | |
| def _info(self): | |
| return DatasetInfo( | |
| description="Paired images of oracle bone rubbings and facsimiles with annotations.", | |
| features=Features({ | |
| "Facsimile": Image(), | |
| "Rubbing": Image(), | |
| "RubbingName": Value("string"), | |
| "RecordUtilSentenceGroupVoList": Sequence({ | |
| "GroupCategory": Value("string"), | |
| "RecordUtilOracleCharVoList": Sequence({ | |
| "Position": Value("string"), | |
| "OrderNumber": Value("int32"), | |
| "SeatFont": Value("int32"), | |
| "Mark": Value("int32"), | |
| "Label": Value("string"), | |
| "SubLabel": Value("string"), | |
| }), | |
| }), | |
| }), | |
| supervised_keys=None, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| if os.path.exists(self.base_path): | |
| data_dir = self.base_path | |
| else: | |
| data_dir = dl_manager.download_and_extract(self.config.data_dir or ".") | |
| return [ | |
| SplitGenerator( | |
| name=Split.TRAIN, | |
| gen_kwargs={ | |
| "filepath": os.path.join(data_dir, "data.json"), | |
| "base_path": data_dir, | |
| }, | |
| ), | |
| ] | |
| def _generate_examples(self, filepath, base_path): | |
| with open(filepath, encoding="utf-8") as f: | |
| data = json.load(f) | |
| with zipfile.ZipFile(os.path.join(base_path, "rubbing.zip"), 'r') as rubbing_ref: | |
| with zipfile.ZipFile(os.path.join(base_path, "facsimile.zip"), 'r') as facsimile_ref: | |
| for idx, item in enumerate(data): | |
| with rubbing_ref.open(item["Rubbing"]) as file: | |
| rubbing_content = file.read() | |
| with facsimile_ref.open(item["Facsimile"]) as file: | |
| facsimile_content = file.read() | |
| yield idx, { | |
| "Facsimile": facsimile_content, | |
| "Rubbing": rubbing_content, | |
| "RubbingName": item["RubbingName"], | |
| "RecordUtilSentenceGroupVoList": [ | |
| { | |
| "GroupCategory": group["GroupCategory"], | |
| "RecordUtilOracleCharVoList": [ | |
| { | |
| "Position": char["Position"], | |
| "OrderNumber": char["OrderNumber"], | |
| "SeatFont": char["SeatFont"], | |
| "Mark": char["Mark"], | |
| "Label": char["Label"], | |
| "SubLabel": char["SubLabel"], | |
| } | |
| for char in group["RecordUtilOracleCharVoList"] | |
| ], | |
| } | |
| for group in item["RecordUtilSentenceGroupVoList"] | |
| ], | |
| } | |