IDD_labeling.py 4.99 KB
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
import os
import numpy as np
import cv2
from PIL import Image
from paddleseg import utils
import xml.dom.minidom


def mkdir(path):
    sub_dir = os.path.dirname(path)
    if not os.path.exists(sub_dir):
        os.makedirs(sub_dir)


def get_image_list(image_path):
    """Get image list"""
    valid_suffix = [
        '.JPEG', '.jpeg', '.JPG', '.jpg', '.BMP', '.bmp', '.PNG', '.png'
    ]
    image_list = []
    image_dir = None
    if os.path.isfile(image_path):
        if os.path.splitext(image_path)[-1] in valid_suffix:
            image_list.append(image_path)
    elif os.path.isdir(image_path):
        image_dir = image_path
        for root, dirs, files in os.walk(image_path):
            for f in files:
                if '.ipynb_checkpoints' in root:
                    continue
                if os.path.splitext(f)[-1] in valid_suffix:
                    image_list.append(os.path.join(root.split('/')[-1], f))
    else:
        raise FileNotFoundError(
            '`--image_path` is not found. it should be an image file or a directory including images'
        )

    if len(image_list) == 0:
        raise RuntimeError('There are not image file in `--image_path`')

    return image_list, image_dir


def refine_pred():
    image_list, image_dir = get_image_list(
        'detection_out/pseudo_color_prediction')
    for ii in image_list:
        name_pred = 'detection_out/pseudo_color_prediction/' + ii
        name_label = 'data/IDD_Detection/Annotations/all/' + ii[:-3] + 'xml'
        pred = np.array(Image.open(name_pred)).astype(np.float32)
        if not os.path.exists(name_label):
            pred_mask = utils.visualize.get_pseudo_color_map(pred)
            pred_saved_path = 'detect_out/pred_refine/' + ii
            mkdir(pred_saved_path)
            pred_mask.save(pred_saved_path)
            continue

        dom = xml.dom.minidom.parse(name_label)
        root = dom.documentElement
        objects = root.getElementsByTagName("object")
        for item in objects:
            name = item.getElementsByTagName("name")[0]
            if name.firstChild.data == 'traffic sign' or name.firstChild.data == 'traffic light':
                print(ii)
                xmin = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'xmin')[0].firstChild.data)
                ymin = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'ymin')[0].firstChild.data)
                xmax = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'xmax')[0].firstChild.data)
                ymax = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'ymax')[0].firstChild.data)
                if name.firstChild.data == 'traffic sign':
                    pred[ymin:ymax, xmin:xmax] = 18
                elif name.firstChild.data == 'traffic light':
                    pred[ymin:ymax, xmin:xmax] = 19

        pred_mask = utils.visualize.get_pseudo_color_map(pred)
        pred_saved_path = 'detect_out/pred_refine/' + ii
        mkdir(pred_saved_path)
        pred_mask.save(pred_saved_path)


def test():
    path = '/Users/liliulei/Downloads/IDD_Detection/JPEGImages/frontNear/'
    image_list, image_dir = get_image_list(path)

    for ii in image_list:
        name_xml = '/Users/liliulei/Downloads/IDD_Detection/Annotations/frontNear/' + ii[:
                                                                                         -3] + 'xml'
        image = cv2.imread(path + ii)
        # print(image.shape)
        (h, w) = image.shape[0:2]

        pred = np.zeros_like(image)

        dom = xml.dom.minidom.parse(name_xml)
        root = dom.documentElement
        objects = root.getElementsByTagName("object")
        for item in objects:
            name = item.getElementsByTagName("name")[0]
            print(name.firstChild.data)
            if name.firstChild.data == 'traffic sign' or name.firstChild.data == 'traffic light':
                xmin = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'xmin')[0].firstChild.data)
                ymin = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'ymin')[0].firstChild.data)
                xmax = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'xmax')[0].firstChild.data)
                ymax = int(
                    item.getElementsByTagName('bndbox')[0].getElementsByTagName(
                        'ymax')[0].firstChild.data)
                if name.firstChild.data == 'traffic sign':
                    pred[ymin:ymax, xmin:xmax, 0] = 255
                elif name.firstChild.data == 'traffic light':
                    pred[ymin:ymax, xmin:xmax, 1] = 255

        new_im = image * 0.5 + pred * 0.5

        cv2.imwrite(ii.split('/')[-1][:-3] + 'png', new_im)


refine_pred()