Create rock-paper-scissors.py
Browse files- rock-paper-scissors.py +51 -0
rock-paper-scissors.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
|
6 |
+
class RockPaperScissor(datasets.GeneratorBasedBuilder):
|
7 |
+
"""Rock Paper Scissors dataset."""
|
8 |
+
VERSION = datasets.Version("1.0.0")
|
9 |
+
|
10 |
+
def _info(self):
|
11 |
+
return datasets.DatasetInfo(
|
12 |
+
description="Rock Paper Scissors contains images from various hands, from different races, ages, and "
|
13 |
+
"genders, posed into Rock / Paper or Scissors and labeled as such.",
|
14 |
+
features=datasets.Features(
|
15 |
+
{
|
16 |
+
"image": datasets.Image(),
|
17 |
+
"label": datasets.ClassLabel(names=["rock", "paper", "scissors"]),
|
18 |
+
}
|
19 |
+
),
|
20 |
+
supervised_keys=("image", "label"),
|
21 |
+
homepage="https://laurencemoroney.com/datasets.html",
|
22 |
+
license="CC By 2.0",
|
23 |
+
)
|
24 |
+
|
25 |
+
def _split_generators(self, dl_manager):
|
26 |
+
urls = {
|
27 |
+
"train": "https://storage.googleapis.com/download.tensorflow.org/data/rps.zip",
|
28 |
+
"test": "https://storage.googleapis.com/download.tensorflow.org/data/rps-test-set.zip",
|
29 |
+
}
|
30 |
+
extracted_paths = dl_manager.download_and_extract(urls)
|
31 |
+
return [
|
32 |
+
datasets.SplitGenerator(
|
33 |
+
name=datasets.Split.TRAIN,
|
34 |
+
gen_kwargs={"data_dir": extracted_paths["train"]},
|
35 |
+
),
|
36 |
+
datasets.SplitGenerator(
|
37 |
+
name=datasets.Split.TEST,
|
38 |
+
gen_kwargs={"data_dir": extracted_paths["test"]},
|
39 |
+
),
|
40 |
+
]
|
41 |
+
|
42 |
+
def _generate_examples(self, data_dir):
|
43 |
+
for root, _, files in os.walk(data_dir):
|
44 |
+
for file_name in files:
|
45 |
+
if file_name.endswith(".png"):
|
46 |
+
label = os.path.basename(root) # Folder name as label
|
47 |
+
file_path = os.path.join(root, file_name)
|
48 |
+
# Open image and ensure it is RGB
|
49 |
+
with open(file_path, "rb") as img_file:
|
50 |
+
image = Image.open(img_file).convert("RGB")
|
51 |
+
yield file_path, {"image": image, "label": label}
|