KuAvLab commited on
Commit
75e71e7
·
verified ·
1 Parent(s): 8e3592a

Update EMT.py

Browse files
Files changed (1) hide show
  1. EMT.py +36 -38
EMT.py CHANGED
@@ -98,51 +98,49 @@ class EMT(datasets.GeneratorBasedBuilder):
98
 
99
  def _generate_examples(self, images, annotation_path):
100
  """Generate dataset examples by matching images to their corresponding annotations."""
 
 
101
  annotations = {}
102
-
103
- for file_path, file_obj in images:
104
- img_name = os.path.basename(file_path) # e.g., "000001.jpg"
105
- video_name = os.path.basename(os.path.dirname(file_path)) # e.g., "video_1112"
106
-
107
- ann_file = os.path.join(annotation_path, f"{video_name}.txt")
108
-
109
- if os.path.exists(ann_file):
110
- if ann_file not in annotations:
111
- annotations[ann_file] = {}
112
-
113
- if img_name not in annotations[ann_file]:
114
- annotations[ann_file][img_name] = []
115
-
116
- with open(ann_file, "r", encoding="utf-8") as f:
117
- for line in f:
118
- parts = line.strip().split()
119
- if len(parts) < 8:
120
- continue
121
-
122
- frame_id, track_id, class_name = parts[:3]
123
- bbox = list(map(float, parts[4:8]))
124
- class_id = _GT_OBJECT_CLASSES.get(class_name, -1)
125
-
126
- if f"{frame_id}.jpg" == img_name:
127
- annotations[ann_file][img_name].append(
128
- {
129
- "bbox": bbox,
130
- "class_id": class_id,
131
- "track_id": int(track_id),
132
- "class_name": class_name,
133
- }
134
- )
135
-
136
  # Yield dataset entries
137
  idx = 0
138
  for file_path, file_obj in images:
139
  img_name = os.path.basename(file_path)
140
  video_name = os.path.basename(os.path.dirname(file_path))
141
- ann_file = os.path.join(annotation_path, f"{video_name}.txt")
142
-
143
- if ann_file in annotations and img_name in annotations[ann_file]:
144
  yield idx, {
145
  "image": {"path": file_path, "bytes": file_obj.read()},
146
- "objects": annotations[ann_file][img_name],
147
  }
148
  idx += 1
 
98
 
99
  def _generate_examples(self, images, annotation_path):
100
  """Generate dataset examples by matching images to their corresponding annotations."""
101
+
102
+ # Load ALL annotations into memory before iterating over images
103
  annotations = {}
104
+
105
+ for ann_file in os.listdir(annotation_path): # Iterate over all annotation files
106
+ video_name = os.path.splitext(ann_file)[0] # Extract video folder name
107
+ ann_path = os.path.join(annotation_path, ann_file)
108
+
109
+ with open(ann_path, "r", encoding="utf-8") as f:
110
+ for line in f:
111
+ parts = line.strip().split()
112
+ if len(parts) < 8:
113
+ continue
114
+
115
+ frame_id, track_id, class_name = parts[:3]
116
+ bbox = list(map(float, parts[4:8]))
117
+ class_id = _GT_OBJECT_CLASSES.get(class_name, -1)
118
+ img_name = f"{frame_id}.jpg"
119
+
120
+ # Store annotation in a simple dictionary
121
+ key = f"{video_name}/{img_name}"
122
+ if key not in annotations:
123
+ annotations[key] = []
124
+
125
+ annotations[key].append(
126
+ {
127
+ "bbox": bbox,
128
+ "class_id": class_id,
129
+ "track_id": int(track_id),
130
+ "class_name": class_name,
131
+ }
132
+ )
133
+
 
 
 
 
134
  # Yield dataset entries
135
  idx = 0
136
  for file_path, file_obj in images:
137
  img_name = os.path.basename(file_path)
138
  video_name = os.path.basename(os.path.dirname(file_path))
139
+ key = f"{video_name}/{img_name}" # Match image to preloaded annotations
140
+
141
+ if key in annotations:
142
  yield idx, {
143
  "image": {"path": file_path, "bytes": file_obj.read()},
144
+ "objects": annotations[key],
145
  }
146
  idx += 1