KuAvLab commited on
Commit
dfb9bd5
·
verified ·
1 Parent(s): 4b9d07c

Update EMT.py

Browse files
Files changed (1) hide show
  1. EMT.py +39 -29
EMT.py CHANGED
@@ -67,62 +67,72 @@ class EMT(datasets.GeneratorBasedBuilder):
67
 
68
  def _split_generators(self, dl_manager):
69
  """Download train/test images and annotations."""
70
-
71
- # Download and extract images
72
- image_paths = {
73
- "train": dl_manager.download_and_extract(_TRAIN_IMAGE_ARCHIVE_URL),
74
- "test": dl_manager.download_and_extract(_TEST_IMAGE_ARCHIVE_URL),
75
  }
76
-
77
- # Download annotations (extracted automatically)
78
- annotation_paths = {
79
- "train": dl_manager.download_and_extract(f"{_ANNOTATION_REPO}/train"),
80
- "test": dl_manager.download_and_extract(f"{_ANNOTATION_REPO}/test"),
81
  }
82
-
 
 
 
 
 
 
 
 
 
 
 
 
83
  return [
84
  datasets.SplitGenerator(
85
  name=datasets.Split.TRAIN,
86
  gen_kwargs={
87
- "images": dl_manager.iter_archive(image_paths["train"]),
88
- "annotation_path": annotation_paths["train"],
89
  },
90
  ),
91
  datasets.SplitGenerator(
92
  name=datasets.Split.TEST,
93
  gen_kwargs={
94
- "images": dl_manager.iter_archive(image_paths["test"]),
95
- "annotation_path": annotation_paths["test"],
96
  },
97
  ),
98
  ]
99
-
100
  def _generate_examples(self, images, annotation_path):
101
  """Generate dataset examples by matching images to their corresponding annotations."""
102
 
103
- # Load ALL annotations into memory before iterating over images
104
  annotations = {}
105
-
106
- for ann_file in os.listdir(annotation_path): # Iterate over all annotation files
 
 
107
  ann_path = os.path.join(annotation_path, ann_file)
108
- video_name = os.path.splitext(ann_file)[0] # Extract video folder name
109
-
110
  with open(ann_path, "r", encoding="utf-8") as f:
111
  for line in f:
112
  parts = line.strip().split()
113
  if len(parts) < 8:
114
  continue
115
-
116
  frame_id, track_id, class_name = parts[:3]
117
  bbox = list(map(float, parts[4:8]))
118
  class_id = _GT_OBJECT_CLASSES.get(class_name, -1)
119
  img_name = f"{frame_id}.jpg"
120
-
121
- # Store annotation in a simple dictionary
122
  key = f"{video_name}/{img_name}"
123
  if key not in annotations:
124
  annotations[key] = []
125
-
126
  annotations[key].append(
127
  {
128
  "bbox": bbox,
@@ -131,14 +141,14 @@ class EMT(datasets.GeneratorBasedBuilder):
131
  "class_name": class_name,
132
  }
133
  )
134
-
135
  # Yield dataset entries
136
  idx = 0
137
  for file_path, file_obj in images:
138
  img_name = os.path.basename(file_path)
139
- video_name = os.path.basename(os.path.dirname(file_path))
140
- key = f"{video_name}/{img_name}" # Match image to preloaded annotations
141
-
142
  if key in annotations:
143
  yield idx, {
144
  "image": {"path": file_path, "bytes": file_obj.read()},
 
67
 
68
  def _split_generators(self, dl_manager):
69
  """Download train/test images and annotations."""
70
+ image_urls = {
71
+ "train": _TRAIN_IMAGE_ARCHIVE_URL,
72
+ "test": _TEST_IMAGE_ARCHIVE_URL,
 
 
73
  }
74
+
75
+ # Download the individual annotation files for train and test
76
+ annotation_urls = {
77
+ "train": _ANNOTATION_REPO + "/train/",
78
+ "test": _ANNOTATION_REPO + "/test/",
79
  }
80
+
81
+ # Download image files
82
+ images = {
83
+ "train": dl_manager.iter_archive(image_urls["train"]),
84
+ "test": dl_manager.iter_archive(image_urls["test"]),
85
+ }
86
+
87
+ # Download annotation files
88
+ annotations = {
89
+ "train": dl_manager.download_and_extract(annotation_urls["train"]),
90
+ "test": dl_manager.download_and_extract(annotation_urls["test"]),
91
+ }
92
+
93
  return [
94
  datasets.SplitGenerator(
95
  name=datasets.Split.TRAIN,
96
  gen_kwargs={
97
+ "images": images["train"],
98
+ "annotation_path": annotations["train"],
99
  },
100
  ),
101
  datasets.SplitGenerator(
102
  name=datasets.Split.TEST,
103
  gen_kwargs={
104
+ "images": images["test"],
105
+ "annotation_path": annotations["test"],
106
  },
107
  ),
108
  ]
109
+
110
  def _generate_examples(self, images, annotation_path):
111
  """Generate dataset examples by matching images to their corresponding annotations."""
112
 
 
113
  annotations = {}
114
+
115
+ # Load all annotations into memory
116
+ for ann_file in os.listdir(annotation_path):
117
+ video_name = os.path.splitext(ann_file)[0] # Get video folder name
118
  ann_path = os.path.join(annotation_path, ann_file)
119
+
 
120
  with open(ann_path, "r", encoding="utf-8") as f:
121
  for line in f:
122
  parts = line.strip().split()
123
  if len(parts) < 8:
124
  continue
125
+
126
  frame_id, track_id, class_name = parts[:3]
127
  bbox = list(map(float, parts[4:8]))
128
  class_id = _GT_OBJECT_CLASSES.get(class_name, -1)
129
  img_name = f"{frame_id}.jpg"
130
+
131
+ # Store annotation in a dictionary
132
  key = f"{video_name}/{img_name}"
133
  if key not in annotations:
134
  annotations[key] = []
135
+
136
  annotations[key].append(
137
  {
138
  "bbox": bbox,
 
141
  "class_name": class_name,
142
  }
143
  )
144
+
145
  # Yield dataset entries
146
  idx = 0
147
  for file_path, file_obj in images:
148
  img_name = os.path.basename(file_path)
149
+ video_name = os.path.basename(os.path.dirname(file_path)) # Match the video folder
150
+ key = f"{video_name}/{img_name}"
151
+
152
  if key in annotations:
153
  yield idx, {
154
  "image": {"path": file_path, "bytes": file_obj.read()},