Add files using upload-large-folder tool
Browse files
OBIMD.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from datasets import DatasetInfo, BuilderConfig, GeneratorBasedBuilder, SplitGenerator, \
|
| 4 |
+
Split, Features, Value, Sequence, Image
|
| 5 |
+
import zipfile
|
| 6 |
+
|
| 7 |
+
class OBIMDConfig(BuilderConfig):
|
| 8 |
+
def __init__(self, **kwargs):
|
| 9 |
+
super().__init__(version="1.0.0", **kwargs)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class OBIMDDataset(GeneratorBasedBuilder):
|
| 13 |
+
BUILDER_CONFIGS = [
|
| 14 |
+
OBIMDConfig(name="default", description="Oracle bone OBIMD dataset."),
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
def _info(self):
|
| 18 |
+
return DatasetInfo(
|
| 19 |
+
description="Paired images of oracle bone rubbings and facsimiles with annotations.",
|
| 20 |
+
features=Features({
|
| 21 |
+
"Facsimile": Image(),
|
| 22 |
+
"Rubbing": Image(),
|
| 23 |
+
"RubbingName": Value("string"),
|
| 24 |
+
"RecordUtilSentenceGroupVoList": Sequence({
|
| 25 |
+
"GroupCategory": Value("string"),
|
| 26 |
+
"RecordUtilOracleCharVoList": Sequence({
|
| 27 |
+
"Position": Value("string"),
|
| 28 |
+
"OrderNumber": Value("int32"),
|
| 29 |
+
"SeatFont": Value("int32"),
|
| 30 |
+
"Mark": Value("int32"),
|
| 31 |
+
"Label": Value("string"),
|
| 32 |
+
"SubLabel": Value("string"),
|
| 33 |
+
}),
|
| 34 |
+
}),
|
| 35 |
+
}),
|
| 36 |
+
supervised_keys=None,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
def _split_generators(self, dl_manager):
|
| 40 |
+
if os.path.exists(self.base_path):
|
| 41 |
+
data_dir = self.base_path
|
| 42 |
+
else:
|
| 43 |
+
data_dir = dl_manager.download_and_extract(self.config.data_dir or ".")
|
| 44 |
+
|
| 45 |
+
return [
|
| 46 |
+
SplitGenerator(
|
| 47 |
+
name=Split.TRAIN,
|
| 48 |
+
gen_kwargs={
|
| 49 |
+
"filepath": os.path.join(data_dir, "data.json"),
|
| 50 |
+
"base_path": data_dir,
|
| 51 |
+
},
|
| 52 |
+
),
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
def _generate_examples(self, filepath, base_path):
|
| 56 |
+
with open(filepath, encoding="utf-8") as f:
|
| 57 |
+
data = json.load(f)
|
| 58 |
+
with zipfile.ZipFile(os.path.join(base_path, "rubbing.zip"), 'r') as rubbing_ref:
|
| 59 |
+
with zipfile.ZipFile(os.path.join(base_path, "facsimile.zip"), 'r') as facsimile_ref:
|
| 60 |
+
for idx, item in enumerate(data):
|
| 61 |
+
|
| 62 |
+
with rubbing_ref.open(item["Rubbing"]) as file:
|
| 63 |
+
rubbing_content = file.read()
|
| 64 |
+
with facsimile_ref.open(item["Facsimile"]) as file:
|
| 65 |
+
facsimile_content = file.read()
|
| 66 |
+
|
| 67 |
+
yield idx, {
|
| 68 |
+
"Facsimile": facsimile_content,
|
| 69 |
+
"Rubbing": rubbing_content,
|
| 70 |
+
"RubbingName": item["RubbingName"],
|
| 71 |
+
"RecordUtilSentenceGroupVoList": [
|
| 72 |
+
{
|
| 73 |
+
"GroupCategory": group["GroupCategory"],
|
| 74 |
+
"RecordUtilOracleCharVoList": [
|
| 75 |
+
{
|
| 76 |
+
"Position": char["Position"],
|
| 77 |
+
"OrderNumber": char["OrderNumber"],
|
| 78 |
+
"SeatFont": char["SeatFont"],
|
| 79 |
+
"Mark": char["Mark"],
|
| 80 |
+
"Label": char["Label"],
|
| 81 |
+
"SubLabel": char["SubLabel"],
|
| 82 |
+
}
|
| 83 |
+
for char in group["RecordUtilOracleCharVoList"]
|
| 84 |
+
],
|
| 85 |
+
}
|
| 86 |
+
for group in item["RecordUtilSentenceGroupVoList"]
|
| 87 |
+
],
|
| 88 |
+
}
|