Coil100-Augmented / download_coil100-binary.py
dappu97's picture
Upload 2 files
d7d373a verified
import os
import zipfile
import requests
import argparse
from huggingface_hub import HfApi
# Parameters
repo_id = "dappu97/Coil100-Augmented/coil-100-augmented-binary" # Replace with your Hugging Face repo ID
temp_zip_folder = "./temp_zips" # Temporary folder to store downloaded ZIPs
# Initialize Hugging Face API
api = HfApi()
# Step 1: List and download all ZIP files from the Hugging Face repository
def download_zip_files(repo_id, temp_zip_folder):
files = api.list_repo_files(repo_id, repo_type="dataset")
zip_files = [file for file in files if file.endswith(".zip")]
for zip_file in zip_files:
url = f"https://huggingface.co/datasets/{repo_id}/resolve/main/{zip_file}"
local_path = os.path.join(temp_zip_folder, zip_file)
os.makedirs(os.path.dirname(local_path), exist_ok=True)
print(f"Downloading {zip_file}...")
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(local_path, "wb") as f:
f.write(response.content)
print(f"Downloaded {zip_file} to {local_path}.")
else:
print(f"Failed to download {zip_file}. HTTP status code: {response.status_code}")
# Step 2: Extract all ZIP files and save images in a single folder
def extract_zip_files(temp_zip_folder, output_folder):
os.makedirs(temp_zip_folder, exist_ok=True)
for zip_file in os.listdir(temp_zip_folder):
zip_path = os.path.join(temp_zip_folder, zip_file)
if not zipfile.is_zipfile(zip_path):
continue # Skip non-ZIP files
print(f"Extracting {zip_file}...")
with zipfile.ZipFile(zip_path, "r") as z:
for file_name in z.namelist():
# Extract only files (skip directories)
if not file_name.endswith("/"):
file_data = z.read(file_name)
output_path = os.path.join(output_folder, os.path.basename(file_name))
with open(output_path, "wb") as f:
f.write(file_data)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Download and unzip ZIP files from a Hugging Face dataset.")
parser.add_argument("output_folder", help="Path to the folder where extracted files will be saved.")
args = parser.parse_args()
# Folder to save all images
output_folder = args.output_folder
# Create output directories
os.makedirs(output_folder, exist_ok=True)
os.makedirs(temp_zip_folder, exist_ok=True)
# Download and extract the dataset
download_zip_files(repo_id, temp_zip_folder)
extract_zip_files(os.path.join(temp_zip_folder, "coil-100-augmented-binary"), output_folder)
print(f"All images have been saved to {output_folder}.")