Upload 2 files
Browse files- download_coil100-binary.py +80 -0
- download_coil100.py +80 -0
download_coil100-binary.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
import os
|
9 |
+
import zipfile
|
10 |
+
import requests
|
11 |
+
import argparse
|
12 |
+
from huggingface_hub import HfApi
|
13 |
+
|
14 |
+
# Parameters
|
15 |
+
repo_id = "dappu97/Coil100-Augmented/coil-100-augmented-binary" # Replace with your Hugging Face repo ID
|
16 |
+
temp_zip_folder = "./temp_zips" # Temporary folder to store downloaded ZIPs
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
# Initialize Hugging Face API
|
21 |
+
api = HfApi()
|
22 |
+
|
23 |
+
# Step 1: List and download all ZIP files from the Hugging Face repository
|
24 |
+
def download_zip_files(repo_id, temp_zip_folder):
|
25 |
+
files = api.list_repo_files(repo_id, repo_type="dataset")
|
26 |
+
zip_files = [file for file in files if file.endswith(".zip")]
|
27 |
+
|
28 |
+
for zip_file in zip_files:
|
29 |
+
url = f"https://huggingface.co/datasets/{repo_id}/resolve/main/{zip_file}"
|
30 |
+
local_path = os.path.join(temp_zip_folder, zip_file)
|
31 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
32 |
+
print(f"Downloading {zip_file}...")
|
33 |
+
response = requests.get(url, stream=True)
|
34 |
+
if response.status_code == 200:
|
35 |
+
with open(local_path, "wb") as f:
|
36 |
+
f.write(response.content)
|
37 |
+
print(f"Downloaded {zip_file} to {local_path}.")
|
38 |
+
else:
|
39 |
+
print(f"Failed to download {zip_file}. HTTP status code: {response.status_code}")
|
40 |
+
|
41 |
+
# Step 2: Extract all ZIP files and save images in a single folder
|
42 |
+
def extract_zip_files(temp_zip_folder, output_folder):
|
43 |
+
os.makedirs(temp_zip_folder, exist_ok=True)
|
44 |
+
|
45 |
+
for zip_file in os.listdir(temp_zip_folder):
|
46 |
+
|
47 |
+
zip_path = os.path.join(temp_zip_folder, zip_file)
|
48 |
+
if not zipfile.is_zipfile(zip_path):
|
49 |
+
continue # Skip non-ZIP files
|
50 |
+
|
51 |
+
print(f"Extracting {zip_file}...")
|
52 |
+
with zipfile.ZipFile(zip_path, "r") as z:
|
53 |
+
for file_name in z.namelist():
|
54 |
+
# Extract only files (skip directories)
|
55 |
+
if not file_name.endswith("/"):
|
56 |
+
file_data = z.read(file_name)
|
57 |
+
output_path = os.path.join(output_folder, os.path.basename(file_name))
|
58 |
+
with open(output_path, "wb") as f:
|
59 |
+
f.write(file_data)
|
60 |
+
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
|
64 |
+
parser = argparse.ArgumentParser(description="Download and unzip ZIP files from a Hugging Face dataset.")
|
65 |
+
parser.add_argument("output_folder", help="Path to the folder where extracted files will be saved.")
|
66 |
+
args = parser.parse_args()
|
67 |
+
|
68 |
+
# Folder to save all images
|
69 |
+
output_folder = args.output_folder
|
70 |
+
|
71 |
+
# Create output directories
|
72 |
+
os.makedirs(output_folder, exist_ok=True)
|
73 |
+
os.makedirs(temp_zip_folder, exist_ok=True)
|
74 |
+
|
75 |
+
|
76 |
+
# Download and extract the dataset
|
77 |
+
download_zip_files(repo_id, temp_zip_folder)
|
78 |
+
extract_zip_files(os.path.join(temp_zip_folder, "coil-100-augmented-binary"), output_folder)
|
79 |
+
|
80 |
+
print(f"All images have been saved to {output_folder}.")
|
download_coil100.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
import os
|
9 |
+
import zipfile
|
10 |
+
import requests
|
11 |
+
import argparse
|
12 |
+
from huggingface_hub import HfApi
|
13 |
+
|
14 |
+
# Parameters
|
15 |
+
repo_id = "dappu97/Coil100-Augmented/coil-100-augmented" # Replace with your Hugging Face repo ID
|
16 |
+
temp_zip_folder = "./temp_zips" # Temporary folder to store downloaded ZIPs
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
# Initialize Hugging Face API
|
21 |
+
api = HfApi()
|
22 |
+
|
23 |
+
# Step 1: List and download all ZIP files from the Hugging Face repository
|
24 |
+
def download_zip_files(repo_id, temp_zip_folder):
|
25 |
+
files = api.list_repo_files(repo_id, repo_type="dataset")
|
26 |
+
zip_files = [file for file in files if file.endswith(".zip")]
|
27 |
+
|
28 |
+
for zip_file in zip_files:
|
29 |
+
url = f"https://huggingface.co/datasets/{repo_id}/resolve/main/{zip_file}"
|
30 |
+
local_path = os.path.join(temp_zip_folder, zip_file)
|
31 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
32 |
+
print(f"Downloading {zip_file}...")
|
33 |
+
response = requests.get(url, stream=True)
|
34 |
+
if response.status_code == 200:
|
35 |
+
with open(local_path, "wb") as f:
|
36 |
+
f.write(response.content)
|
37 |
+
print(f"Downloaded {zip_file} to {local_path}.")
|
38 |
+
else:
|
39 |
+
print(f"Failed to download {zip_file}. HTTP status code: {response.status_code}")
|
40 |
+
|
41 |
+
# Step 2: Extract all ZIP files and save images in a single folder
|
42 |
+
def extract_zip_files(temp_zip_folder, output_folder):
|
43 |
+
os.makedirs(temp_zip_folder, exist_ok=True)
|
44 |
+
|
45 |
+
for zip_file in os.listdir(temp_zip_folder):
|
46 |
+
|
47 |
+
zip_path = os.path.join(temp_zip_folder, zip_file)
|
48 |
+
if not zipfile.is_zipfile(zip_path):
|
49 |
+
continue # Skip non-ZIP files
|
50 |
+
|
51 |
+
print(f"Extracting {zip_file}...")
|
52 |
+
with zipfile.ZipFile(zip_path, "r") as z:
|
53 |
+
for file_name in z.namelist():
|
54 |
+
# Extract only files (skip directories)
|
55 |
+
if not file_name.endswith("/"):
|
56 |
+
file_data = z.read(file_name)
|
57 |
+
output_path = os.path.join(output_folder, os.path.basename(file_name))
|
58 |
+
with open(output_path, "wb") as f:
|
59 |
+
f.write(file_data)
|
60 |
+
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
|
64 |
+
parser = argparse.ArgumentParser(description="Download and unzip ZIP files from a Hugging Face dataset.")
|
65 |
+
parser.add_argument("output_folder", help="Path to the folder where extracted files will be saved.")
|
66 |
+
args = parser.parse_args()
|
67 |
+
|
68 |
+
# Folder to save all images
|
69 |
+
output_folder = args.output_folder
|
70 |
+
|
71 |
+
# Create output directories
|
72 |
+
os.makedirs(output_folder, exist_ok=True)
|
73 |
+
os.makedirs(temp_zip_folder, exist_ok=True)
|
74 |
+
|
75 |
+
|
76 |
+
# Download and extract the dataset
|
77 |
+
download_zip_files(repo_id, temp_zip_folder)
|
78 |
+
extract_zip_files(os.path.join(temp_zip_folder, "coil-100-augmented"), output_folder)
|
79 |
+
|
80 |
+
print(f"All images have been saved to {output_folder}.")
|