JeffreyJsam commited on
Commit
1720672
·
verified ·
1 Parent(s): 3ebd901

Delete sample_swim.py

Browse files
Files changed (1) hide show
  1. sample_swim.py +0 -83
sample_swim.py DELETED
@@ -1,83 +0,0 @@
1
- import argparse
2
- from io import BytesIO
3
- from pathlib import Path
4
- from huggingface_hub import list_repo_tree, hf_hub_url
5
- from huggingface_hub.hf_api import RepoFile
6
- import fsspec
7
- from PIL import Image
8
- from tqdm import tqdm
9
-
10
- def sample_dataset(
11
- repo_id: str,
12
- image_subdir: str,
13
- label_subdir: str,
14
- output_dir: str,
15
- max_files: int = 500,
16
- ):
17
-
18
- image_files = list_repo_tree(
19
- repo_id=repo_id,
20
- path_in_repo=image_subdir,
21
- repo_type="dataset",
22
- recursive=True
23
- )
24
-
25
- count = 0
26
- for img_file in tqdm(image_files, desc="Downloading samples"):
27
- if not isinstance(img_file, RepoFile) or not img_file.path.lower().endswith((".png")):
28
- continue
29
-
30
- # Relative path after the image_subdir (e.g., img_0001.png)
31
- rel_path = Path(img_file.path).relative_to(image_subdir)
32
- label_path = f"{label_subdir}/{rel_path.with_suffix('.txt')}" # Change extension to .txt
33
-
34
- image_url = hf_hub_url(repo_id=repo_id, filename=img_file.path, repo_type="dataset")
35
- label_url = hf_hub_url(repo_id=repo_id, filename=label_path, repo_type="dataset")
36
-
37
- local_image_path = Path(output_dir) / img_file.path
38
- local_label_path = Path(output_dir) / label_path
39
-
40
- local_image_path.parent.mkdir(parents=True, exist_ok=True)
41
- local_label_path.parent.mkdir(parents=True, exist_ok=True)
42
-
43
- try:
44
- # Download and save the image
45
- with fsspec.open(image_url) as f:
46
- image = Image.open(BytesIO(f.read()))
47
- image.save(local_image_path)
48
-
49
- # Download and save the corresponding .txt label
50
- with fsspec.open(label_url) as f:
51
- txt_content = f.read()
52
- with open(local_label_path, "wb") as out_f:
53
- out_f.write(txt_content)
54
-
55
- # print(f"[{count+1}] {rel_path} and {rel_path.with_suffix('.txt')}")
56
- count += 1
57
- except Exception as e:
58
- print(f" Failed {rel_path}: {e}")
59
-
60
- if count >= max_files:
61
- break
62
-
63
- print(f" Downloaded {count} image/txt pairs.")
64
- print(f" Saved under: {Path(output_dir).resolve()}")
65
-
66
- def parse_args():
67
- parser = argparse.ArgumentParser(description="Stream and sample paired images + txt labels from a Hugging Face folder-structured dataset.")
68
- parser.add_argument("--repo-id", required=False, default = "JeffreyJsam/SWiM-SpacecraftWithMasks",help="Hugging Face dataset repo ID.")
69
- parser.add_argument("--image-subdir", required=False, default = "Baseline/images/val/000", help="Subdirectory path for images.")
70
- parser.add_argument("--label-subdir", required=False, default="Baseline/labels/val/000", help="Subdirectory path for txt masks.")
71
- parser.add_argument("--output-dir", default="./Sampled-SWiM", help="Where to save sampled data.")
72
- parser.add_argument("--count", type=int, default=500, help="How many samples to download.")
73
- return parser.parse_args()
74
-
75
- if __name__ == "__main__":
76
- args = parse_args()
77
- sample_dataset(
78
- repo_id=args.repo_id,
79
- image_subdir=args.image_subdir,
80
- label_subdir=args.label_subdir,
81
- output_dir=args.output_dir,
82
- max_files=args.count,
83
- )