Update chess_ground-targz.py
Browse files- chess_ground-targz.py +36 -29
chess_ground-targz.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
import json
|
2 |
-
import datasets
|
3 |
import os
|
|
|
4 |
import tarfile
|
|
|
5 |
|
6 |
# Description of the dataset
|
7 |
_DESCRIPTION = """\
|
8 |
-
Dataset for extracting notations from chess scoresheets.
|
9 |
"""
|
10 |
|
11 |
# BibTeX citation for the dataset
|
@@ -22,10 +22,10 @@ _LICENSE = "Creative Commons Attribution 3.0"
|
|
22 |
|
23 |
|
24 |
class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
25 |
-
"""Dataset for linking chess
|
26 |
|
27 |
def _info(self):
|
28 |
-
# Define the features of your dataset (
|
29 |
features = datasets.Features(
|
30 |
{
|
31 |
"image": datasets.Image(), # Image feature for chess scoresheets
|
@@ -45,9 +45,9 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
|
45 |
|
46 |
def _split_generators(self, dl_manager):
|
47 |
"""Define the splits of the dataset."""
|
48 |
-
|
49 |
-
# Load the image dataset
|
50 |
-
image_dataset_url = "https://huggingface.co/datasets/Chesscorner/
|
51 |
extracted_image_path = dl_manager.download_and_extract(image_dataset_url)
|
52 |
|
53 |
# Load the text dataset (ground truths)
|
@@ -58,54 +58,61 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
|
58 |
datasets.SplitGenerator(
|
59 |
name=datasets.Split.TRAIN,
|
60 |
gen_kwargs={
|
61 |
-
"image_path": extracted_image_path,
|
62 |
-
"text_filepath": text_filepath,
|
63 |
},
|
64 |
),
|
65 |
]
|
66 |
|
67 |
def _generate_examples(self, image_path, text_filepath):
|
68 |
-
"""Generate examples linking images
|
69 |
-
|
70 |
idx = 0
|
71 |
-
|
72 |
-
image_mapping = self._extract_images_from_tar(image_path)
|
73 |
|
74 |
-
#
|
|
|
|
|
|
|
75 |
with open(text_filepath, encoding="utf-8") as fp:
|
76 |
for line in fp:
|
77 |
obj = json.loads(line)
|
78 |
text = obj["text"]
|
79 |
|
80 |
-
# Extract
|
81 |
-
text_id = text[:5] # Adjust this
|
82 |
-
|
83 |
-
# Find the corresponding image
|
84 |
-
image_file = os.path.join(f"{text_id}.png", image_mapping)
|
85 |
|
86 |
-
#
|
87 |
-
|
|
|
|
|
|
|
88 |
yield idx, {
|
89 |
"image": image_file,
|
90 |
"text": text,
|
91 |
}
|
92 |
else:
|
93 |
-
print(f"
|
94 |
|
95 |
idx += 1
|
96 |
|
97 |
def _extract_images_from_tar(self, tar_path):
|
98 |
-
"""Extracts the images from the tar archive and returns a mapping of image filenames to file paths."""
|
99 |
|
100 |
image_mapping = {}
|
101 |
-
|
102 |
-
|
|
|
|
|
103 |
|
104 |
-
#
|
105 |
for member in tar.getmembers():
|
106 |
-
if member.isfile(): # Only include actual files
|
107 |
image_filename = os.path.basename(member.name)
|
108 |
-
|
|
|
|
|
|
|
|
|
109 |
|
110 |
return image_mapping
|
|
|
111 |
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import json
|
3 |
import tarfile
|
4 |
+
import datasets
|
5 |
|
6 |
# Description of the dataset
|
7 |
_DESCRIPTION = """\
|
8 |
+
Dataset for extracting notations from chess scoresheets, integrating both image and text data.
|
9 |
"""
|
10 |
|
11 |
# BibTeX citation for the dataset
|
|
|
22 |
|
23 |
|
24 |
class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
25 |
+
"""Dataset for linking chess scoresheet images with ground truth text."""
|
26 |
|
27 |
def _info(self):
|
28 |
+
# Define the features of your dataset (images + text)
|
29 |
features = datasets.Features(
|
30 |
{
|
31 |
"image": datasets.Image(), # Image feature for chess scoresheets
|
|
|
45 |
|
46 |
def _split_generators(self, dl_manager):
|
47 |
"""Define the splits of the dataset."""
|
48 |
+
|
49 |
+
# Load the image dataset (tar file)
|
50 |
+
image_dataset_url = "https://huggingface.co/datasets/Chesscorner/chess-images/resolve/main/images.tar.gz"
|
51 |
extracted_image_path = dl_manager.download_and_extract(image_dataset_url)
|
52 |
|
53 |
# Load the text dataset (ground truths)
|
|
|
58 |
datasets.SplitGenerator(
|
59 |
name=datasets.Split.TRAIN,
|
60 |
gen_kwargs={
|
61 |
+
"image_path": extracted_image_path, # Path to extracted tar directory
|
62 |
+
"text_filepath": text_filepath, # Path to the text dataset
|
63 |
},
|
64 |
),
|
65 |
]
|
66 |
|
67 |
def _generate_examples(self, image_path, text_filepath):
|
68 |
+
"""Generate examples by linking images and text."""
|
|
|
69 |
idx = 0
|
|
|
|
|
70 |
|
71 |
+
# Extract and map text IDs to their corresponding images
|
72 |
+
image_mapping = self._extract_images_from_tar(image_path)
|
73 |
+
|
74 |
+
# Load the text dataset (ground truths) from the JSONL file
|
75 |
with open(text_filepath, encoding="utf-8") as fp:
|
76 |
for line in fp:
|
77 |
obj = json.loads(line)
|
78 |
text = obj["text"]
|
79 |
|
80 |
+
# Extract the text ID (assuming text ID matches image filename)
|
81 |
+
text_id = text[:5] # Adjust this based on the actual pattern of text IDs
|
|
|
|
|
|
|
82 |
|
83 |
+
# Find the corresponding image file
|
84 |
+
image_file = image_mapping.get(f"{text_id}.png") # Adjust file extension if necessary
|
85 |
+
|
86 |
+
# Ensure the image exists and yield the example
|
87 |
+
if image_file:
|
88 |
yield idx, {
|
89 |
"image": image_file,
|
90 |
"text": text,
|
91 |
}
|
92 |
else:
|
93 |
+
print(f"Image not found for ID: {text_id}")
|
94 |
|
95 |
idx += 1
|
96 |
|
97 |
def _extract_images_from_tar(self, tar_path):
|
98 |
+
"""Extracts the images from the tar.gz archive and returns a mapping of image filenames to file paths."""
|
99 |
|
100 |
image_mapping = {}
|
101 |
+
|
102 |
+
# Ensure extraction of individual files, not directories
|
103 |
+
with tarfile.open(tar_path) as tar:
|
104 |
+
tar.extractall(path="extracted_images") # Extract images to a specific directory
|
105 |
|
106 |
+
# Iterate over the extracted files and map them
|
107 |
for member in tar.getmembers():
|
108 |
+
if member.isfile(): # Only include actual files, not directories
|
109 |
image_filename = os.path.basename(member.name)
|
110 |
+
image_path = os.path.join("extracted_images", member.name)
|
111 |
+
|
112 |
+
# Check if the path points to a valid image file
|
113 |
+
if os.path.isfile(image_path):
|
114 |
+
image_mapping[image_filename] = image_path
|
115 |
|
116 |
return image_mapping
|
117 |
+
|
118 |
|