Example #1
0
def classify_image(image_data):
    img_array = np.fromstring(image_data, dtype=np.uint8)
    image = cv.imdecode(img_array, cv.IMREAD_COLOR)
    image_size = image.shape[:2]

    x, y = random_choice(image_size)
    image = safe_crop(image, x, y)
    print('Start processing image...')

    x_test = np.empty((1, img_rows, img_cols, 3), dtype=np.float32)
    x_test[0, :, :, 0:3] = image / 255.

    out = model.predict(x_test)
    out = np.reshape(out, (img_rows, img_cols, num_classes))
    out = np.argmax(out, axis=2)
    out = to_bgr(out)

    ret = image * 0.6 + out * 0.4
    ret = ret.astype(np.uint8)

    K.clear_session()
    is_success, buffer = cv.imencode(".png", out)
    buffer
    out_bytes = BytesIO(buffer)
    return out_bytes
Example #2
0
    def test_generate_trimap(self):
        image = cv.imread('fg/1-1252426161dfXY.jpg')
        alpha = cv.imread('mask/1-1252426161dfXY.jpg', 0)
        trimap = generate_trimap(alpha)
        self.assertEqual(trimap.shape, (615, 410))

        # ensure np.where works as expected.
        count = 0
        h, w = trimap.shape[:2]
        for i in range(h):
            for j in range(w):
                if trimap[i, j] == unknown_code:
                    count += 1
        x_indices, y_indices = np.where(trimap == unknown_code)
        num_unknowns = len(x_indices)
        self.assertEqual(count, num_unknowns)

        # ensure an unknown pixel is chosen
        ix = random.choice(range(num_unknowns))
        center_x = x_indices[ix]
        center_y = y_indices[ix]

        self.assertEqual(trimap[center_x, center_y], unknown_code)

        x, y = random_choice(trimap)
        # print(x, y)
        image = safe_crop(image, x, y)
        trimap = safe_crop(trimap, x, y)
        alpha = safe_crop(alpha, x, y)
        cv.imwrite('temp/test_generate_trimap_image.png', image)
        cv.imwrite('temp/test_generate_trimap_trimap.png', trimap)
        cv.imwrite('temp/test_generate_trimap_alpha.png', alpha)
Example #3
0
    def test_safe_crop(self):
        name = 'SUNRGBD/kv1/NYUdata/NYU0899'
        image, image_size = get_image(name)
        semantic = get_category(name, image_size)
        different_sizes = [(320, 320), (480, 480), (640, 640)]
        crop_size = random.choice(different_sizes)

        x, y = random_choice(image_size, crop_size)
        image = safe_crop(image, x, y, crop_size)
        semantic = safe_crop(semantic, x, y, crop_size)
        semantic = to_bgr(semantic)
        cv.imwrite('temp/test_safe_crop_image.png', image)
        cv.imwrite('temp/test_safe_crop_semantic.png', semantic)
Example #4
0
 def test_flip(self):
     image = cv.imread('fg/1-1252426161dfXY.jpg')
     # print(image.shape)
     alpha = cv.imread('mask/1-1252426161dfXY.jpg', 0)
     trimap = generate_trimap(alpha)
     x, y = random_choice(trimap)
     image = safe_crop(image, x, y)
     trimap = safe_crop(trimap, x, y)
     alpha = safe_crop(alpha, x, y)
     image = np.fliplr(image)
     trimap = np.fliplr(trimap)
     alpha = np.fliplr(alpha)
     cv.imwrite('temp/test_flip_image.png', image)
     cv.imwrite('temp/test_flip_trimap.png', trimap)
     cv.imwrite('temp/test_flip_alpha.png', alpha)
Example #5
0
 def test_resize(self):
     name = '0_0.png'
     filename = os.path.join('merged', name)
     image = cv.imread(filename)
     bg_h, bg_w = image.shape[:2]
     a = get_alpha(name)
     a_h, a_w = a.shape[:2]
     alpha = np.zeros((bg_h, bg_w), np.float32)
     alpha[0:a_h, 0:a_w] = a
     trimap = generate_trimap(alpha)
     # 剪切尺寸 320:640:480 = 3:1:1
     crop_size = (480, 480)
     x, y = random_choice(trimap, crop_size)
     image = safe_crop(image, x, y, crop_size)
     trimap = safe_crop(trimap, x, y, crop_size)
     alpha = safe_crop(alpha, x, y, crop_size)
     cv.imwrite('temp/test_resize_image.png', image)
     cv.imwrite('temp/test_resize_trimap.png', trimap)
     cv.imwrite('temp/test_resize_alpha.png', alpha)
Example #6
0
    '''
    filename = './single_human_paring/img/man/unexpose/img_023.jpg'
    img_rows, img_cols = 320, 320
    channel = 3

    model_weights_path = 'Look_Into_Person/models/model.11-0.8409.hdf5'
    model = build_model()
    model.load_weights(model_weights_path)

    print(model.summary())

    image = cv.imread(filename)
    image = cv.resize(image, (img_rows, img_cols), cv.INTER_CUBIC)
    image_size = image.shape[:2]

    x, y = random_choice(image_size)
    image = safe_crop(image, x, y)
    print('Start processing image: {}'.format(filename))

    x_test = np.empty((1, img_rows, img_cols, 3), dtype=np.float32)
    x_test[0, :, :, 0:3] = image / 255.

    out = model.predict(x_test)
    out = np.reshape(out, (img_rows, img_cols, num_classes))
    out = np.argmax(out, axis=2)
    out = to_bgr(out)
    #
    # ret = image * 0.6 + out * 0.4
    # ret = ret.astype(np.uint8)

    #if not os.path.exists('images'):
Example #7
0
        print('\nStart processing image: {}'.format(filename))

        bgr_img = cv.imread(os.path.join(out_test_path, filename))
        bg_h, bg_w = bgr_img.shape[:2]
        print('bg_h, bg_w: ' + str((bg_h, bg_w)))

        a = get_alpha_test(image_name)
        a_h, a_w = a.shape[:2]
        print('a_h, a_w: ' + str((a_h, a_w)))

        alpha = np.zeros((bg_h, bg_w), np.float32)
        alpha[0:a_h, 0:a_w] = a
        trimap = generate_trimap(alpha)
        different_sizes = [(320, 320), (320, 320), (320, 320), (480, 480), (640, 640)]
        crop_size = random.choice(different_sizes)
        x, y = random_choice(trimap, crop_size)
        print('x, y: ' + str((x, y)))

        bgr_img = safe_crop(bgr_img, x, y, crop_size)
        alpha = safe_crop(alpha, x, y, crop_size)
        trimap = safe_crop(trimap, x, y, crop_size)
        cv.imwrite('images/{}_image.png'.format(i), np.array(bgr_img).astype(np.uint8))
        cv.imwrite('images/{}_trimap.png'.format(i), np.array(trimap).astype(np.uint8))
        cv.imwrite('images/{}_alpha.png'.format(i), np.array(alpha).astype(np.uint8))

        x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
        x_test[0, :, :, 0:3] = bgr_img / 255.
        x_test[0, :, :, 3] = trimap / 255.

        y_true = np.empty((1, img_rows, img_cols, 2), dtype=np.float32)
        y_true[0, :, :, 0] = alpha / 255.
Example #8
0
        filename = samples[i]
        image_name = filename.split('.')[0]
        print('Start processing image: {}'.format(filename))
        x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
        bgr_img = cv.imread(os.path.join(out_test_path, filename))
        bg_h, bg_w = bgr_img.shape[:2]
        print(bg_h, bg_w)
        a = get_alpha_test(image_name)
        a_h, a_w = a.shape[:2]
        print(a_h, a_w)
        alpha = np.zeros((bg_h, bg_w), np.float32)
        alpha[0:a_h, 0:a_w] = a
        trimap = generate_trimap(alpha)
        different_sizes = [(320, 320), (320, 320), (320, 320), (480, 480), (640, 640)]
        crop_size = random.choice(different_sizes)
        x, y = random_choice(trimap, crop_size)
        print(x, y)
        bgr_img = safe_crop(bgr_img, x, y, crop_size)
        alpha = safe_crop(alpha, x, y, crop_size)
        trimap = safe_crop(trimap, x, y, crop_size)
        cv.imwrite('images/{}_image.png'.format(i), np.array(bgr_img).astype(np.uint8))
        cv.imwrite('images/{}_trimap.png'.format(i), np.array(trimap).astype(np.uint8))
        cv.imwrite('images/{}_alpha.png'.format(i), np.array(alpha).astype(np.uint8))

        x_test = np.empty((1, 320, 320, 4), dtype=np.float32)
        x_test[0, :, :, 0:3] = bgr_img / 255.
        x_test[0, :, :, 3] = trimap / 255.

        y_true = np.empty((1, 320, 320, 2), dtype=np.float32)
        y_true[0, :, :, 0] = alpha / 255.
        y_true[0, :, :, 1] = trimap / 255.
Example #9
0
    test_images = [f for f in os.listdir(rgb_test_path) if
                   os.path.isfile(os.path.join(rgb_test_path, f)) and f.endswith('.png')]

    samples = random.sample(test_images, 10)

    for i in range(len(samples)):
        image_name = samples[i]
        filename = os.path.join(rgb_test_path, image_name)
        image = cv.imread(filename)
        label = get_semantic(image_name)
        image_size = image.shape[:2]
        different_sizes = [(320, 320), (480, 480), (480, 480), (480, 480), (640, 640), (640, 640), (640, 640),
                           (960, 960), (960, 960), (960, 960)]
        crop_size = random.choice(different_sizes)

        x, y = random_choice(image_size, crop_size)
        image = safe_crop(image, x, y, crop_size)
        label = safe_crop(label, x, y, crop_size)
        print('Start processing image: {}'.format(filename))

        x_test = np.empty((1, img_rows, img_cols, 3), dtype=np.float32)
        x_test[0, :, :, 0:3] = image / 255.

        out = model.predict(x_test)
        out = np.reshape(out, (img_rows, img_cols, num_classes))
        out = np.argmax(out, axis=2)
        out = to_bgr(out)

        str_msg = 'crop_size: %s' % (str(crop_size))
        draw_str(out, (20, 20), str_msg)