Beispiel #1
0
def unnormalize_data(data, data_range, debug=True):
    '''
	this function unnormalizes 1-d label to normal scale based on range of data
	'''
    if debug:
        print(
            'debug mode is on during data unnormalizing. Please turn off after debuging'
        )
        assert isnparray(data), 'only numpy array is supported'
        if istuple(data_range) or islist(data_range):
            assert len(data_range) == 2, 'data range is not correct'
        elif isnparray(data_range):
            assert data_range.size == 2, 'data range is not correct'
    max_value = data_range[1]
    min_value = data_range[0]
    unnormalized = data * (max_value - min_value) + min_value

    if debug:
        normalized = normalize_data(data=unnormalized,
                                    data_range=data_range,
                                    debug=False)
        assert_almost_equal(data,
                            normalized,
                            decimal=6,
                            err_msg='data is not correct: %f vs %f' %
                            (data, normalized))
    return unnormalized
Beispiel #2
0
def normalize_data(data, data_range=None, debug=True):
    '''
	this function normalizes 1-d label to 0-1
	'''
    if debug:
        assert isnparray(data), 'only numpy array is supported'
        print(
            'debug mode is on during data normalizing. Please turn off after debuging'
        )

    if data_range is None:
        max_value = np.max(data)
        min_value = np.min(data)
    else:
        if debug:
            if istuple(data_range) or islist(data_range):
                assert len(data_range) == 2, 'data range is not correct'
            elif isnparray(data_range):
                assert data_range.size == 2, 'data range is not correct'
        max_value = data_range[1]
        min_value = data_range[0]

    normalized_data = float(data - min_value)
    normalized_data = normalized_data / (max_value - min_value)

    if debug:
        unnormalized = unnormalize_data(data=normalized_data,
                                        data_range=(min_value, max_value),
                                        debug=False)
        assert_almost_equal(data,
                            unnormalized,
                            decimal=6,
                            err_msg='data is not correct: %f vs %f' %
                            (data, unnormalized))
    return normalized_data
Beispiel #3
0
def apply_rotation_tight(bbox_in, angle_in_degree, im_shape, debug=True):
    '''
    return 4 points clockwise
    '''
    if debug:
        assert isnparray(bbox_in) and bbox_in.size == 4, 'box is not correct'

    bbox_in = np.reshape(bbox_in, (4, ))
    bbox_tight = bbox_rotation_inv(bbox_in, angle_in_degree, im_shape, debug=debug) # get top left and bottom right coordinate of the rotated bbox in the image coordinate
    # print('bbox after inverse the rotation')
    # print(bbox_tight)
    pts_total = np.zeros((4, 2), dtype=np.int)
    pts_tl = np.array([bbox_tight[0], bbox_tight[1]])
    pts_br = np.array([bbox_tight[2], bbox_tight[3]])
    line1 = get_line(imagecoor2cartesian(pts_tl, debug=debug), angle_in_degree + 90.00, debug=debug)
    line2 = get_line(imagecoor2cartesian(pts_br, debug=debug), angle_in_degree, debug=debug)
    pts_bl = cartesian2imagecoor(get_intersection(line1, line2, debug=debug), debug=debug)
    pts_tr = cartesian2imagecoor(get_intersection(get_line(imagecoor2cartesian(pts_tl, debug=debug), angle_in_degree, debug=debug), get_line(imagecoor2cartesian(pts_br, debug=debug), angle_in_degree + 90.00, debug=debug), debug=debug), debug=debug)

    # print np.reshape(pts_tl, (1, 2)).shape
    # print pts_total[0, :].shape

    pts_total[0, :] = np.reshape(pts_tl, (1, 2))
    pts_total[1, :] = np.reshape(pts_tr, (1, 2))
    pts_total[2, :] = np.reshape(pts_br, (1, 2))
    pts_total[3, :] = np.reshape(pts_bl, (1, 2))
    return pts_total
Beispiel #4
0
def bbox_rotatedtight2rotatedloose(bbox_in, angle_in_degree, debug=True):
    '''
    transfer the rotated bbox with tight version to loose version, both contains only two points (top left and bottom right)
    only a single box is feeded into
    '''
    if debug:
        assert isnparray(bbox_in) and bbox_in.size == 4, 'box is not correct'

    pts_tl = np.array([bbox_in[0], bbox_in[1]])
    pts_br = np.array([bbox_in[2], bbox_in[3]])
    line1 = get_line(imagecoor2cartesian(pts_tl), angle_in_degree + 90.00)
    line2 = get_line(imagecoor2cartesian(pts_br), angle_in_degree)
    pts_bl = cartesian2imagecoor(get_intersection(line1, line2))
    pts_tr = cartesian2imagecoor(get_intersection(get_line(imagecoor2cartesian(pts_tl), angle_in_degree), get_line(imagecoor2cartesian(pts_br), angle_in_degree + 90.00)))
    # assert_almost_equal(np.dot(pts_bl - pts_br, pts_bl - pts_tl), 0, err_msg='The intersection points are wrong')
    # assert_almost_equal(np.dot(pts_tr - pts_br, pts_tr - pts_tl), 0, err_msg='The intersection points are wrong')

    pts_tl_final = np.zeros((2), dtype=np.float32)
    pts_br_final = np.zeros((2), dtype=np.float32)
    pts_tl_final[0] = min({pts_tl[0], pts_br[0], pts_bl[0], pts_tr[0]})
    pts_tl_final[1] = min({pts_tl[1], pts_br[1], pts_bl[1], pts_tr[1]})
    pts_br_final[0] = max({pts_tl[0], pts_br[0], pts_bl[0], pts_tr[0]})
    pts_br_final[1] = max({pts_tl[1], pts_br[1], pts_bl[1], pts_tr[1]})

    # print(pts_tl_final)
    # print(pts_br_final)
    test = np.hstack((pts_tl_final, pts_br_final))
    return test
Beispiel #5
0
def identity(data, data_range=None, debug=True):
    if debug:
        print(
            'debug mode is on during identity function. Please turn off after debuging'
        )
        assert isnparray(data), 'data is not correct'
    return data
Beispiel #6
0
def print_np_shape(nparray, debug=True):
    '''
	print a string to represent the shape of a numpy array
	'''
    if debug:
        assert isnparray(
            nparray), 'input is not a numpy array and does not have any shape'

    return '(%s)' % (functools.reduce(lambda x, y: str(x) + ', ' + str(y),
                                      nparray.shape))
Beispiel #7
0
def bboxcheck(bbox, debug=True):
    '''
    check the input to be a bounding box 

    parameter:
        bbox:   N x 4 numpy array, N >= 0
    
    return:
        True or False
    '''    
    return isnparray(bbox) and bbox.shape[1] == 4 and bbox.shape[0] >= 0
Beispiel #8
0
def bbox_rotation_inv(bbox_in, angle_in_degree, image_shape, debug=True):
    '''
    bbox_in is two coordinate
    angle is clockwise
    '''
    if debug:
        assert isnparray(bbox_in) and bbox_in.size == 4, 'box is not correct'

    im_width = image_shape[1]
    im_height = image_shape[0]
    coor_in_tl = np.array([(bbox_in[0] - im_width/2)/im_width*2, (bbox_in[1] - im_height/2)/im_height*2, 1]) # normalization
    coor_in_br = np.array([(bbox_in[2] - im_width/2)/im_width*2, (bbox_in[3] - im_height/2)/im_height*2, 1]) # normalization
    # print(coor_in_tl)
    # print(coor_in_br)
    affine = np.array([[math.cos(rad(angle_in_degree)), math.sin(rad(angle_in_degree)), 0], [-math.sin(rad(angle_in_degree)), math.cos(rad(angle_in_degree)), 0]])
    coor_out_tl = np.dot(coor_in_tl, affine.transpose())
    coor_out_br = np.dot(coor_in_br, affine.transpose())
    # print(coor_out_tl)
    # print(coor_out_br)
    bbox_recover = [coor_out_tl[0] * im_width/2 + im_width/2, coor_out_tl[1] * im_height/2 + im_height/2, coor_out_br[0] * im_width/2 + im_width/2, coor_out_br[1] * im_height/2 + im_height/2]
    bbox_recover = np.array(bbox_recover, dtype = float)

    return bbox_recover
Beispiel #9
0
def unpreprocess_image_caffe(image_datablob,
                             pixel_mean=None,
                             swap_channel=True,
                             debug=True):
    '''
	this function unpreprocesses image for caffe only,
	including transfer from bgr to rgb
	from NxCxHxW to a list of HxWxC 
	'''
    if debug:
        print(
            'debug mode is on during unpreprocessing. Please turn off after debuging'
        )
        assert isnparray(image_datablob
                         ) and image_datablob.ndim == 4, 'input is not correct'
        assert image_datablob.shape[1] == 1 or image_datablob.shape[
            1] == 3, 'this is not an blob of image, channel is not 1 or 3'

    if pixel_mean is not None:
        assert pixel_mean.shape == (1, 1, 3) or pixel_mean.shape == (
            1, ), 'pixel mean is not correct'
        pixel_mean_reshape = np.reshape(pixel_mean, (1, 3, 1, 1))
        image_datablob += pixel_mean_reshape

    image_datablob = np.transpose(
        image_datablob,
        (0, 2, 3, 1))  # permute to [batch, height, weight, channel]

    if image_datablob.shape[-1] == 3 and swap_channel:  # channel dimension
        image_datablob = image_datablob[:, :, :,
                                        [2, 1,
                                         0]]  # from bgr to rgb for color image
    image_data_list = list()
    for i in xrange(image_datablob.shape[0]):
        image_data_list.append(image_datablob[i, :, :, :])
    return image_data_list