Datasets:

Languages:
English
ArXiv:
License:
File size: 5,995 Bytes
b51f134
 
62cd5a4
 
 
 
08bea20
62cd5a4
 
 
 
 
 
135a36b
312c3d3
62cd5a4
 
be5297a
62cd5a4
 
 
 
be5297a
62cd5a4
 
 
 
 
 
be5297a
62cd5a4
be5297a
62cd5a4
be5297a
 
62cd5a4
 
 
be5297a
62cd5a4
 
be5297a
 
 
 
62cd5a4
 
 
08bea20
 
 
62cd5a4
 
9b8a65c
 
62cd5a4
 
 
be5297a
62cd5a4
 
08bea20
 
 
62cd5a4
 
 
be5297a
62cd5a4
 
9b8a65c
62cd5a4
9b8a65c
 
 
08bea20
62cd5a4
9b8a65c
 
 
 
 
62cd5a4
9b8a65c
 
62cd5a4
08bea20
62cd5a4
 
 
312c3d3
62cd5a4
be5297a
62cd5a4
 
 
 
312c3d3
 
 
62cd5a4
 
 
 
 
be5297a
62cd5a4
 
 
 
 
 
 
 
 
 
 
9b8a65c
 
 
 
 
 
 
08bea20
 
 
9b8a65c
 
 
 
 
 
 
 
 
 
 
08bea20
 
 
9b8a65c
08bea20
 
 
 
 
 
9b8a65c
 
 
 
62cd5a4
 
 
 
9b8a65c
08bea20
62cd5a4
9b8a65c
62cd5a4
 
 
9b8a65c
08bea20
 
 
 
 
 
 
9b8a65c
62cd5a4
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Script to download 3D objects from the Smithsonian Institution."""

import multiprocessing
import os
from functools import partial
from multiprocessing import Pool
from typing import Dict, List, Optional, Tuple

import fsspec
import pandas as pd
import requests
from loguru import logger
from tqdm import tqdm

from objaverse_xl.utils import get_uid_from_str


def load_smithsonian_metadata(download_dir: str = "~/.objaverse") -> pd.DataFrame:
    """Loads the Smithsonian Object Metadata dataset as a Pandas DataFrame.

    Args:
        download_dir (str, optional): Directory to download the parquet metadata file.
            Supports all file systems supported by fsspec. Defaults to "~/.objaverse".

    Returns:
        pd.DataFrame: Smithsonian Object Metadata dataset as a Pandas DataFrame with
            columns for the object "title", "url", "quality", "file_type", "uid", and
            "license". The quality is always Medium and the file_type is always glb.
    """
    filename = os.path.join(download_dir, "smithsonian", "object-metadata.parquet")
    fs, path = fsspec.core.url_to_fs(filename)
    fs.makedirs(os.path.dirname(path), exist_ok=True)

    # download the parquet file if it doesn't exist
    if not fs.exists(path):
        url = "https://huggingface.co/datasets/allenai/objaverse-xl/resolve/main/smithsonian/object-metadata.parquet"
        response = requests.get(url)
        response.raise_for_status()
        with fs.open(path, "wb") as file:
            file.write(response.content)

    # load the parquet file with fsspec
    with fs.open(path) as f:
        df = pd.read_parquet(f)

    return df


def _download_smithsonian_object(
    url: str, download_dir: str = "~/.objaverse"
) -> Tuple[str, Optional[str]]:
    """Downloads a Smithsonian Object from a URL.

    Overwrites the file if it already exists and assumes this was previous checked.

    Args:
        url (str): URL to download the Smithsonian Object from.
        download_dir (str, optional): Directory to download the Smithsonian Object to.
            Supports all file systems supported by fsspec. Defaults to "~/.objaverse".

    Returns:
        Tuple[str, Optional[str]]: Tuple of the URL and the path to the downloaded
            Smithsonian Object. If the Smithsonian Object was not downloaded, the path
            will be None.
    """
    uid = get_uid_from_str(url)

    filename = os.path.join(download_dir, "smithsonian", "objects", f"{uid}.glb")
    fs, path = fsspec.core.url_to_fs(filename)

    response = requests.get(url)

    # check if the path is valid
    if response.status_code == 404:
        logger.warning(f"404 for {url}")
        return url, None

    # write to tmp path so that we don't have a partial file
    tmp_path = f"{path}.tmp"
    with fs.open(tmp_path, "wb") as file:
        for chunk in response.iter_content(chunk_size=8192):
            file.write(chunk)

    # rename to final path
    fs.rename(tmp_path, path)

    return url, filename


def download_smithsonian_objects(
    urls: Optional[List[str]] = None,
    processes: Optional[int] = None,
    download_dir: str = "~/.objaverse",
) -> List[Dict[str, str]]:
    """Downloads all Smithsonian Objects.

    Args:
        urls (Optional[List[str]], optional): List of URLs to download the Smithsonian
            Objects from. If None, all Smithsonian Objects will be downloaded. Defaults
            to None.
        processes (Optional[int], optional): Number of processes to use for downloading
            the Smithsonian Objects. If None, the number of processes will be set to the
            number of CPUs on the machine (multiprocessing.cpu_count()). Defaults to
            None.
        download_dir (str, optional): Directory to download the Smithsonian Objects to.
            Supports all file systems supported by fsspec. Defaults to "~/.objaverse".

    Returns:
        List[Dict[str, str]]: List of dictionaries with keys "download_path" and "url"
            for each downloaded object.
    """
    if processes is None:
        processes = multiprocessing.cpu_count()
    if urls is None:
        df = load_smithsonian_metadata(download_dir=download_dir)
        urls = df["url"].tolist()

    # filename = os.path.join(download_dir, "smithsonian", "objects", f"{uid}.glb")
    objects_dir = os.path.join(download_dir, "smithsonian", "objects")
    fs, path = fsspec.core.url_to_fs(objects_dir)
    fs.makedirs(path, exist_ok=True)

    # get the existing glb files
    existing_glb_files = fs.glob(os.path.join(objects_dir, "*.glb"), refresh=True)
    existing_uids = [
        os.path.basename(file).split(".")[0] for file in existing_glb_files
    ]

    # find the urls that need to be downloaded
    out = []
    urls_to_download = set([])
    already_downloaded_urls = set([])
    for url in urls:
        uid = get_uid_from_str(url)
        if uid not in existing_uids:
            urls_to_download.add(url)
        else:
            already_downloaded_urls.add(url)
        out.append(
            {"download_path": os.path.join(objects_dir, f"{uid}.glb"), "url": url}
        )

    logger.info(
        f"Found {len(already_downloaded_urls)} Smithsonian Objects already downloaded"
    )
    logger.info(
        f"Downloading {len(urls_to_download)} Smithsonian Objects with {processes=}"
    )

    if len(urls_to_download) == 0:
        return out

    with Pool(processes=processes) as pool:
        results = list(
            tqdm(
                pool.imap_unordered(
                    partial(_download_smithsonian_object, download_dir=download_dir),
                    urls_to_download,
                ),
                total=len(urls_to_download),
                desc="Downloading Smithsonian Objects",
            )
        )

    out.extend(
        [
            {"download_path": download_path, "url": url}
            for url, download_path in results
            if download_path is not None
        ]
    )

    return out