Update testdata.py
Browse files- testdata.py +32 -47
testdata.py
CHANGED
|
@@ -18,6 +18,9 @@ import os
|
|
| 18 |
import glob as glob
|
| 19 |
from tqdm import tqdm
|
| 20 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 21 |
import datasets
|
| 22 |
|
| 23 |
|
|
@@ -84,10 +87,26 @@ class TestData(datasets.GeneratorBasedBuilder):
|
|
| 84 |
citation=_CITATION,
|
| 85 |
)
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def _generate_examples(self, data_dir, annot_dir):
|
| 88 |
image_dir = os.path.join(data_dir, "fisheye")
|
| 89 |
anno_dir = os.path.join(data_dir, "annotations")
|
| 90 |
files = []
|
|
|
|
|
|
|
| 91 |
for file_type in IMG_EXT:
|
| 92 |
files.extend(list(Path(image_dir).glob(f'**/*.{file_type}')))
|
| 93 |
|
|
@@ -95,55 +114,21 @@ class TestData(datasets.GeneratorBasedBuilder):
|
|
| 95 |
img_relative_path = image_path.relative_to(image_dir)
|
| 96 |
gt_pah = (Path(label_dir) / src_img_relative_path).with_suffix('.txt')
|
| 97 |
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if not line.endswith(".jpg"):
|
| 108 |
-
break
|
| 109 |
-
image_file_path = os.path.join(image_dir, line)
|
| 110 |
-
faces = []
|
| 111 |
-
if split != "test":
|
| 112 |
-
# Read number of bounding boxes
|
| 113 |
-
nbboxes = int(f.readline())
|
| 114 |
-
# Cases with 0 bounding boxes, still have one line with all zeros.
|
| 115 |
-
# So we have to read it and discard it.
|
| 116 |
-
if nbboxes == 0:
|
| 117 |
-
f.readline()
|
| 118 |
else:
|
| 119 |
-
|
| 120 |
-
line = f.readline()
|
| 121 |
-
line = line.rstrip()
|
| 122 |
-
line_split = line.split()
|
| 123 |
-
assert len(line_split) == 10, f"Cannot parse line: {line_split}"
|
| 124 |
-
line_parsed = [int(n) for n in line_split]
|
| 125 |
-
(
|
| 126 |
-
xmin,
|
| 127 |
-
ymin,
|
| 128 |
-
wbox,
|
| 129 |
-
hbox,
|
| 130 |
-
blur,
|
| 131 |
-
expression,
|
| 132 |
-
illumination,
|
| 133 |
-
invalid,
|
| 134 |
-
occlusion,
|
| 135 |
-
pose,
|
| 136 |
-
) = line_parsed
|
| 137 |
-
faces.append(
|
| 138 |
{
|
| 139 |
-
"bbox": [
|
| 140 |
-
"blur": blur,
|
| 141 |
-
"expression": expression,
|
| 142 |
-
"illumination": illumination,
|
| 143 |
-
"occlusion": occlusion,
|
| 144 |
-
"pose": pose,
|
| 145 |
-
"invalid": invalid,
|
| 146 |
}
|
| 147 |
)
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 18 |
import glob as glob
|
| 19 |
from tqdm import tqdm
|
| 20 |
from pathlib import Path
|
| 21 |
+
from typing import List
|
| 22 |
+
import re
|
| 23 |
+
from collections import defaultdict
|
| 24 |
import datasets
|
| 25 |
|
| 26 |
|
|
|
|
| 87 |
citation=_CITATION,
|
| 88 |
)
|
| 89 |
|
| 90 |
+
def parse_annotation(annotation_path: str) -> List[float]:
|
| 91 |
+
"""Read annotation from file."""
|
| 92 |
+
annotation = defaultdict(list)
|
| 93 |
+
with open(annotation_path, 'r') as f:
|
| 94 |
+
line = f.readline().strip()
|
| 95 |
+
while line:
|
| 96 |
+
assert re.match(r'^\d( [\d\.]+){4,5}$', line), 'Incorrect line: %s' % line
|
| 97 |
+
cls, cx, cy, w, h = line.split()[:5]
|
| 98 |
+
cls, cx, cy, w, h = int(cls), float(cx), float(cy), float(w), float(h)
|
| 99 |
+
x1, y1, x2, y2 = cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2
|
| 100 |
+
annotation[cls].append([x1, y1, x2, y2])
|
| 101 |
+
line = f.readline().strip()
|
| 102 |
+
return annotation
|
| 103 |
+
|
| 104 |
def _generate_examples(self, data_dir, annot_dir):
|
| 105 |
image_dir = os.path.join(data_dir, "fisheye")
|
| 106 |
anno_dir = os.path.join(data_dir, "annotations")
|
| 107 |
files = []
|
| 108 |
+
faces = []
|
| 109 |
+
plates = []
|
| 110 |
for file_type in IMG_EXT:
|
| 111 |
files.extend(list(Path(image_dir).glob(f'**/*.{file_type}')))
|
| 112 |
|
|
|
|
| 114 |
img_relative_path = image_path.relative_to(image_dir)
|
| 115 |
gt_pah = (Path(label_dir) / src_img_relative_path).with_suffix('.txt')
|
| 116 |
|
| 117 |
+
annotation = parse_annotation(gt_pah)
|
| 118 |
|
| 119 |
+
for cls, bboxes in annotation.items():
|
| 120 |
+
for x1, y1, x2, y2 in bboxes:
|
| 121 |
+
if cls == 0:
|
| 122 |
+
faces.append(
|
| 123 |
+
{
|
| 124 |
+
"bbox": [x1, y1, x2, y2]
|
| 125 |
+
}
|
| 126 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
else:
|
| 128 |
+
plates(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
{
|
| 130 |
+
"bbox": [x1, y1, x2, y2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
}
|
| 132 |
)
|
| 133 |
+
|
| 134 |
+
yield idx, {"image": image_path, "faces": faces, "plates": plates}
|