DEEPFRUlT_DATASET / deepfruit_dataset.py
sc890's picture
Update deepfruit_dataset.py
d633506 verified
# 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
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/DEEPFRUlT_DATASET/resolve/main/Fruits_Dataset_Train.zip',
"test": 'https://huggingface.co/datasets/sc890/DEEPFRUlT_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"),
"folder_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,
"folder_number": number_dir,
"image": img,
"image_path": relative_image_path,
"label": label_dict[image_file]
}