Datasets:

Modalities:
Image
Text
Size:
< 1K
ArXiv:
Libraries:
Datasets
License:
Yechi50121 commited on
Commit
2d4bfb4
·
verified ·
1 Parent(s): 3a0e677

Delete transform.py

Browse files
Files changed (1) hide show
  1. transform.py +0 -93
transform.py DELETED
@@ -1,93 +0,0 @@
1
- import os
2
- import glob
3
- import json
4
- import shutil
5
-
6
- def process_and_flatten_split(split_name):
7
- """
8
- Processes metadata and physically FLATTENS the directory structure so images
9
- are directly under the split folder (e.g., train/*.jpg), exactly matching
10
- Hugging Face's most strict ImageFolder requirements.
11
-
12
- SIMPLIFIED VERSION: Only outputs `file_name` and an empty `text` field for debugging.
13
- """
14
- if not os.path.exists(split_name):
15
- print(f"Directory {split_name} not found. Skipping.")
16
- return
17
-
18
- images_dir = os.path.join(split_name, "images")
19
-
20
- # Check if images are in an "images" subdirectory or already flattened
21
- if not os.path.exists(images_dir):
22
- # Maybe it's already flattened from a previous run
23
- image_files = glob.glob(os.path.join(split_name, "*.jpg")) + glob.glob(os.path.join(split_name, "*.png"))
24
- if not image_files:
25
- print(f"No images found for {split_name} in 'images/' or root. Skipping.")
26
- return
27
- images_dir = split_name # Images are already directly in the split folder
28
- else:
29
- # Standard case: images are in split_name/images/
30
- image_files = glob.glob(os.path.join(images_dir, "*.*"))
31
-
32
- # Filter only actual image files
33
- image_files = [f for f in image_files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp'))]
34
-
35
- metadata_records = []
36
-
37
- for img_path in image_files:
38
- img_basename = os.path.basename(img_path)
39
-
40
- # VERY SIMPLE SCHEMA: Just file_name and an empty text field
41
- record = {
42
- "file_name": img_basename,
43
- "text": ""
44
- }
45
-
46
- metadata_records.append(record)
47
-
48
- # --- DIRECTORY FLATTENING & CLEANUP ---
49
- print(f"\n[{split_name}] Flattening directory structure...")
50
-
51
- # 1. Move images out of 'images/' and directly into 'split_name/'
52
- if images_dir != split_name:
53
- for img_path in image_files:
54
- shutil.move(img_path, os.path.join(split_name, os.path.basename(img_path)))
55
-
56
- # Remove the now-empty 'images' folder
57
- try:
58
- os.rmdir(images_dir)
59
- print(f"[{split_name}] Removed empty 'images/' subdirectory.")
60
- except OSError:
61
- pass
62
-
63
- # 2. Write the standard jsonl file DIRECTLY into the split root
64
- metadata_path = os.path.join(split_name, "metadata.jsonl")
65
- with open(metadata_path, "w") as f:
66
- for record in metadata_records:
67
- f.write(json.dumps(record) + "\n")
68
- print(f"[{split_name}] ✅ Generated {metadata_path} with {len(metadata_records)} records.")
69
-
70
- # 3. Move raw annotations OUT of the split completely to avoid Text Dataset fallback
71
- os.makedirs("raw_annotations", exist_ok=True)
72
- anno_2d_dir = os.path.join(split_name, "2D_annotations")
73
- if os.path.exists(anno_2d_dir):
74
- shutil.move(anno_2d_dir, f"raw_annotations/{split_name}_2D_annotations")
75
- print(f"[{split_name}] 📦 Moved 2D_annotations to raw_annotations/")
76
-
77
- anno_3d_dir = os.path.join(split_name, "3D_annotations")
78
- if os.path.exists(anno_3d_dir):
79
- shutil.move(anno_3d_dir, f"raw_annotations/{split_name}_3D_annotations")
80
- print(f"[{split_name}] 📦 Moved 3D_annotations to raw_annotations/")
81
-
82
-
83
- if __name__ == "__main__":
84
- print("Processing Dataset Metadata & Flattening structure for Hugging Face Viewer...\n")
85
-
86
- # Process all splits using the simplified schema
87
- process_and_flatten_split("train")
88
- process_and_flatten_split("val")
89
- process_and_flatten_split("test")
90
-
91
- print("\n🎉 Done! Structure is flattened and metadata ONLY contains 'file_name' and 'text'.")
92
- print("\n⚠️ CRITICAL NEXT STEP:")
93
- print("Please upload these highly simplified metadata.jsonl files to Hugging Face to test if the Viewer loads.")