|
import os |
|
import cv2 |
|
import numpy as np |
|
import glob |
|
from detectron2.utils.visualizer import ColorMode, Visualizer |
|
from detectron2.data import MetadataCatalog |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADE20K_150_CATEGORIES = [ |
|
{"color": [250, 250, 250], "id": 0, "isthing": 0, "name": "background"}, |
|
{"color": [194, 255, 0], "id": 1, "isthing": 0, "name": "aeroplane"}, |
|
{"color": [6, 230, 230], "id": 2, "isthing": 0, "name": "bicycle"}, |
|
{"color": [80, 150, 20], "id": 3, "isthing": 0, "name": "bird"}, |
|
{"color": [4, 200, 3], "id": 4, "isthing": 0, "name": "boat"}, |
|
{"color": [255, 5, 153], "id": 5, "isthing": 0, "name": "bottle"}, |
|
{"color": [0, 255, 245], "id": 6, "isthing": 0, "name": "bus"}, |
|
{"color": [204, 5, 255], "id": 7, "isthing": 1, "name": "car"}, |
|
{"color": [255, 0, 102], "id": 8, "isthing": 1, "name": "cat "}, |
|
{"color": [4, 250, 7], "id": 9, "isthing": 0, "name": "chair"}, |
|
{"color": [224, 5, 255], "id": 10, "isthing": 1, "name": "cow"}, |
|
{"color": [235, 255, 7], "id": 11, "isthing": 0, "name": "diningtable"}, |
|
{"color": [150, 5, 61], "id": 12, "isthing": 1, "name": "dog"}, |
|
{"color": [0, 255, 112], "id": 13, "isthing": 0, "name": "horse"}, |
|
{"color": [8, 255, 51], "id": 14, "isthing": 1, "name": "motorbike"}, |
|
{"color": [255, 6, 82], "id": 15, "isthing": 1, "name": "person"}, |
|
{"color": [143, 255, 140], "id": 16, "isthing": 0, "name": "pottedplant"}, |
|
{"color": [204, 255, 4], "id": 17, "isthing": 0, "name": "sheep"}, |
|
{"color": [255, 51, 7], "id": 18, "isthing": 1, "name": "sofa"}, |
|
{"color": [204, 70, 3], "id": 19, "isthing": 1, "name": "train"}, |
|
{"color": [0, 102, 200], "id": 20, "isthing": 1, "name": "tvmonitor"}, |
|
|
|
] |
|
|
|
id_to_color = {category["id"]: category["color"] for category in ADE20K_150_CATEGORIES} |
|
|
|
def _get_ade20k_full_meta(): |
|
|
|
|
|
stuff_ids = [k["id"] for k in ADE20K_150_CATEGORIES] |
|
|
|
|
|
|
|
|
|
stuff_dataset_id_to_contiguous_id = {k: i for i, k in enumerate(stuff_ids)} |
|
stuff_classes = [k["name"].split(",")[0] for k in ADE20K_150_CATEGORIES] |
|
|
|
color = [k["color"] for k in ADE20K_150_CATEGORIES] |
|
|
|
ret = { |
|
"stuff_dataset_id_to_contiguous_id": stuff_dataset_id_to_contiguous_id, |
|
"stuff_classes": stuff_classes, |
|
"color":color |
|
} |
|
return ret |
|
|
|
def register_all_ade20k_full(root): |
|
root = os.path.join(root, "ADE20K_2021_17_01") |
|
meta = _get_ade20k_full_meta() |
|
for name, dirname in [("val", "validation")]: |
|
image_dir = os.path.join(root, "images_detectron2", dirname) |
|
gt_dir = os.path.join(root, "annotations_detectron2", dirname) |
|
name = f"sem_seg" |
|
MetadataCatalog.get(name).set( |
|
stuff_classes=meta["stuff_classes"][:], |
|
stuff_colors = meta["color"][:], |
|
image_root=image_dir, |
|
sem_seg_root=gt_dir, |
|
evaluator_type="sem_seg", |
|
ignore_label=65535, |
|
) |
|
|
|
def visualize_segmentation(image_path, mask_path, vis_file): |
|
_root = os.getenv("DETECTRON2_DATASETS", "datasets") |
|
register_all_ade20k_full(_root) |
|
mask = cv2.imread(mask_path)[:,:,0] |
|
image = cv2.imread(image_path) |
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
h,w = image.shape[:2] |
|
metadata = MetadataCatalog.get("sem_seg") |
|
visualizer = Visualizer(image, metadata, instance_mode=ColorMode.IMAGE) |
|
vis_output = visualizer.draw_sem_seg( |
|
mask, alpha=0.5 |
|
) |
|
vis_image = vis_output.get_image()[:, :, ::-1] |
|
vis_output.save(vis_file) |
|
return vis_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|