Beispiel #1
0
def main():
    # load some VOC2007 images for test
    #image = Image.open("000001.jpg")
    #boxes = np.array([[48,240,195,371,11],[8,12,352,498,14]])

    image = Image.open("000004.jpg")
    boxes = np.array([[13,311,84,362,6],[362,330,500,389,6],[235,328,334,375,6],[175,327,252,364,6],[139,320,189,359,6],[108,325,150,353,6],[84,323,121,350,6]])

    #image = Image.open("000010.jpg")
    #boxes = np.array([[87,97,258,427,12],[133,72,245,284,14]])


    classes = boxes[:, -1]
    scores = [1.0]*len(classes)

    class_names = get_classes('../../configs/voc_classes.txt')
    colors = get_colors(class_names)


    image_origin = draw_boxes(np.array(image, dtype='uint8'), boxes[:, :4], classes, scores, class_names, colors)

    image, boxes = random_rotate(image, boxes, prob=1.0)
    image = draw_boxes(np.array(image, dtype='uint8'), boxes[:, :4], classes, scores, class_names, colors)

    Image.fromarray(image_origin).show()
    Image.fromarray(image).show()
Beispiel #2
0
def get_ground_truth_data(annotation_line, input_shape, augment=True, max_boxes=100):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line.split()
    image = Image.open(line[0])
    image_size = image.size
    model_input_size = tuple(reversed(input_shape))
    boxes = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])

    if not augment:
        new_image, padding_size, offset = letterbox_resize(image, target_size=model_input_size, return_padding_info=True)
        image_data = np.array(new_image)
        image_data = normalize_image(image_data)

        # reshape boxes
        boxes = reshape_boxes(boxes, src_shape=image_size, target_shape=model_input_size, padding_shape=padding_size, offset=offset)
        if len(boxes)>max_boxes:
            boxes = boxes[:max_boxes]

        # fill in box data
        box_data = np.zeros((max_boxes,5))
        if len(boxes)>0:
            box_data[:len(boxes)] = boxes

        return image_data, box_data

    # random resize image and crop|padding to target size
    image, padding_size, padding_offset = random_resize_crop_pad(image, target_size=model_input_size)

    # random horizontal flip image
    image, horizontal_flip = random_horizontal_flip(image)

    # random adjust brightness
    image = random_brightness(image)

    # random adjust color level
    image = random_chroma(image)

    # random adjust contrast
    image = random_contrast(image)

    # random adjust sharpness
    image = random_sharpness(image)

    # random convert image to grayscale
    image = random_grayscale(image)

    # random do normal blur to image
    #image = random_blur(image)

    # random do motion blur to image
    #image = random_motion_blur(image, prob=0.2)

    # random vertical flip image
    image, vertical_flip = random_vertical_flip(image)

    # random distort image in HSV color space
    # NOTE: will cost more time for preprocess
    #       and slow down training speed
    #image = random_hsv_distort(image)

    # reshape boxes based on augment
    boxes = reshape_boxes(boxes, src_shape=image_size, target_shape=model_input_size, padding_shape=padding_size, offset=padding_offset, horizontal_flip=horizontal_flip, vertical_flip=vertical_flip)

    # random rotate image and boxes
    image, boxes = random_rotate(image, boxes)

    # random add gridmask augment for image and boxes
    image, boxes = random_gridmask(image, boxes)

    if len(boxes)>max_boxes:
        boxes = boxes[:max_boxes]

    # prepare image & box data
    image_data = np.array(image)
    image_data = normalize_image(image_data)
    box_data = np.zeros((max_boxes,5))
    if len(boxes)>0:
        box_data[:len(boxes)] = boxes

    return image_data, box_data