CCAiM-CloudsDataset / meta_data.py
serbekun's picture
add new 42 photos
b6991dc
import json
import os
from datasets import Dataset, Features, ClassLabel, Image
from huggingface_hub import login
JSON_FILE = "clouds_1/labels.json"
DATASET_NAME = "CCAiM-CloudsDataset"
IMAGE_FOLDER = "clouds_1"
HUGGINGFACE_TOKEN = input("hugging face aces token: ")
POSSIBLE_SUFFIXES = [".jpg", ".jpeg", ".png", ".bmp", ".webp", "JPG"]
login(token=HUGGINGFACE_TOKEN)
with open(JSON_FILE, "r", encoding="utf-8") as f:
data_json = json.load(f)
all_labels = sorted(set(data_json.values()))
class_labels = ClassLabel(names=all_labels)
def find_file(prefix, folder):
for suffix in POSSIBLE_SUFFIXES:
candidate = os.path.join(folder, prefix + suffix)
if os.path.exists(candidate):
return candidate
raise FileNotFoundError(f"file not found with prefix {prefix} {POSSIBLE_SUFFIXES}")
data_list = []
for prefix, label in data_json.items():
try:
image_path = find_file(prefix, IMAGE_FOLDER)
data_list.append({"image": image_path, "label": label})
except FileNotFoundError as e:
print(e)
features = Features({
"image": Image(),
"label": ClassLabel(names=all_labels)
})
dataset = Dataset.from_list(data_list, features=features)
dataset.push_to_hub(DATASET_NAME, private=False)
print(f"Dataset {DATASET_NAME} pulled to Hugging Face!")