File size: 3,782 Bytes
b25a2a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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"]
],
}
|