Ejemplo n.º 1
0
    def __init__(self, cfgs):
        self.cfgs = cfgs
        label_map = LabelMap(cfgs)
        self.name_label_map, self.label_name_map = label_map.name2label(), label_map.label2name()

        self.NOT_DRAW_BOXES = 0
        self.ONLY_DRAW_BOXES = -1
        self.ONLY_DRAW_BOXES_WITH_SCORES = -2

        self.STANDARD_COLORS = [
            'AliceBlue', 'Chartreuse', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque',
            'BlanchedAlmond', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite',
            'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan',
            'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange',
            'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet',
            'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite',
            'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod',
            'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki',
            'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue',
            'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey',
            'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue',
            'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime',
            'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid',
            'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen',
            'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin',
            'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed',
            'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed',
            'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple',
            'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown',
            'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
            'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow',
            'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White',
            'WhiteSmoke', 'Yellow', 'YellowGreen', 'LightBlue', 'LightGreen'
        ]
        self.FONT = ImageFont.load_default()
def read_xml_gtbox_and_label(xml_path):
    """
    :param xml_path: the path of voc xml
    :return: a list contains gtboxes and labels, shape is [num_of_gtboxes, 9],
           and has [x1, y1, x2, y2, x3, y3, x4, y4, label] in a per row
    """
    label_map = LabelMap(cfgs)
    tree = ET.parse(xml_path)
    root = tree.getroot()
    img_width = None
    img_height = None
    box_list = []
    for child_of_root in root:
        # if child_of_root.tag == 'filename':
        #     assert child_of_root.text == xml_path.split('/')[-1].split('.')[0] \
        #                                  + FLAGS.img_format, 'xml_name and img_name cannot match'

        if child_of_root.tag == 'size':
            for child_item in child_of_root:
                if child_item.tag == 'width':
                    img_width = int(child_item.text)
                if child_item.tag == 'height':
                    img_height = int(child_item.text)

        if child_of_root.tag == 'object':
            label = None
            for child_item in child_of_root:
                if child_item.tag == 'name':
                    label = label_map.name2label()[child_item.text]
                if child_item.tag == 'bndbox':
                    tmp_box = []
                    for node in child_item:
                        tmp_box.append(float(node.text))
                    assert label is not None, 'label is none, error'
                    tmp_box.append(label)
                    box_list.append(tmp_box)

    gtbox_label = np.array(box_list, dtype=np.int32)

    return img_height, img_width, gtbox_label
Ejemplo n.º 3
0
def WriterXMLFiles(filename, path, gtbox_label_list, w, h, d):

    # dict_box[filename]=json_dict[filename]
    doc = xml.dom.minidom.Document()
    root = doc.createElement('annotation')
    doc.appendChild(root)

    foldername = doc.createElement("folder")
    foldername.appendChild(doc.createTextNode("JPEGImages"))
    root.appendChild(foldername)

    nodeFilename = doc.createElement('filename')
    nodeFilename.appendChild(doc.createTextNode(filename))
    root.appendChild(nodeFilename)

    pathname = doc.createElement("path")
    pathname.appendChild(doc.createTextNode("xxxx"))
    root.appendChild(pathname)

    sourcename = doc.createElement("source")

    databasename = doc.createElement("database")
    databasename.appendChild(doc.createTextNode("Unknown"))
    sourcename.appendChild(databasename)

    annotationname = doc.createElement("annotation")
    annotationname.appendChild(doc.createTextNode("xxx"))
    sourcename.appendChild(annotationname)

    imagename = doc.createElement("image")
    imagename.appendChild(doc.createTextNode("xxx"))
    sourcename.appendChild(imagename)

    flickridname = doc.createElement("flickrid")
    flickridname.appendChild(doc.createTextNode("0"))
    sourcename.appendChild(flickridname)

    root.appendChild(sourcename)

    nodesize = doc.createElement('size')
    nodewidth = doc.createElement('width')
    nodewidth.appendChild(doc.createTextNode(str(w)))
    nodesize.appendChild(nodewidth)
    nodeheight = doc.createElement('height')
    nodeheight.appendChild(doc.createTextNode(str(h)))
    nodesize.appendChild(nodeheight)
    nodedepth = doc.createElement('depth')
    nodedepth.appendChild(doc.createTextNode(str(d)))
    nodesize.appendChild(nodedepth)
    root.appendChild(nodesize)

    segname = doc.createElement("segmented")
    segname.appendChild(doc.createTextNode("0"))
    root.appendChild(segname)

    label_name_map = LabelMap(cfgs).label2name()

    for gtbox_label in gtbox_label_list:

        nodeobject = doc.createElement('object')
        nodename = doc.createElement('name')
        nodename.appendChild(
            doc.createTextNode(str(label_name_map[gtbox_label[-1]])))
        nodeobject.appendChild(nodename)

        nodetruncated = doc.createElement('truncated')
        nodetruncated.appendChild(doc.createTextNode(str(0)))
        nodeobject.appendChild(nodetruncated)

        nodedifficult = doc.createElement('difficult')
        nodedifficult.appendChild(doc.createTextNode(str(0)))
        nodeobject.appendChild(nodedifficult)

        nodepose = doc.createElement('pose')
        nodepose.appendChild(doc.createTextNode('xxx'))
        nodeobject.appendChild(nodepose)

        nodebndbox = doc.createElement('bndbox')
        nodex1 = doc.createElement('x1')
        nodex1.appendChild(doc.createTextNode(str(gtbox_label[0])))
        nodebndbox.appendChild(nodex1)
        nodey1 = doc.createElement('y1')
        nodey1.appendChild(doc.createTextNode(str(gtbox_label[1])))
        nodebndbox.appendChild(nodey1)
        nodex2 = doc.createElement('x2')
        nodex2.appendChild(doc.createTextNode(str(gtbox_label[2])))
        nodebndbox.appendChild(nodex2)
        nodey2 = doc.createElement('y2')
        nodey2.appendChild(doc.createTextNode(str(gtbox_label[3])))
        nodebndbox.appendChild(nodey2)
        nodex3 = doc.createElement('x3')
        nodex3.appendChild(doc.createTextNode(str(gtbox_label[4])))
        nodebndbox.appendChild(nodex3)
        nodey3 = doc.createElement('y3')
        nodey3.appendChild(doc.createTextNode(str(gtbox_label[5])))
        nodebndbox.appendChild(nodey3)
        nodex4 = doc.createElement('x4')
        nodex4.appendChild(doc.createTextNode(str(gtbox_label[6])))
        nodebndbox.appendChild(nodex4)
        nodey4 = doc.createElement('y4')
        nodey4.appendChild(doc.createTextNode(str(gtbox_label[7])))
        nodebndbox.appendChild(nodey4)

        # ang = doc.createElement('angle')
        # ang.appendChild(doc.createTextNode(str(angle)))
        # nodebndbox.appendChild(ang)
        nodeobject.appendChild(nodebndbox)
        root.appendChild(nodeobject)
    fp = open(os.path.join(path, filename), 'w')
    doc.writexml(fp, indent='\n')
    fp.close()
 def __init__(self, cfgs):
     self.cfgs = cfgs
     self.args = parse_args()
     label_map = LabelMap(cfgs)
     self.name_label_map, self.label_name_map = label_map.name2label(
     ), label_map.label2name()
Ejemplo n.º 5
0
def convert_pascal_to_tfrecord():
    """convert txt (points + label format) to tfrecord
        VOC_dir:
            --trainval
                --images
                --labels
            --test
                --images
                --labels
    """
    allNeedConvert = ['trainval', 'test']
    for train_or_test in allNeedConvert:

        label_Path = FLAGS.VOC_dir + "{}/".format(
            train_or_test) + FLAGS.txt_dir
        image_path = FLAGS.VOC_dir + "{}/".format(
            train_or_test) + FLAGS.image_dir
        # xml_path = os.path.join(FLAGS.VOC_dir, FLAGS.xml_dir)
        # image_path = os.path.join(FLAGS.VOC_dir, FLAGS.image_dir)
        print(image_path)
        save_path = os.path.join(
            FLAGS.save_dir, FLAGS.dataset + '_' + train_or_test + '.tfrecord')
        mkdir_or_exist(FLAGS.save_dir)

        writer = tf.python_io.TFRecordWriter(path=save_path)

        txtFullPathList, _ = get_files(label_Path, _ends=['*.txt'])
        for count, txt in enumerate(txtFullPathList):
            (txtPath, tmpTxtName) = os.path.split(txt)
            (txt_name, extension) = os.path.splitext(tmpTxtName)

            img_name = txt_name + FLAGS.img_format
            img_path = image_path + '/' + img_name

            if not os.path.exists(img_path):
                print('{} is not exist!'.format(img_path))
                continue
            ships = simpletxt_parse(txt, space=' ', boxType='points')
            label_map = LabelMap(cfgs)
            # print(label_map.name2label())
            gtboxes_and_label = []
            for ship in ships:
                gtbox_label = [0, 0, 0, 0, 0, 0, 0, 0, 0]
                gtbox_label[:8] = ship['points']
                gtbox_label[8] = label_map.name2label()[ship['label']]
                gtboxes_and_label.append(gtbox_label)
            img_height, img_width = 1024, 1024
            gtboxes_and_label = np.array(gtboxes_and_label, dtype=np.int32)

            img = cv2.imread(img_path)[:, :, ::-1]
            img = np.array(img, dtype=np.int32)
            img_raw = img.tobytes()
            num_objects = gtboxes_and_label.shape[0]
            # shape = gtboxes_and_label.shape
            # gtboxes_and_label=gtboxes_and_label.tobytes()
            feature = tf.train.Features(
                feature={
                    # do not need encode() in linux
                    'img_name':
                    _bytes_feature(img_name.encode()),
                    # 'img_name': _bytes_feature(img_name),
                    'img_height':
                    _int64_feature(img_height),
                    'img_width':
                    _int64_feature(img_width),
                    'img':
                    tf.train.Feature(bytes_list=tf.train.BytesList(
                        value=[img_raw])),
                    'num_objects':
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=[num_objects])),
                    'gtboxes_and_label':
                    _bytes_feature(gtboxes_and_label.tostring())
                })
            example = tf.train.Example(features=feature)

            writer.write(example.SerializeToString())

        print('Conversion is complete!save path:{}'.format(
            train_or_test, save_path))
        writer.close()
 def __init__(self, cfgs):
     self.cfgs = cfgs
     label_map = LabelMap(cfgs)
     self.name2label = label_map.name2label()