Exemplo n.º 1
0
def read_kitti_anno(label_file, detect_truck):

    labels = [line.rstrip().split(' ') for line in open(label_file)]
    rect_list = []
    for label in labels:
		#create the label of vehicles
        if not (label[0] == 'Car' or label[0] == 'Van' or
                label[0] == 'Truck' or label[0] == 'DontCare'):
            continue
        notruck = not detect_truck
        if notruck and label[0] == 'Truck':
            continue
        if label[0] == 'DontCare':
            class_id = -1
        else:
            class_id = 1
        #create rectangle
		object_rect = AnnoLib.AnnoRect(
            x1=float(label[4]), y1=float(label[5]),
            x2=float(label[6]), y2=float(label[7]))
        #check the condition
		assert object_rect.x1 < object_rect.x2
        assert object_rect.y1 < object_rect.y2
        object_rect.classID = class_id
		#append to the list of the rresult
        rect_list.append(object_rect)
Exemplo n.º 2
0
def read_daimler_anno(label_file, detect_truck):
    """ Reads a daimler cyclist annotation file.

    Args:
    label_file: Path to file

    Returns:
      Lists of rectangels: .
    """
    label_data = json.load(open(label_file))
    labels = [item for item in label_data['children']]
    rect_list = []
    for label in labels:
        if not (label['identity'] == 'pedestrian' or label['identity']
                == 'cyclist' or label['identity'] == 'wheelchairuser'
                or label['identity'] == 'motorcyclist' or label['identity']
                == 'tricyclist' or label['identity'] == 'mopedrider'):
            continue
        notruck = not detect_truck
        if notruck and label['identity'] == 'Truck':
            continue
        if label['identity'] == 'DontCare':
            class_id = -1
        else:
            class_id = 1
        object_rect = AnnoLib.AnnoRect(x1=float(label['mincol']),
                                       y1=float(label['minrow']),
                                       x2=float(label['maxcol']),
                                       y2=float(label['maxrow']))
        assert object_rect.x1 < object_rect.x2
        assert object_rect.y1 < object_rect.y2
        object_rect.classID = class_id
        rect_list.append(object_rect)

    return rect_list
Exemplo n.º 3
0
def add_rectangles(H,
                   orig_image,
                   confidences,
                   boxes,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.1,
                   show_removed=True,
                   tau=0.25,
                   show_suppressed=True):
    image = np.copy(orig_image[0])
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes,
                         (-1, H["grid_height"], H["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences,
        (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes']))
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])]
                 for _ in range(H["grid_height"])]
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                conf = np.max(confidences_r[0, y, x, n, 1:])
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    all_rects_r = [r for row in all_rects for cell in row for r in cell]
    if use_stitching:
        from stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    pairs = [(acc_rects, (0, 255, 0))]
    if show_suppressed:
        pairs.append((all_rects_r, (255, 0, 0)))
    for rect_set, color in pairs:
        for rect in rect_set:
            if rect.confidence > min_conf:
                cv2.rectangle(image, (rect.cx - int(rect.width / 2),
                                      rect.cy - int(rect.height / 2)),
                              (rect.cx + int(rect.width / 2),
                               rect.cy + int(rect.height / 2)), color, 2)

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width / 2.
        r.x2 = rect.cx + rect.width / 2.
        r.y1 = rect.cy - rect.height / 2.
        r.y2 = rect.cy + rect.height / 2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
def read_kitti_anno(label_file):
    """ Reads a kitti annotation file.

    Args:
    label_file: Path to file

    Returns:
      Lists of rectangels: Cars and don't care area.
    """
    labels = [line.rstrip().split(' ') for line in open(label_file)]
    rect_list = []
    for label in labels:
        if not (label[0] == 'Car' or label[0] == 'Van'
                or label[0] == 'DontCare'):
            continue
        if label[0] == 'DontCare':
            class_id = -1
        else:
            class_id = 1
        object_rect = AnnoLib.AnnoRect(x1=float(label[4]),
                                       y1=float(label[5]),
                                       x2=float(label[6]),
                                       y2=float(label[7]))
        assert object_rect.x1 < object_rect.x2
        assert object_rect.y1 < object_rect.y2
        object_rect.classID = class_id
        rect_list.append(object_rect)

    return rect_list
Exemplo n.º 5
0
def add_rectangles(H,
                   orig_image,
                   confidences,
                   boxes,
                   arch,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.5,
                   tau=0.25):
    from utils.rect import Rect
    from utils.stitch_wrapper import stitch_rects
    import numpy as np
    image = np.copy(orig_image[0])
    boxes_r = np.reshape(
        boxes, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 2))
    cell_pix_size = H['arch']['region_size']
    all_rects = [[[] for _ in range(arch["grid_width"])]
                 for _ in range(arch["grid_height"])]
    for n in range(0, H['arch']['rnn_len']):
        for y in range(arch["grid_height"]):
            for x in range(arch["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                conf = confidences_r[0, y, x, n, 1]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                h = max(1, bbox[3])
                w = max(1, bbox[2])
                #w = h * 0.4
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    if use_stitching:
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = [
            r for row in all_rects for cell in row for r in cell
            if r.confidence > 0.1
        ]

    for rect in acc_rects:
        if rect.confidence > min_conf:
            cv2.rectangle(image, (rect.cx - int(rect.width / 2),
                                  rect.cy - int(rect.height / 2)),
                          (rect.cx + int(rect.width / 2),
                           rect.cy + int(rect.height / 2)), (0, 255, 0), 2)

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width / 2.
        r.x2 = rect.cx + rect.width / 2.
        r.y1 = rect.cy - rect.height / 2.
        r.y2 = rect.cy + rect.height / 2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
Exemplo n.º 6
0
def calculate_medium_box(boxes):
    conf_sum = reduce(lambda t, b: t + b.score, boxes, 0)
    aggregation = {}
    for name in ['x1', 'y1', 'x2', 'y2']:
        aggregation[name] = reduce(lambda t, b: t+b.__dict__[name]*b.score, boxes, 0) / conf_sum

    new_box = al.AnnoRect(**aggregation)
    new_box.classID = boxes[0].classID
    new_box.score = conf_sum / len(boxes)
    return new_box
Exemplo n.º 7
0
def read_kitti_anno(label_file, calib_file, detect_truck):
    """ Reads a kitti annotation file.

    Args:
    label_file: Path to file

    Returns:
      Lists of rectangels: Cars and don't care area.
    """
    labels = [line.rstrip().split(' ') for line in open(label_file)]

    label_file_split = label_file.rstrip().split('/')
    index = label_file_split[-1].split('.')[0]
    #import pdb
    #pdb.set_trace()
    calibs = [line.rstrip().split(' ') for line in open(calib_file)]
    assert calibs[2][0] == 'P2:'
    calib = np.reshape(calibs[2][1:], (3, 4)).astype(np.float32)
    calib_pinv = np.linalg.pinv(calib)
    rect_list = []
    for label in labels:
        if not (label[0] == 'Car' or label[0] == 'Van' or label[0] == 'Truck'
                or label[0] == 'DontCare'):
            continue
        notruck = not detect_truck
        if notruck and label[0] == 'Truck':
            continue
        if label[0] == 'DontCare':
            class_id = -1
        else:
            class_id = 1
        object_rect = AnnoLib.AnnoRect(x1=float(label[4]),
                                       y1=float(label[5]),
                                       x2=float(label[6]),
                                       y2=float(label[7]),
                                       height=float(label[8]),
                                       width=float(label[9]),
                                       length=float(label[10]),
                                       x=float(label[11]),
                                       y=float(label[12]),
                                       z=float(label[13]),
                                       alpha=float(label[14]),
                                       calib=calib,
                                       calib_pinv=calib_pinv)
        assert object_rect.x1 < object_rect.x2
        assert object_rect.y1 < object_rect.y2
        object_rect.classID = class_id

        view_angle = np.arctan2(object_rect.z_3d, object_rect.x_3d)
        object_rect.alpha += view_angle - np.pi * 0.5

        rect_list.append(object_rect)
    return rect_list
Exemplo n.º 8
0
def add_rectangles(orig_image,
                   confidences,
                   boxes,
                   arch,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.5):
    image = np.copy(orig_image[0])
    num_cells = arch["grid_height"] * arch["grid_width"]
    boxes_r = np.reshape(
        boxes, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 2))
    cell_pix_size = 32
    all_rects = [[[] for _ in range(arch["grid_width"])]
                 for _ in range(arch["grid_height"])]
    for n in range(rnn_len):
        for y in range(arch["grid_height"]):
            for x in range(arch["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                conf = confidences_r[0, y, x, n, 1]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    if use_stitching:
        from stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects)
    else:
        acc_rects = [r for row in all_rects for cell in row for r in cell]

    for rect in acc_rects:
        if rect.confidence > min_conf:
            cv2.rectangle(image, (rect.cx - int(rect.width / 2),
                                  rect.cy - int(rect.height / 2)),
                          (rect.cx + int(rect.width / 2),
                           rect.cy + int(rect.height / 2)), (0, 255, 0), 2)

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width / 2.
        r.x2 = rect.cx + rect.width / 2.
        r.y1 = rect.cy - rect.height / 2.
        r.y2 = rect.cy + rect.height / 2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
Exemplo n.º 9
0
def get_multiclass_rectangles(H, confidences, boxes, rnn_len):
    boxes_r = np.reshape(boxes, (-1,
                                 H["grid_height"],
                                 H["grid_width"],
                                 rnn_len,
                                 4))
    confidences_r = np.reshape(confidences, (-1,
                                             H["grid_height"],
                                             H["grid_width"],
                                             rnn_len,
                                             H['num_classes']))
    # print "boxes_r shape" + str(boxes_r.shape)
    # print "confidences" + str(confidences.shape)
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])]
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                # conf = np.max(confidences_r[0, y, x, n, 1:])
                index, conf = get_silhouette_confidence(confidences_r[0, y, x, n, 1:])
                # print index, conf
                # print np.max(confidences_r[0, y, x, n, 1:])
                # print "conf" + str(conf)
                # print "conf" + str(confidences_r[0, y, x, n, 1:])
                new_rect=multiclass_rectangle.Rectangle_Multiclass()
                new_rect.set_unlabeled_rect(abs_cx,abs_cy,w,h,conf)
                all_rects[y][x].append(new_rect)
    # print "confidences_r" + str(confidences_r.shape)

    all_rects_r = [r for row in all_rects for cell in row for r in cell]
    min_conf = get_higher_confidence(all_rects_r)
    acc_rects=[rect for rect in all_rects_r if rect.true_confidence>min_conf]
    rects = []
    for rect in all_rects_r:
    	if rect.true_confidence>min_conf:
	        r = al.AnnoRect()
	        r.x1 = rect.cx - rect.width/2.
	        r.x2 = rect.cx + rect.width/2.
	        r.y1 = rect.cy - rect.height/2.
	        r.y2 = rect.cy + rect.height/2.
	        r.score = rect.true_confidence
	        r.silhouetteID=rect.label
	        rects.append(r)
    print len(rects),len(acc_rects)
    return rects, acc_rects
Exemplo n.º 10
0
def get_cell_grid(cell_width, cell_height, region_size):
    cell_regions = []
    for iy in range(cell_height):
        for ix in range(cell_width):
            cidx = iy * cell_width + ix
            ox = (ix + 0.5) * region_size
            oy = (iy + 0.5) * region_size

            r = al.AnnoRect(ox - 0.5 * region_size, oy - 0.5 * region_size,
                            ox + 0.5 * region_size, oy + 0.5 * region_size)
            r.track_id = cidx

            cell_regions.append(r)

    return cell_regions
Exemplo n.º 11
0
def read_kitti_anno(label_file, detect_truck):
    """ Reads a kitti annotation file.

    Args:
    label_file: Path to file

    Returns:
      Lists of rectangels: Cars and don't care area.
    """
    labels = [line.rstrip().split(' ') for line in open(label_file)]
    rect_list = []
    for label in labels:
        if not (label[0] == 'Car' or label[0] == 'Van' or label[0] == 'Truck'
                or label[0] == 'DontCare'):
            continue
        #notruck = not detect_truck
        notruck = 0
        if notruck and label[0] == 'Truck':
            continue
        if label[0] == 'DontCare':
            class_id = -1
        else:
            class_id = 1
        bbox = [
            float(label[1]),
            float(label[2]),
            float(label[3]),
            float(label[4])
        ]
        if bbox[0] <= 0: bbox[0] = 3
        if bbox[1] <= 0: bbox[1] = 2
        if bbox[2] >= 1919: bbox[2] = 1916
        if bbox[3] >= 1055: bbox[3] = 1053
        object_rect = AnnoLib.AnnoRect(x1=float(bbox[0]),
                                       y1=float(bbox[1]),
                                       x2=float(bbox[2]),
                                       y2=float(bbox[3]))
        assert object_rect.x1 < object_rect.x2
        assert object_rect.y1 < object_rect.y2
        object_rect.classID = class_id
        rect_list.append(object_rect)

    return rect_list
Exemplo n.º 12
0
def read_rects(file):
    with open(file, 'r') as f:
        lines = f.readlines()

    rects = []
    for line in lines:
        # 0    1 2 3  4  5  6  7 8 9 0 1 2 3 4  5
        # "Car 0 1 0 %f %f %f %f 0 0 0 0 0 0 0 %f"
        split = line.split(' ')

        # If not a car, don't care
        if split[0] != 'Car':
            continue

        x1 = float(split[4])
        y1 = float(split[5])
        x2 = float(split[6])
        y2 = float(split[7])
        rect = AnnotationLib.AnnoRect(x1, y1, x2, y2)
        rect.score = float(split[-1])
        rects.append(rect)
    return rects
def get_singleclass_rectangles(H, confidences, boxes, min_conf,rnn_len=1):
    boxes_r = np.reshape(boxes, (-1,
                                 H["grid_height"],
                                 H["grid_width"],
                                 rnn_len,
                                 4))
    confidences_r = np.reshape(confidences, (-1,
                                             H["grid_height"],
                                             H["grid_width"],
                                             rnn_len,
                                             2))
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])]

    for n in range(0, H['rnn_len']):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                conf = confidences_r[0, y, x, n, 1]
                abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y
                h = max(1, bbox[3])
                w = max(1, bbox[2])
                #w = h * 0.4
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))
    all_rects_r = [r for row in all_rects for cell in row for r in cell if r.true_confidence > min_conf]
    rects = []
    for rect in all_rects_r:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width/2.
        r.x2 = rect.cx + rect.width/2.
        r.y1 = rect.cy - rect.height/2.
        r.y2 = rect.cy + rect.height/2.
        r.score = rect.true_confidence
        rects.append(r)
    
    return rects
Exemplo n.º 14
0
def pal2al(_annolist):
    #annotations = [];
    annotations = AnnotationLib.AnnoList();

    for adesc in _annolist.attribute_desc:
        annotations.attribute_desc[adesc.name] = adesc;
        print("attribute: ", adesc.name, adesc.id)

        for valdesc in adesc.val_to_str:
            annotations.add_attribute_val(adesc.name, valdesc.s, valdesc.id);

    attribute_name_from_id = {adesc.id: aname for aname, adesc in annotations.attribute_desc.iteritems()}
    attribute_dtype_from_id = {adesc.id: adesc.dtype for aname, adesc in annotations.attribute_desc.iteritems()}

    for _a in _annolist.annotation:
        anno = AnnotationLib.Annotation()

        anno.imageName = _a.imageName;

        anno.rects = [];

        for _r in _a.rect:
            rect = AnnotationLib.AnnoRect()

            rect.x1 = _r.x1;
            rect.x2 = _r.x2;
            rect.y1 = _r.y1;
            rect.y2 = _r.y2;

            if _r.HasField("id"):
                rect.id = _r.id;

            if _r.HasField("track_id"):
                rect.track_id = _r.track_id;

            if _r.HasField("score"):
                rect.score = _r.score;

            for _at in _r.attribute:
                try:
                    cur_aname = attribute_name_from_id[_at.id];
                    cur_dtype = attribute_dtype_from_id[_at.id];
                except KeyError as e:
                    print("attribute: ", _at.id)
                    print(e)
                    assert(False);

                if cur_dtype == AnnotationLib.AnnoList.TYPE_INT32:
                    rect.at[cur_aname] = _at.val;
                elif cur_dtype == AnnotationLib.AnnoList.TYPE_FLOAT:
                    rect.at[cur_aname] = _at.fval;
                elif cur_dtype == AnnotationLib.AnnoList.TYPE_STRING:
                    rect.at[cur_aname] = _at.strval;
                else:
                    assert(False);

            anno.rects.append(rect);

        annotations.append(anno);

    return annotations;
Exemplo n.º 15
0
def add_rectangles(H, orig_image, confidences, boxes, depths, locations, corners, use_stitching=False, rnn_len=1, min_conf=0.1, show_removed=True, tau=0.25,
    color_removed=(0, 0, 255), color_acc=(0, 0, 255)):
    image = np.copy(orig_image[0])
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes, (-1,
                                 H["grid_height"],
                                 H["grid_width"],
                                 rnn_len,
                                 4))
    confidences_r = np.reshape(confidences, (-1,
                                             H["grid_height"],
                                             H["grid_width"],
                                             rnn_len,
                                             H['num_classes']))

    depths_r = np.reshape(depths, (-1, 
                                   H['grid_height'], 
                                   H['grid_width'], 
                                   rnn_len, 
                                   1))

    locations_r = np.reshape(locations, (-1, 
                                         H['grid_height'], 
                                         H['grid_width'], 
                                         rnn_len, 
                                         3))

    corners_r = np.reshape(corners, (-1, 3, 8))
    dimensions_yaw = [get_dimensions(corners_r[index]) for index in range(corners_r.shape[0])]
    dimensions_yaw = np.reshape(dimensions_yaw, (-1, 
                                                 H['grid_height'], 
                                                 H['grid_width'], 
                                                 rnn_len, 
                                                 4))

    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])]
    
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                depth = depths_r[0, y, x, n, 0]
                location = locations_r[0, y, x, n, :]
                dims_alpha = dimensions_yaw[0, y, x, n, :]
                conf = np.max(confidences_r[0, y, x, n, 1:]) 
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf,depth,location[0],location[1],location[2],dims_alpha[0],dims_alpha[1],dims_alpha[2],dims_alpha[3]))

    all_rects_r = [r for row in all_rects for cell in row for r in cell] 
    if use_stitching:
        from utils.stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    if not show_removed:
        all_rects_r = []
  
    pairs = [(all_rects_r, color_removed), (acc_rects, color_acc)]
    im = Image.fromarray(image.astype('uint8'))
    draw = ImageDraw.Draw(im)
    for rect_set, color in pairs:
        for rect in rect_set:
            if rect.confidence > min_conf:
                _draw_rect(draw, rect, color)

    image = np.array(im).astype('float32')

    rects = []
  
    for rect in acc_rects:
        if rect.confidence < min_conf:
            continue
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width/2.
        r.x2 = rect.cx + rect.width/2.
        r.y1 = rect.cy - rect.height/2.
        r.y2 = rect.cy + rect.height/2.
        r.score = rect.true_confidence
        r.x_3d = rect.x_3d
        r.y_3d = rect.y_3d
        r.z_3d = rect.z_3d
        r.height_3d = rect.height_3d
        r.width_3d = rect.width_3d
        r.length_3d = rect.length_3d
        r.alpha = rect.alpha
       
        view_angle = np.arctan2(rect.z_3d, rect.x_3d)
        r.alpha += np.pi * 0.5 - view_angle

        rects.append(r)
   
    return image, rects
Exemplo n.º 16
0
def add_rectangles(H,
                   orig_image,
                   confidences,
                   boxes,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.1,
                   show_removed=True,
                   tau=0.25,
                   color_removed=(0, 0, 255),
                   color_acc=(0, 0, 255)):
    image = np.copy(orig_image[0])
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes,
                         (-1, H["grid_height"], H["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences,
        (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes']))
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])]
                 for _ in range(H["grid_height"])]
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                conf = np.max(confidences_r[0, y, x, n, 1:])
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    all_rects_r = [r for row in all_rects for cell in row for r in cell]
    if use_stitching:
        from stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    if not show_removed:
        all_rects_r = []

    pairs = [(all_rects_r, color_removed), (acc_rects, color_acc)]
    im = Image.fromarray(image.astype('uint8'))
    draw = ImageDraw.Draw(im)
    for rect_set, color in pairs:
        for rect in rect_set:
            if rect.confidence > min_conf:
                _draw_rect(draw, rect, color)

    image = np.array(im).astype('float32')

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width / 2.
        r.x2 = rect.cx + rect.width / 2.
        r.y1 = rect.cy - rect.height / 2.
        r.y2 = rect.cy + rect.height / 2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
Exemplo n.º 17
0
def test(config):
    """ Takes the config, run test program
    """

    data_mean = load_data_mean(config["data"]["idl_mean"],
                               config["net"]["img_width"],
                               config["net"]["img_height"],
                               image_scaling=1.0)

    num_test_images = 599

    # Warning: load_idl returns an infinite generator. Calling list() before islice() will hang.
    test_list = list(
        itertools.islice(
            load_idl(config["data"]["test_idl"], data_mean, config["net"],
                     False), 0, num_test_images))
    img = np.copy(test_list[-1]["raw"])
    # plt.imshow(img)

    net = apollocaffe.ApolloNet()
    net.phase = 'test'
    forward(net, test_list[0], config["net"], True)
    net.load("data/snapshot/reinspect_hcs_800000.h5")

    annolist = al.AnnoList()
    net_config = config["net"]
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]

    if config.has_key("conf_th"):
        conf_th = config["conf_th"]
    else:
        conf_th = 0.6

    mae = 0.
    for i in range(num_test_images):
        inputs = test_list[i]
        bbox_list, conf_list = forward(net, inputs, net_config, True)

        img = np.copy(inputs["raw"])
        all_rects = [[[] for x in range(net_config["grid_width"])]
                     for y in range(net_config["grid_height"])]
        for n in range(len(bbox_list)):
            for k in range(net_config["grid_height"] *
                           net_config["grid_width"]):
                y = int(k / net_config["grid_width"])
                x = int(k % net_config["grid_width"])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k, 1].flatten()[0]
                # notice the output rect [cx, cy, w, h]
                # cx means center x-cord
                abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
                abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
                w = bbox[2, 0, 0]
                h = bbox[3, 0, 0]
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

        acc_rects = stitch_rects(all_rects)

        display = True
        if display:
            for rect in acc_rects:
                if rect.true_confidence < conf_th:
                    continue
                cv2.rectangle(img, (rect.cx - int(rect.width / 2),
                                    rect.cy - int(rect.height / 2)),
                              (rect.cx + int(rect.width / 2),
                               rect.cy + int(rect.height / 2)), (255, 0, 0), 2)


#                cv2.circle(img,
#                              (rect.cx, rect.cy),
#                              ((rect.width + rect.height)/4),
#                              (255,0,0),
#                              2)
            img_name = './data/tmp/%05d.jpg' % i
            plt.imsave(img_name, img)
            plt.figure(figsize=(15, 10))
            plt.imshow(img)

        anno = al.Annotation()
        anno.imageName = inputs["imname"]
        # count
        number = 0
        for rect in acc_rects:
            r = al.AnnoRect()
            r.x1 = rect.cx - rect.width / 2.
            r.x2 = rect.cx + rect.width / 2.
            r.y1 = rect.cy - rect.height / 2.
            r.y2 = rect.cy + rect.height / 2.
            r.score = rect.true_confidence
            anno.rects.append(r)
            if r.score > conf_th:
                number += 1
        annolist.append(anno)
        mae += abs(number - len(inputs["rects"]))
        print anno.imageName, number, len(
            inputs["rects"]), abs(number - len(inputs["rects"]))
    print mae / num_test_images
Exemplo n.º 18
0
def add_rectangles(H,
                   orig_image,
                   confidences,
                   boxes,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.1,
                   show_removed=True,
                   tau=0.25,
                   show_suppressed=True):
    image = np.copy(orig_image[0])
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes,
                         (-1, H["grid_height"], H["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences,
        (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes']))
    cell_pix_size = H['region_size']

    # all_rects is an "array" of lists (grid_height, grid_width, ?) that builds up all of the bounding boxes
    # found in each cell. Ultimately the 3rd dimension will have length num_boxes_in_cell
    all_rects = [[[] for _ in range(H["grid_width"])]
                 for _ in range(H["grid_height"])]

    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):

                bbox = boxes_r[0, y, x, n, :]

                # These next couple lines appear to convert from cell-based coordinates to absolute image coordinates
                # i.e. the origin corner of the bounding box is defined relative to a cell, not the origin of the image
                abs_cx = int(bbox[0]) + old_div(cell_pix_size,
                                                2) + cell_pix_size * x
                abs_cy = int(bbox[1]) + old_div(cell_pix_size,
                                                2) + cell_pix_size * y

                # However, width and height of bounding box are still in absolute original pixels
                w = bbox[2]
                h = bbox[3]

                # Appear to be taking the confidence of every class but the first (assuming they add to 1...),
                # Or maybe this is designed only for binary classification now
                conf = np.max(confidences_r[0, y, x, n, 1:])
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    all_rects_r = [r for row in all_rects for cell in row for r in cell]

    # Stitching looks a lot like a hacky way to deal with what are essentially bad predictions
    if use_stitching:
        from stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    # Pairs is a list of tuples: ([list of rect bounding boxes in openCV format], RGB_tuple)
    if show_suppressed:
        # Red if it's an originally predicted box
        pairs = [(all_rects_r, (255, 0, 0))]
    else:
        pairs = []
    # Green if it's a stitched box
    pairs.append((acc_rects, (0, 255, 0)))

    for rect_set, color in pairs:
        # The important part here is that acc_rects is drawn AFTER all_rects... so green boxes should appear over red ones
        for rect in rect_set:
            if rect.confidence > min_conf:
                # min_conf default is 0.1...
                # So it appears that low-confidence rects that were later stitched are drawn
                cv2.rectangle(image, (rect.cx - int(old_div(rect.width, 2)),
                                      rect.cy - int(old_div(rect.height, 2))),
                              (rect.cx + int(old_div(rect.width, 2)),
                               rect.cy + int(old_div(rect.height, 2))), color,
                              1)

    cv2.putText(
        image,
        str(len([rect for rect in acc_rects if rect.confidence > min_conf])),
        (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

    rects = []
    for rect in acc_rects:
        if rect.confidence > min_conf:
            r = al.AnnoRect()
            r.x1 = rect.cx - old_div(rect.width, 2.)
            r.x2 = rect.cx + old_div(rect.width, 2.)
            r.y1 = rect.cy - old_div(rect.height, 2.)
            r.y2 = rect.cy + old_div(rect.height, 2.)
            r.score = rect.confidence
            rects.append(r)

    return image, rects
Exemplo n.º 19
0
def read_kitti_anno(label_file, detect_truck):
    """ Reads a kitti annotation file.

    Args:
    label_file: Path to file

    Returns:
      Lists of rectangels: Cars and don't care area.
    """
    labels = [line.rstrip().split(' ') for line in open(label_file)]
    rect_list = []
    for label in labels:
        if not (label[0] == 'Car' or label[0] == 'Pedestrian' or
                label[0] == 'Truck' or label[0] == 'DontCare'):
            continue
        notruck = not detect_truck
        if notruck and label[0] == 'Truck':
            continue
        class_id = -100
        if label[0] == 'DontCare':
            class_id = -1
        if label[0] == 'Car':
            class_id = 1
        if label[0] == 'Pedestrian':
            class_id = 2
        #if label[0] == 'Cyclist':
            #class_id = 3
        print("***123456***");
        print(label[0]);
        print(class_id);
        print("***123456***");
        object_rect = AnnoLib.AnnoRect(
            x1=float(label[4]), y1=float(label[5]),
            x2=float(label[6]), y2=float(label[7]))
        assert object_rect.x1 < object_rect.x2
        assert object_rect.y1 < object_rect.y2
        object_rect.classID = class_id
        rect_list.append(object_rect)
        '''w=float(label[6])-float(label[4])
        h=float(label[7])-float(label[5])
        if class_id == 2:
            for x_scale_factor in ([-0.25,0,0.25]):
                #print("**********")
                #print(x_scale_factor)
                x_offset=w*x_scale_factor
                x_1=float(label[4])+x_offset
                x_1=max(0,x_1)
                x_2=float(label[6])+x_offset
                x_2=min(x_2,1280)
                for y_scale_factor in ([-0.25,0,0.25]):
                    print("*********")
                    print("({},{})".format(x_scale_factor,y_scale_factor))
                    y_offset=h*y_scale_factor
                    y_1=float(label[5])+y_offset
                    y_1=max(0,y_1)
                    y_2=float(label[7])+y_offset
                    y_2=min(y_2,1280)
                    object_rect_aug=AnnoLib.AnnoRect(x1=x_1,x2=x_2,y1=y_1,y2=y_2)
                    assert object_rect.x1 < object_rect.x2
                    assert object_rect.y1 < object_rect.y2
                    object_rect.classID = class_id
                    rect_list.append(object_rect)'''
    return rect_list