Datasets:

ArXiv:
leonleyang commited on
Commit
7d88d4a
·
verified ·
1 Parent(s): 2f235e6

Create hasy-v2.py

Browse files
Files changed (1) hide show
  1. hasy-v2.py +116 -0
hasy-v2.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import datasets
4
+ from PIL import Image
5
+ import tempfile
6
+
7
+
8
+ class HASYv2(datasets.GeneratorBasedBuilder):
9
+ """
10
+ The HASYv2 dataset contains handwritten symbol images of 369 classes.
11
+ Each image is 32x32 pixels in size.
12
+ """
13
+
14
+ VERSION = datasets.Version("1.0.0")
15
+
16
+ def _info(self):
17
+ return datasets.DatasetInfo(
18
+ description="""The HASYv2 dataset contains 32x32 black-and-white images of 369 handwritten symbol classes.
19
+ It includes over 168,236 samples categorized into various classes like Latin characters, numerals, and symbols.""",
20
+ features=datasets.Features(
21
+ {
22
+ "image": datasets.Image(),
23
+ "label": datasets.ClassLabel(names=self._labels()),
24
+ }
25
+ ),
26
+ supervised_keys=("image", "label"),
27
+ homepage="https://github.com/MartinThoma/HASY",
28
+ citation="""@article{thoma2017hasyv2,
29
+ title={The hasyv2 dataset},
30
+ author={Thoma, Martin},
31
+ journal={arXiv preprint arXiv:1701.08380},
32
+ year={2017}}""",
33
+ )
34
+
35
+ def _split_generators(self, dl_manager):
36
+ url = "https://zenodo.org/record/259444/files/HASYv2.tar.bz2?download=1"
37
+ archive_path = dl_manager.download_and_extract(url)
38
+
39
+ fold_1_dir = os.path.join(archive_path, "classification-task/fold-1")
40
+ return [
41
+ datasets.SplitGenerator(
42
+ name=datasets.Split.TRAIN,
43
+ gen_kwargs={"csv_path": os.path.join(fold_1_dir, "train.csv"), "base_dir": archive_path},
44
+ ),
45
+ datasets.SplitGenerator(
46
+ name=datasets.Split.TEST,
47
+ gen_kwargs={"csv_path": os.path.join(fold_1_dir, "test.csv"), "base_dir": archive_path},
48
+ ),
49
+ ]
50
+
51
+ def _generate_examples(self, csv_path, base_dir):
52
+ # Read the CSV file
53
+ df = pd.read_csv(csv_path)
54
+
55
+ for idx, row in df.iterrows():
56
+ # Resolve the full path to the image
57
+ image_path = os.path.join(base_dir, row["path"].lstrip("../../"))
58
+
59
+ # Open the image and convert to grayscale
60
+ with Image.open(image_path).convert("L") as image:
61
+ # Save the processed image to a temporary file
62
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
63
+ image.save(temp_file.name, format="PNG")
64
+ temp_image_path = temp_file.name
65
+
66
+ yield idx, {
67
+ "image": temp_image_path, # Provide the path to the temporary file
68
+ "label": str(row["symbol_id"]), # Pass the label as a string
69
+ }
70
+
71
+ @staticmethod
72
+ def _labels():
73
+ return [
74
+ "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
75
+ "41", "42", "43", "44", "45", "46", "47", "48", "49", "50",
76
+ "51", "52", "53", "54", "55", "56", "59", "70", "71", "72",
77
+ "73", "74", "75", "76", "77", "78", "79", "81", "82", "87",
78
+ "88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
79
+ "98", "99", "100", "101", "102", "103", "104", "105", "106",
80
+ "107", "108", "110", "111", "112", "113", "114", "115", "116",
81
+ "117", "150", "151", "152", "153", "154", "155", "156", "157",
82
+ "158", "159", "160", "161", "162", "163", "164", "165", "166",
83
+ "167", "168", "169", "170", "171", "174", "175", "176", "177",
84
+ "178", "179", "180", "181", "182", "183", "184", "185", "186",
85
+ "187", "188", "189", "190", "191", "192", "193", "194", "195",
86
+ "196", "197", "254", "257", "259", "260", "261", "262", "263",
87
+ "264", "265", "266", "267", "268", "269", "508", "510", "511",
88
+ "512", "513", "514", "517", "520", "521", "523", "524", "526",
89
+ "527", "528", "529", "530", "531", "532", "533", "534", "535",
90
+ "536", "537", "538", "539", "540", "541", "542", "544", "549",
91
+ "550", "553", "555", "562", "564", "574", "577", "582", "583",
92
+ "584", "591", "595", "600", "601", "603", "604", "605", "607",
93
+ "608", "609", "610", "611", "612", "613", "614", "615", "616",
94
+ "617", "618", "620", "621", "622", "630", "631", "634", "635",
95
+ "636", "639", "640", "644", "647", "650", "661", "671", "678",
96
+ "679", "683", "684", "698", "711", "712", "713", "716", "728",
97
+ "739", "741", "743", "748", "751", "753", "756", "757", "758",
98
+ "759", "761", "762", "763", "764", "765", "767", "768", "770",
99
+ "771", "775", "777", "778", "783", "785", "786", "788", "791",
100
+ "792", "801", "809", "812", "817", "822", "823", "827", "837",
101
+ "838", "881", "882", "884", "885", "886", "887", "888", "889",
102
+ "890", "891", "892", "894", "901", "912", "913", "914", "915",
103
+ "916", "917", "918", "919", "920", "921", "922", "923", "924",
104
+ "934", "936", "941", "943", "944", "945", "946", "947", "948",
105
+ "949", "950", "951", "953", "956", "957", "958", "959", "960",
106
+ "965", "968", "971", "972", "973", "974", "977", "992", "993",
107
+ "994", "995", "996", "997", "998", "999", "1000", "1004", "1005",
108
+ "1006", "1007", "1008", "1010", "1011", "1012", "1013", "1016",
109
+ "1018", "1019", "1031", "1037", "1042", "1045", "1046", "1051",
110
+ "1053", "1062", "1064", "1065", "1066", "1074", "1075", "1077",
111
+ "1078", "1079", "1080", "1082", "1086", "1090", "1093", "1101",
112
+ "1102", "1103", "1111", "1112", "1115", "1116", "1117", "1168",
113
+ "1169", "1177", "1184", "1185", "1187", "1314", "1315", "1316",
114
+ "1317", "1369", "1371", "1374", "1382", "1385", "1394", "1395",
115
+ "1396", "1400"
116
+ ]