Chesscorner commited on
Commit
e203fb8
·
verified ·
1 Parent(s): 4fad638

Update chess_ground-targz.py

Browse files
Files changed (1) hide show
  1. chess_ground-targz.py +44 -17
chess_ground-targz.py CHANGED
@@ -3,7 +3,7 @@ import datasets
3
 
4
  # Description of the dataset
5
  _DESCRIPTION = """\
6
- Dataset for extracting notations from chess-scoresheets.
7
  """
8
 
9
  # BibTeX citation for the dataset
@@ -18,45 +18,72 @@ year={2020}
18
  # License of the dataset
19
  _LICENSE = "Creative Commons Attribution 3.0"
20
 
21
- # Class for loading the chess ground truth dataset
22
- class ChessGroundTargz(datasets.GeneratorBasedBuilder):
23
- """Chess Moves ground truths for chess scoresheets"""
24
-
25
  def _info(self):
26
- # Define the features of your dataset (i.e., the columns)
27
  features = datasets.Features(
28
  {
29
- "text": datasets.Value("string"),
 
30
  }
31
  )
32
  return datasets.DatasetInfo(
33
  description=_DESCRIPTION,
34
  features=features,
35
- homepage="Chesscorner/jsonl-chess-dataset", # Update with the correct homepage link
36
  license=_LICENSE,
37
  citation=_CITATION,
38
  )
39
 
40
  def _split_generators(self, dl_manager):
41
  """Define the splits of the dataset."""
42
- # Instead of downloading, we are now referencing the dataset on Hugging Face
43
- # Hugging Face handles downloading and caching automatically
 
 
 
 
 
 
 
44
  return [
45
  datasets.SplitGenerator(
46
  name=datasets.Split.TRAIN,
47
  gen_kwargs={
48
- "filepath": dl_manager.download_and_extract("https://huggingface.co/datasets/Chesscorner/jsonl-chess-dataset/resolve/main/train.jsonl/train.jsonl"),
 
49
  },
50
  ),
51
  ]
52
 
53
- def _generate_examples(self, filepath):
 
 
54
  idx = 0
55
- # open the file and read the lines
56
- with open(filepath, encoding="utf-8") as fp:
57
  for line in fp:
58
- # load json line
59
  obj = json.loads(line)
60
- yield idx, obj
61
- idx += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
3
 
4
  # Description of the dataset
5
  _DESCRIPTION = """\
6
+ Dataset for extracting notations from chess scoresheets.
7
  """
8
 
9
  # BibTeX citation for the dataset
 
18
  # License of the dataset
19
  _LICENSE = "Creative Commons Attribution 3.0"
20
 
21
+
22
+ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
23
+ """Dataset for linking chess scoresheets images and their ground truth text."""
24
+
25
  def _info(self):
26
+ # Define the features of your dataset (image and text)
27
  features = datasets.Features(
28
  {
29
+ "image": datasets.Image(), # Image feature for chess scoresheets
30
+ "text": datasets.Value("string"), # Text feature for chess notations
31
  }
32
  )
33
  return datasets.DatasetInfo(
34
  description=_DESCRIPTION,
35
  features=features,
36
+ homepage="Chesscorner/chess-images", # Update with the correct homepage link
37
  license=_LICENSE,
38
  citation=_CITATION,
39
  )
40
 
41
  def _split_generators(self, dl_manager):
42
  """Define the splits of the dataset."""
43
+
44
+ # Load the image dataset
45
+ image_dataset_url = "https://huggingface.co/datasets/Chesscorner/chess-images/resolve/main/images.tar.gz"
46
+ extracted_image_path = dl_manager.download_and_extract(image_dataset_url)
47
+
48
+ # Load the text dataset (ground truths)
49
+ text_dataset_url = "https://huggingface.co/datasets/Chesscorner/jsonl-chess-dataset/resolve/main/train.jsonl/train.jsonl"
50
+ text_filepath = dl_manager.download_and_extract(text_dataset_url)
51
+
52
  return [
53
  datasets.SplitGenerator(
54
  name=datasets.Split.TRAIN,
55
  gen_kwargs={
56
+ "image_path": extracted_image_path,
57
+ "text_filepath": text_filepath,
58
  },
59
  ),
60
  ]
61
 
62
+ def _generate_examples(self, image_path, text_filepath):
63
+ """Generate examples linking images to text."""
64
+
65
  idx = 0
66
+ # Load the text dataset
67
+ with open(text_filepath, encoding="utf-8") as fp:
68
  for line in fp:
 
69
  obj = json.loads(line)
70
+ text = obj["text"]
71
+
72
+ # Extract image ID from the text (assuming the first 3 characters of text match image filename)
73
+ image_id = text[:3] # Adjust this logic based on the exact pattern you have
74
+
75
+ # Find the corresponding image
76
+ image_file = f"{image_path}/{image_id}.png" # Adjust file extension if needed
77
+
78
+ # Check if the image file exists (you can add extra checks here)
79
+ if os.path.exists(image_file):
80
+ yield idx, {
81
+ "image": image_file,
82
+ "text": text,
83
+ }
84
+ else:
85
+ print(f"Image not found for ID: {image_id}")
86
+
87
+ idx += 1
88
+
89