File size: 4,629 Bytes
ef7d2ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Address all TODOs and remove all explanatory comments
"""TODO: Add a description here."""


import csv
import json
from PIL import Image as IMG
import os
import datasets
import pandas as pd

from datasets import GeneratorBasedBuilder, DatasetInfo, Features, ClassLabel,Image, SplitGenerator, Sequence

_DESCRIPTION = """\
The "DeepFruit" dataset is a comprehensive collection designed for the advancement of research in fruit detection, recognition, and classification.
The type of fruit is determined by various external appearance features. The dataset, from Mendeley, comprises 21,122 images of 20 diverse fruit types across 8 different combinations. This dataset includes separate images and  CSV files for training and testing, each containing varying quantities of each fruit. The objective of this study is to convert fruit images into the PIL (Python Imaging Library) format.
"""
_URLS = {
    "train": 'https://huggingface.co/datasets/sc890/deepfruit_dataset/resolve/main/Fruits_Dataset_Train.zip',
    "test": 'https://huggingface.co/datasets/sc890/deepfruit_dataset/resolve/main/Fruits_Dataset_Test.zip',
}
class DeepFruitDataset(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        BuilderConfig(name="deepfruit_dataset", version=Version("1.0.0"))
    ]
    _URLS = _URLS
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features= datasets.Features({
                "image_id": datasets.Value("string"),
                "number":datasets.Value("int32"),
                "image": datasets.Image(),
                "image_path": datasets.Value("string"),
                "label": datasets.Value("string"),
            }),
            homepage="https://data.mendeley.com/datasets/5prc54r4rt/1",  
            license="Mendeley License: CC BY 4.0",  
        )

    def _split_generators(self, dl_manager):
        urls_to_download = self._URLS
        downloaded_files = dl_manager.download_and_extract(urls_to_download)
        print(downloaded_files)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"], "folder": "Fruits_Dataset_Train", "csv_name": "Labels_Train.csv"}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"], "folder": "Fruits_Dataset_Test","csv_name": "Labels_Test.csv"}),
        ]

    def _generate_examples(self, filepath, folder, csv_name=None):
        path = os.path.join(filepath, folder, csv_name)
        label_dict = {}
        count = 0
        with open(path, 'r') as file:
            for line in file:
                if count == 0:
                    count += 1
                    continue
                image_name = line.split(",")[0]
                label = line.replace(image_name, "")
                label = label.replace(",", "")
                label = label.replace("\n", "")
                label_dict[image_name] = label

        for number_dir in range(1, 9):
            image_dir = os.path.join(filepath,folder, str(number_dir)) 
            for image_file in os.listdir(image_dir):  
                if not image_file.endswith('.jpg'):
                    continue
                last_dot_index = image_file.rfind('.')
                if last_dot_index != -1:
                    image_id = image_file[:last_dot_index]
                else:
                    image_id = image_file  
                image_id = str(number_dir)  + image_id
                image_path = os.path.join(image_dir, image_file)  
                relative_image_path = os.path.relpath(image_path, start=filepath)
                img = IMG.open(image_path)
                yield image_id, {
                    "image_id": image_id,
                    "number": number_dir,
                    "image": img, 
                    "image_path": relative_image_path,
                    "label": label_dict[image_file]
                }