def gen_car_and_carplate_independently(fileitem):
    num_car = 0

    dom = xml.dom.minidom.parse(os.path.join(root_dir, fileitem))
    root = dom.documentElement

    # size
    size = root.getElementsByTagName('size')[0]
    width = int(size.getElementsByTagName('width')[0].childNodes[0].nodeValue)
    height = int(size.getElementsByTagName('height')[0].childNodes[0].nodeValue)
    depth = int(size.getElementsByTagName('depth')[0].childNodes[0].nodeValue)

    # 创建dom文档
    doc = Document()
    # 创建根节点
    annotation = doc.createElement('annotation')
    # 根节点插入dom树
    doc.appendChild(annotation)
    # folder
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder.appendChild(doc.createTextNode('car'))
    # filename
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename.appendChild(doc.createTextNode(fileitem.split('.')[0] + '.jpg'))
    # source
    source = doc.createElement('source')
    annotation.appendChild(source)
    database_ = doc.createElement('database')
    database_.appendChild(doc.createTextNode('car'))
    source.appendChild(database_)
    # 创建size节点
    size = doc.createElement('size')
    annotation.appendChild(size)
    width_ = doc.createElement('width')
    width_.appendChild(doc.createTextNode(str(width)))
    height_ = doc.createElement('height')
    height_.appendChild(doc.createTextNode(str(height)))
    depth_ = doc.createElement('depth')
    depth_.appendChild(doc.createTextNode(str(depth)))
    size.appendChild(width_)
    size.appendChild(height_)
    size.appendChild(depth_)
    # segmentation
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented.appendChild(doc.createTextNode(str(0)))

    # notice object and obj
    objects = root.getElementsByTagName('object')
    for index, obj in enumerate(objects):
        bndbox = obj.getElementsByTagName('bndbox')[0]
        try:
            car_type = obj.getElementsByTagName('type')[0].childNodes[0].nodeValue
        except:
            continue
        else:
            num_car += 1
            car_xmin = int(bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue)
            car_ymin = int(bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue)
            car_xmax = int(bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue)
            car_ymax = int(bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue)
            # 限制不超过图片上下界
            car_xmin = ex.limit_in_bounds(car_xmin, 1, width)
            car_ymin = ex.limit_in_bounds(car_ymin, 1, height)
            car_xmax = ex.limit_in_bounds(car_xmax, 1, width)
            car_ymax = ex.limit_in_bounds(car_ymax, 1, height)

            IsOccluded = int(obj.getElementsByTagName('occlusion')[0].childNodes[0].nodeValue)
            IsTruncated = int(obj.getElementsByTagName('truncated')[0].childNodes[0].nodeValue)
            IsDifficult = int(obj.getElementsByTagName('difficult')[0].childNodes[0].nodeValue)

            # car
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')

            tmp_type = None
            for k, v in merged_type.items():
                if car_type in v:
                    tmp_type = k
                    break

            name.appendChild(doc.createTextNode(tmp_type))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(IsOccluded)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(IsTruncated)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(IsDifficult)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(car_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(car_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(car_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(car_ymax)))
            bndbox.appendChild(ymax_)

    if num_car > 0:
        fp = open(os.path.join(target_dir, fileitem), 'w')
        doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
def gen_car_and_carplate_independently(dic):
    num_car = 0
    num_truck = 0
    num_pedestrian = 0
    num_biker = 0
    num_trafficlight = 0

    img_name = dic['img_name']
    height = dic['height']
    width = dic['width']
    channel = dic['channel']
    objs = dic['objs']

    # 创建dom文档
    doc = Document()
    # 创建根节点
    annotation = doc.createElement('annotation')
    # 根节点插入dom树
    doc.appendChild(annotation)
    # folder
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder.appendChild(doc.createTextNode('Udacity'))
    # filename
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename.appendChild(doc.createTextNode(img_name))
    # source
    source = doc.createElement('source')
    annotation.appendChild(source)
    database_ = doc.createElement('database')
    database_.appendChild(doc.createTextNode('Udacity objects'))
    source.appendChild(database_)
    # 创建size节点
    size = doc.createElement('size')
    annotation.appendChild(size)
    width_ = doc.createElement('width')
    width_.appendChild(doc.createTextNode(str(width)))
    height_ = doc.createElement('height')
    height_.appendChild(doc.createTextNode(str(height)))
    depth_ = doc.createElement('depth')
    depth_.appendChild(doc.createTextNode(str(channel)))
    size.appendChild(width_)
    size.appendChild(height_)
    size.appendChild(depth_)
    # segmentation
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented.appendChild(doc.createTextNode(str(0)))

    for obj in objs:
        obj_class = obj['Label']
        xmin = round(float(obj['xmin']))
        xmax = round(float(obj['xmax']))
        ymin = round(float(obj['ymin']))
        ymax = round(float(obj['ymax']))
        IsOccluded = int(obj['IsOccluded'])

        if need_car and obj_class == 'car':
            num_car += 1
            global Car_num
            Car_num += 1
            # 限制不超过图片上下界
            car_xmin = ex.limit_in_bounds(xmin, 1, width)
            car_ymin = ex.limit_in_bounds(ymin, 1, height)
            car_xmax = ex.limit_in_bounds(xmax, 1, width)
            car_ymax = ex.limit_in_bounds(ymax, 1, height)

            # car
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode('car'))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(IsOccluded)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(car_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(car_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(car_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(car_ymax)))
            bndbox.appendChild(ymax_)

        if need_truck and obj_class == 'truck':
            num_truck += 1
            global Truck_num
            Truck_num += 1
            # 限制不超过图片上下界
            truck_xmin = ex.limit_in_bounds(xmin, 1, width)
            truck_ymin = ex.limit_in_bounds(ymin, 1, height)
            truck_xmax = ex.limit_in_bounds(xmax, 1, width)
            truck_ymax = ex.limit_in_bounds(ymax, 1, height)

            # truck
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode("truck"))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(IsOccluded)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(truck_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(truck_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(truck_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(truck_ymax)))
            bndbox.appendChild(ymax_)

        if need_pedestrian and obj_class == 'pedestrian':
            num_pedestrian += 1
            global Pedestrian_num
            Pedestrian_num += 1
            # 限制不超过图片上下界
            pedestrian_xmin = ex.limit_in_bounds(xmin, 1, width)
            pedestrian_ymin = ex.limit_in_bounds(ymin, 1, height)
            pedestrian_xmax = ex.limit_in_bounds(xmax, 1, width)
            pedestrian_ymax = ex.limit_in_bounds(ymax, 1, height)

            # pedestrian
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode("pedestrian"))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(IsOccluded)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(pedestrian_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(pedestrian_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(pedestrian_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(pedestrian_ymax)))
            bndbox.appendChild(ymax_)

        if need_biker and obj_class == 'biker':
            num_biker += 1
            global Biker_num
            Biker_num += 1
            # 限制不超过图片上下界
            biker_xmin = ex.limit_in_bounds(xmin, 1, width)
            biker_ymin = ex.limit_in_bounds(ymin, 1, height)
            biker_xmax = ex.limit_in_bounds(xmax, 1, width)
            biker_ymax = ex.limit_in_bounds(ymax, 1, height)

            # biker
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode("biker"))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(IsOccluded)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(biker_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(biker_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(biker_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(biker_ymax)))
            bndbox.appendChild(ymax_)

        if need_trafficlight and obj_class == 'trafficLight':
            num_trafficlight += 1
            global TrafficLight_num
            TrafficLight_num += 1
            # 限制不超过图片上下界
            trafficlight_xmin = ex.limit_in_bounds(xmin, 1, width)
            trafficlight_ymin = ex.limit_in_bounds(ymin, 1, height)
            trafficlight_xmax = ex.limit_in_bounds(xmax, 1, width)
            trafficlight_ymax = ex.limit_in_bounds(ymax, 1, height)

            # trafficlight
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode("trafficlight"))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(IsOccluded)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(trafficlight_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(trafficlight_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(trafficlight_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(trafficlight_ymax)))
            bndbox.appendChild(ymax_)

    # 如果车辆,卡车或行人数量大于0,才写入文件 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (need_car and num_car > 0) or (need_truck and num_truck > 0) or (need_pedestrian and num_pedestrian > 0)\
            or (need_biker and num_biker > 0) or (need_trafficlight and num_trafficlight > 0):
        fp = open(os.path.join(target_dir,
                               img_name.split('.')[0] + '.xml'), 'w')
        doc.writexml(fp,
                     indent='\t',
                     addindent='\t',
                     newl='\n',
                     encoding="utf-8")
Example #3
0
def gen_car_and_carplate_independently(dic):
    img_name = dic['img_name']
    height = dic['height']
    width = dic['width']
    channel = dic['channel']
    xmin = dic['xmin']
    ymin = dic['ymin']
    xmax = dic['xmax']
    ymax = dic['ymax']

    # 创建dom文档
    doc = Document()
    # 创建根节点
    annotation = doc.createElement('annotation')
    # 根节点插入dom树
    doc.appendChild(annotation)
    # folder
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder.appendChild(doc.createTextNode('AOLP_LE'))
    # filename
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename.appendChild(doc.createTextNode(img_name))
    # source
    source = doc.createElement('source')
    annotation.appendChild(source)
    database_ = doc.createElement('database')
    database_.appendChild(doc.createTextNode('The AOLP Law Enforcement'))
    source.appendChild(database_)
    # 创建size节点
    size = doc.createElement('size')
    annotation.appendChild(size)
    width_ = doc.createElement('width')
    width_.appendChild(doc.createTextNode(str(width)))
    height_ = doc.createElement('height')
    height_.appendChild(doc.createTextNode(str(height)))
    depth_ = doc.createElement('depth')
    depth_.appendChild(doc.createTextNode(str(channel)))
    size.appendChild(width_)
    size.appendChild(height_)
    size.appendChild(depth_)
    # segmentation
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented.appendChild(doc.createTextNode(str(0)))

    # 如果标注出现错误,比如xmin大于xmax,直接跳过,并且不保存xml
    need_save = True
    for i in range(len(xmin)):
        if xmin[i] > xmax[i]:
            print(img_name)
            xmin[i], xmax[i] = xmax[i], xmin[i]
        #     need_save = False
        #     continue
        if ymin[i] > ymax[i]:
            print(img_name)
            ymin[i], ymax[i] = ymax[i], ymin[i]
        #     need_save = False
        #     continue

        # 限制不超过图片上下界
        carplate_xmin = ex.limit_in_bounds(xmin[i], 1, width)
        carplate_ymin = ex.limit_in_bounds(ymin[i], 1, height)
        carplate_xmax = ex.limit_in_bounds(xmax[i], 1, width)
        carplate_ymax = ex.limit_in_bounds(ymax[i], 1, height)

        # carplate
        object = doc.createElement('object')
        annotation.appendChild(object)
        name = doc.createElement('name')
        name.appendChild(doc.createTextNode("carplate"))
        object.appendChild(name)
        # pose
        pose_ = doc.createElement('pose')
        pose_.appendChild(doc.createTextNode('Unspecified'))
        object.appendChild(pose_)
        # truncated
        truncated_ = doc.createElement('truncated')
        truncated_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(truncated_)
        # difficult
        difficult_ = doc.createElement('difficult')
        difficult_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(difficult_)
        # the bndbox
        bndbox = doc.createElement('bndbox')
        object.appendChild(bndbox)
        xmin_ = doc.createElement('xmin')
        xmin_.appendChild(doc.createTextNode(str(carplate_xmin)))
        bndbox.appendChild(xmin_)
        ymin_ = doc.createElement('ymin')
        ymin_.appendChild(doc.createTextNode(str(carplate_ymin)))
        bndbox.appendChild(ymin_)
        xmax_ = doc.createElement('xmax')
        xmax_.appendChild(doc.createTextNode(str(carplate_xmax)))
        bndbox.appendChild(xmax_)
        ymax_ = doc.createElement('ymax')
        ymax_.appendChild(doc.createTextNode(str(carplate_ymax)))
        bndbox.appendChild(ymax_)

    if need_save:
        fp = open(os.path.join(target_anno,
                               str(dic['img_index']) + '.xml'), 'w')
        doc.writexml(fp,
                     indent='\t',
                     addindent='\t',
                     newl='\n',
                     encoding="utf-8")