|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
import zipfile
|
|
import requests
|
|
import argparse
|
|
from huggingface_hub import HfApi
|
|
|
|
|
|
repo_id = "dappu97/Coil100-Augmented/coil-100-augmented"
|
|
temp_zip_folder = "./temp_zips"
|
|
|
|
|
|
|
|
|
|
api = HfApi()
|
|
|
|
|
|
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}")
|
|
|
|
|
|
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
|
|
|
|
print(f"Extracting {zip_file}...")
|
|
with zipfile.ZipFile(zip_path, "r") as z:
|
|
for file_name in z.namelist():
|
|
|
|
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()
|
|
|
|
|
|
output_folder = args.output_folder
|
|
|
|
|
|
os.makedirs(output_folder, exist_ok=True)
|
|
os.makedirs(temp_zip_folder, exist_ok=True)
|
|
|
|
|
|
|
|
download_zip_files(repo_id, temp_zip_folder)
|
|
extract_zip_files(os.path.join(temp_zip_folder, "coil-100-augmented"), output_folder)
|
|
|
|
print(f"All images have been saved to {output_folder}.")
|
|
|