Exemplo n.º 1
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
Exemplo n.º 2
0
def get_ground_truth_data(annotation_line,
                          input_shape,
                          augment=True,
                          max_boxes=20):
    '''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) / 255.

        # reshape boxes
        boxes = reshape_boxes(boxes,
                              src_shape=image_size,
                              target_shape=model_input_size,
                              padding_shape=padding_size,
                              offset=offset)
        # Get box parameters as (x_center, y_center, box_width, box_height, cls_id)
        #boxes = transform_box_info(boxes, model_input_size)

        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 vertical flip image
    image, vertical_flip = random_vertical_flip(image)

    # random distort image in HSV color space
    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)
    # Get box parameters as (x_center, y_center, box_width, box_height, cls_id)
    #boxes = transform_box_info(boxes, model_input_size)

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

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

    return image_data, box_data