Update EMT.py
Browse files
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
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 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 |
-
|
| 142 |
-
|
| 143 |
-
if
|
| 144 |
yield idx, {
|
| 145 |
"image": {"path": file_path, "bytes": file_obj.read()},
|
| 146 |
-
"objects": annotations[
|
| 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
|