Chesscorner commited on
Commit
8d095fd
·
verified ·
1 Parent(s): 59ea6c7

Update chess_ground-targz.py

Browse files
Files changed (1) hide show
  1. chess_ground-targz.py +45 -20
chess_ground-targz.py CHANGED
@@ -1,10 +1,11 @@
 
1
  import json
 
2
  import datasets
3
- import os
4
 
5
  # Description of the dataset
6
  _DESCRIPTION = """\
7
- Dataset for extracting notations from chess scoresheets.
8
  """
9
 
10
  # BibTeX citation for the dataset
@@ -21,10 +22,10 @@ _LICENSE = "Creative Commons Attribution 3.0"
21
 
22
 
23
  class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
24
- """Dataset for linking chess scoresheets images and their ground truth text."""
25
 
26
  def _info(self):
27
- # Define the features of your dataset (image and text)
28
  features = datasets.Features(
29
  {
30
  "image": datasets.Image(), # Image feature for chess scoresheets
@@ -44,8 +45,8 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
44
 
45
  def _split_generators(self, dl_manager):
46
  """Define the splits of the dataset."""
47
-
48
- # Load the image dataset
49
  image_dataset_url = "https://huggingface.co/datasets/Chesscorner/chess-images/resolve/main/flat_images.tar.gz"
50
  extracted_image_path = dl_manager.download_and_extract(image_dataset_url)
51
 
@@ -57,36 +58,60 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
57
  datasets.SplitGenerator(
58
  name=datasets.Split.TRAIN,
59
  gen_kwargs={
60
- "image_path": extracted_image_path,
61
- "text_filepath": text_filepath,
62
  },
63
  ),
64
  ]
65
 
66
  def _generate_examples(self, image_path, text_filepath):
67
- """Generate examples linking images to text."""
68
-
69
  idx = 0
70
- # Load the text dataset
 
 
 
 
71
  with open(text_filepath, encoding="utf-8") as fp:
72
  for line in fp:
73
  obj = json.loads(line)
74
  text = obj["text"]
75
 
76
- # Extract image ID from the text (assuming the first 5 characters of text match image filename)
77
- text_id = text[:5] # Adjust this logic based on the exact pattern you have
78
-
79
- # Find the corresponding image
80
- image_file = os.path.join(f"{text_id}.png", image_path)
81
 
82
- # Check if the image file exists (you can add extra checks here)
83
- if os.path.exists(image_file):
 
 
 
84
  yield idx, {
85
  "image": image_file,
86
  "text": text,
87
  }
88
  else:
89
- print(f"Text not found for ID: {image_path}")
90
 
91
  idx += 1
92
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.gz file)
50
  image_dataset_url = "https://huggingface.co/datasets/Chesscorner/chess-images/resolve/main/flat_images.tar.gz"
51
  extracted_image_path = dl_manager.download_and_extract(image_dataset_url)
52
 
 
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, "r:gz") 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
+