π [Fix] Json dataset file has 91 class problems
Browse files
yolo/tools/dataset_helper.py
CHANGED
|
@@ -6,6 +6,8 @@ from typing import Any, Dict, List, Optional, Tuple
|
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def find_labels_path(dataset_path: str, phase_name: str):
|
| 11 |
"""
|
|
@@ -22,8 +24,7 @@ def find_labels_path(dataset_path: str, phase_name: str):
|
|
| 22 |
|
| 23 |
txt_labels_path = path.join(dataset_path, "labels", phase_name)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
if path.isfile(json_labels_path) and False:
|
| 27 |
return json_labels_path, "json"
|
| 28 |
|
| 29 |
elif path.isdir(txt_labels_path):
|
|
@@ -47,12 +48,13 @@ def create_image_info_dict(labels_path: str) -> Tuple[Dict[str, List], Dict[str,
|
|
| 47 |
"""
|
| 48 |
with open(labels_path, "r") as file:
|
| 49 |
labels_data = json.load(file)
|
| 50 |
-
|
|
|
|
| 51 |
image_info_dict = {path.splitext(img["file_name"])[0]: img for img in labels_data["images"]}
|
| 52 |
return annotations_index, image_info_dict
|
| 53 |
|
| 54 |
|
| 55 |
-
def index_annotations_by_image(data: Dict[str, Any]):
|
| 56 |
"""
|
| 57 |
Use image index to lookup every annotations
|
| 58 |
Args:
|
|
@@ -68,6 +70,8 @@ def index_annotations_by_image(data: Dict[str, Any]):
|
|
| 68 |
if anno["iscrowd"]:
|
| 69 |
continue
|
| 70 |
image_id = anno["image_id"]
|
|
|
|
|
|
|
| 71 |
if image_id not in annotation_lookup:
|
| 72 |
annotation_lookup[image_id] = []
|
| 73 |
annotation_lookup[image_id].append(anno)
|
|
|
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
from yolo.utils.converter_json2txt import discretize_categories
|
| 10 |
+
|
| 11 |
|
| 12 |
def find_labels_path(dataset_path: str, phase_name: str):
|
| 13 |
"""
|
|
|
|
| 24 |
|
| 25 |
txt_labels_path = path.join(dataset_path, "labels", phase_name)
|
| 26 |
|
| 27 |
+
if path.isfile(json_labels_path):
|
|
|
|
| 28 |
return json_labels_path, "json"
|
| 29 |
|
| 30 |
elif path.isdir(txt_labels_path):
|
|
|
|
| 48 |
"""
|
| 49 |
with open(labels_path, "r") as file:
|
| 50 |
labels_data = json.load(file)
|
| 51 |
+
id_to_idx = discretize_categories(labels_data.get("categories", [])) if "categories" in labels_data else None
|
| 52 |
+
annotations_index = index_annotations_by_image(labels_data, id_to_idx) # check lookup is a good name?
|
| 53 |
image_info_dict = {path.splitext(img["file_name"])[0]: img for img in labels_data["images"]}
|
| 54 |
return annotations_index, image_info_dict
|
| 55 |
|
| 56 |
|
| 57 |
+
def index_annotations_by_image(data: Dict[str, Any], id_to_idx: Optional[Dict[int, int]]):
|
| 58 |
"""
|
| 59 |
Use image index to lookup every annotations
|
| 60 |
Args:
|
|
|
|
| 70 |
if anno["iscrowd"]:
|
| 71 |
continue
|
| 72 |
image_id = anno["image_id"]
|
| 73 |
+
if id_to_idx:
|
| 74 |
+
anno["category_id"] = id_to_idx[anno["category_id"]]
|
| 75 |
if image_id not in annotation_lookup:
|
| 76 |
annotation_lookup[image_id] = []
|
| 77 |
annotation_lookup[image_id].append(anno)
|
yolo/utils/converter_json2txt.py
CHANGED
|
@@ -86,5 +86,6 @@ def convert_annotations(json_file: str, output_dir: str) -> None:
|
|
| 86 |
process_annotations(image_annotations, image_info_dict, output_dir, id_to_idx)
|
| 87 |
|
| 88 |
|
| 89 |
-
|
| 90 |
-
convert_annotations("./data/coco/annotations/
|
|
|
|
|
|
| 86 |
process_annotations(image_annotations, image_info_dict, output_dir, id_to_idx)
|
| 87 |
|
| 88 |
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
convert_annotations("./data/coco/annotations/instances_train2017.json", "./data/coco/labels/train2017/")
|
| 91 |
+
convert_annotations("./data/coco/annotations/instances_val2017.json", "./data/coco/labels/val2017/")
|