met_museum / met_museum.py
miccull's picture
update loader
7023d0e
raw
history blame
No virus
9.62 kB
# 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 pandas as pd
import os
import datasets
from PIL import Image
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@misc{www.metmuseum.github.io, title={Metropolitan Museum of Art Open Access Collection}, url={https://metmuseum.github.io/}, journal={https://metmuseum.github.io/}}
"""
# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
Dataset containing 243585 pieces of visual art from various artists,
taken from the Metropolitan Museum of Art's Open Access Collection.
The Metropolitan Museum of Art (The Met) hosts images and metadata for
all objects in its collection, and these can be explored using the following
link: https://www.metmuseum.org/art/collection/search .
Of the approximately 450,000 objects in the collection, The Met has deemed
approximately 243,600 of them to be in the public domain. As such, The Met has
granted a CC0 license to the images and metadata for these objects. Thus,
they may be freely used, collected, remixed, and redistributed for
commercial or non-commercial means, without the museum's express permission.
In addition to the images, the dataset includes additional class labels and features for each image :
* "department": The object's department in the museum collection, e.g. "Drawing and Prints" or "Islamic Art".
* "artist": The artist's name, if applicable.
* "object_id": The ID used to identify the object in The Met's collection.
* "object_date": The year or approximate year(s) in which the object was made.
* "title": The object's title, if applicable.
* "object_name": The object's name, if applicable, e.g "Photograph" or "Drawing". Has more to do with the object's Medium than its title.
* "medium": The object's medium, e.g. "Albumen silver print" or "Bronze".
* "classification": The object's classification, e.g. "Woodwork" or "Ceramics-Porcelain".
* "filesize_bytes": The size, in bytes, of the image file.
* "width": The width, in pixels, of the image file.
* "height": The height, in pixels, of the image file.
* "original_link": A link to the original listing page for the object in The Met's collection.
Note:
* The authors are neither responsible for the content nor the meaning of these images.
By using the Metropolitan Museum of Art Open Access dataset, you agree to obey the terms and conditions of metmuseum.org (https://www.metmuseum.org/information/terms-and-conditions).
Text features taken from the archive @ https://github.com/metmuseum/openaccess/blob/master/MetObjects.csv
Images collected using The Met's API (see dataset homepage for more information).
"""
_HOMEPAGE = "https://www.metmuseum.github.io"
_LICENSE = "CC0"
_URLS = {"default": ""}
_departments = [
"The American Wing",
"European Sculpture and Decorative Arts",
"Modern and Contemporary Art",
"Arms and Armor",
"Medieval Art",
"Asian Art",
"Islamic Art",
"Costume Institute",
"Arts of Africa, Oceania, and the Americas",
"Drawings and Prints",
"Greek and Roman Art",
"Photographs",
"Ancient Near Eastern Art",
"Egyptian Art",
"European Paintings",
"Robert Lehman Collection",
"The Cloisters",
"Musical Instruments",
"The Libraries",
]
class MetMuseum(datasets.GeneratorBasedBuilder):
"""MetMuseum Dataset"""
VERSION = datasets.Version("0.2.0")
# This is an example of a dataset with multiple configurations.
# If you don't want/need to define several sub-sets in your dataset,
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
# If you need to make complex sub-parts in the datasets with configurable options
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
# BUILDER_CONFIG_CLASS = MyBuilderConfig
# You will be able to load one or the other configurations in the following list with
# data = datasets.load_dataset('my_dataset', 'first_domain')
# data = datasets.load_dataset('my_dataset', 'second_domain')
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="default",
version=VERSION,
description="The default version containing 81444 images with 3 labels for each",
),
]
DEFAULT_CONFIG_NAME = "default"
def _info(self):
features = datasets.Features(
{
"image": datasets.Image(),
"department": datasets.ClassLabel(
num_classes=len(_departments), names=_departments
),
# "artist": datasets.Value("string"),
# "object_id": datasets.Value("int32"),
# "accession_year": datasets.Value("string"),
# "title": datasets.Value("string"),
# "object_name": datasets.Value("string"),
# "medium": datasets.Value("string"),
# "object_date": datasets.Value("string"),
# "filesize_bytes": datasets.Value("string"),
# "width": datasets.Value("string"),
# "height": datasets.Value("string"),
# "original_link": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=features, # Here we define them above because they are different between the two configurations
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
# supervised_keys=("sentence", "label"),
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = _URLS[self.config.name]
# data_dir = dl_manager.download_and_extract(urls)
data_dir = "/Users/miccull/projects/opensource/cloob/all-images"
filepath_csv = os.path.join("/Users/miccull/projects/opensource/cloob/metObjectsFinal_all_images.csv")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"datadir": data_dir
},
),
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, datadir):
df = pd.read_csv(datadir + '/../metObjectsFinal_all_images.csv', nrows=100)
df.columns = [col.replace(' ','') for col in df.columns]
# for i in range(len(df)):
# yield i, {
# "image": os.path.join(datadir, df["basename"][i]),
# "department": df['Department'][i]
# }
for i in range(len(df)):
fname = os.path.join(datadir, df["basename"][i])
with open(fname, 'rb') as f:
img_bytes = f.read()
yield i, {
"image": {'path': os.path.join(datadir, df["basename"][i]), 'bytes':img_bytes},
"department": df['Department'][i],
"artist": df['ArtistDisplayName'][i[]],
"object_id": df['ObjectID'][i],
"accession_year": df['AccessionYear'][i],
"title": df['Title'][i],
"object_name":df['ObjectName'][i],
"medium": df['Medium'][i],
"object_date": df['ObjectDate'][i],
"filesize_bytes": df['filesize_bytes'],
"width": df['width'][i],
"height": df['height'][i],
"original_link": df['LinkResource'],
}
# for ix, obj in enumerate(df.itertuples()):
# yield ix, {
# "image": Image.open(os.path.join(datadir, obj.basename)),
# "department": obj.Department,
# # "artist": obj.ArtistDisplayName,
# # "object_id": obj.ObjectID,
# # "accession_year": obj.AccessionYear,
# # "title": obj.Title,
# # "object_name": obj.ObjectName,
# # "medium": obj.Medium,
# # "object_date": obj.ObjectDate,
# # "filesize_bytes": obj.filesize_bytes,
# # "width": obj.width,
# # "height": obj.height,
# # "original_link": obj.LinkResource,
# }