Beispiel #1
0
def main():
    args = get_arguments()

    if args.img_path[-4] != '.':
        files = GetAllFilesListRecusive(args.img_path,
                                        ['.jpg', '.jpeg', '.png', '.bmp'])
    else:
        files = [args.img_path]

    shape = INPUT_SIZE.split(',')
    shape = (int(shape[0]), int(shape[1]), 3)

    if args.pb_file == '':
        sess, pred, x = load_from_checkpoint(shape, args.snapshots_dir,
                                             args.model)
    else:
        sess, pred, x, label_colors, label_names = load_from_pb(
            shape, args.pb_file)

    if args.measure_time:
        calculate_perfomance(sess, x, pred, shape, args.runs, args.batch_size)
        quit()

    for path in files:

        img, filename = load_img(path)

        orig_img = cv2.imread(path)

        if args.pb_file != '':
            img = np.expand_dims(img, axis=0)

        t = time.time()
        preds = sess.run(pred, feed_dict={x: img})

        print('time: ', time.time() - t)
        print('output shape: ', preds.shape)

        msk = decode_labels(preds,
                            num_classes=len(label_colors),
                            label_colours=label_colors)
        im = msk[0]
        im = cv2.resize(im, (orig_img.shape[1], orig_img.shape[0]),
                        interpolation=cv2.INTER_NEAREST)
        print('im', im.shape)

        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)

        im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
        #img = cv2.cvtColor(orig_img, cv2.COLOR_RGB2BGR)
        img = orig_img

        if args.weighted:
            indx = (im == [0, 0, 0])
            print(im.shape, img.shape)
            im = cv2.addWeighted(im, 0.8, img, 0.2, 0)
            im[indx] = img[indx]

        cv2.imwrite(args.save_dir + filename.replace('.jpg', '.png'), im)
Beispiel #2
0
def main():

    args = get_arguments()

    if args.img_path[-4] != '.':
        files = GetAllFilesListRecusive(args.img_path,
                                        ['.jpg', '.jpeg', '.png'])
    else:
        files = [args.img_path]

    shape = INPUT_SIZE.split(',')
    shape = (int(shape[0]), int(shape[1]), 3)

    if args.pb_file == '':
        sess, pred, x = load_from_checkpoint(shape, args.snapshots_dir)
    else:
        sess, pred, x = load_from_pb(shape, args.pb_file)

    if args.measure_time:
        calculate_perfomance(sess, x, pred, shape, args.runs, args.batch_size)
        quit()

    for path in files:

        img, filename, real_size = load_img(path)

        orig_img = copy.deepcopy(img)

        if args.pb_file != '':
            img = np.expand_dims(img, axis=0)

        t = time.time()
        preds = sess.run(pred, feed_dict={x: img})

        #print('time: ', time.time() - t)
        print('output shape: ', preds.shape)

        msk = decode_label(preds)
        im = msk
        #im = preds
        #print('im', im.shape)

        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)

        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB)

        if args.weighted:
            indx = (im == [0, 0, 0])
            print(im.shape, img.shape)
            im = cv2.addWeighted(im, 0.7, img, 0.7, -15)
            im[indx] = img[indx]
        im = cv2.resize(im, (int(real_size[1]), int(real_size[0])))
        cv2.imwrite(args.save_dir + filename.replace('.jpg', '.png'), im)
Beispiel #3
0
def load_img(img_path):
    if os.path.isfile(img_path):
        print('successful load img: {0}'.format(img_path))
    else:
        print('not found file: {0}'.format(img_path))
        sys.exit(0)

    filename = img_path.split('/')[-1]
    img = cv2.imread(img_path)

    shape = INPUT_SIZE.split(',')
    img = cv2.resize(img, (int(shape[0]), int(shape[1])))
    #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    print('input image shape: ', img.shape)

    return img, filename
    return size

SAVE_DIR = './output/'

DATA_LIST_PATH = '/mnt/Data/Datasets/Segmentation/mapillary_vistas_3_class/merged_valid_list.txt'

snapshot_dir = './snapshots'
best_models_dir = './best_models'

num_classes = NUM_CLASSES

num_steps = calc_size(DATA_LIST_PATH) # numbers of images in validation set
time_list = []
INTERVAL = 120
INPUT_SIZE = INPUT_SIZE.split(',')
INPUT_SIZE = [int(INPUT_SIZE[0]), int(INPUT_SIZE[1])]
IGNORE_LABEL = IGNORE_LABEL
batch_size = 1


def get_arguments():
    parser = argparse.ArgumentParser(description="Reproduced PSPNet")

    parser.add_argument("--measure-time", action="store_true",
                        help="whether to measure inference time")
    parser.add_argument("--model", type=str, default='',
                        help="Model to use.",
                        choices=['train', 'trainval', 'train_bn', 'trainval_bn', 'other'],
                        required=True)
    parser.add_argument("--data-list", type=str, default=DATA_LIST_PATH,
Beispiel #5
0
def run_on_video(video_filename,
                 out_filename,
                 model_path,
                 num_classes,
                 save_to='simple',
                 canvas_size=(1600, 800),
                 alpha=0.8,
                 beta=0.2,
                 output_size=(1280, 720),
                 step=1):
    '''
    save_to: simple, double_screen or weighted
    '''
    input_size = (int(INPUT_SIZE.split(',')[0]), int(INPUT_SIZE.split(',')[0]))
    x = tf.placeholder(dtype=tf.float32,
                       shape=(int(input_size[0]), int(input_size[1]), 3))
    img_tf = preprocess(x)
    img_tf, n_shape = check_input(img_tf)

    net = ICNet_BN({'data': img_tf}, num_classes=num_classes)

    raw_output = net.layers['conv6']

    # Predictions.
    raw_output_up = tf.image.resize_bilinear(raw_output,
                                             size=n_shape,
                                             align_corners=True)
    #raw_output_up = tf.image.crop_to_bounding_box(raw_output_up, 0, 0, INPUT_SIZE[0], INPUT_SIZE[1])
    raw_output_up = tf.argmax(raw_output_up, dimension=3)
    pred = tf.expand_dims(raw_output_up, dim=3)

    # Init tf Session
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    init = tf.global_variables_initializer()

    sess.run(init)

    restore_var = tf.global_variables()

    print('model_path', model_path)
    ckpt = tf.train.latest_checkpoint(model_path)
    if len(ckpt):
        loader = tf.train.Saver(var_list=restore_var)
        load(loader, sess, ckpt)


######
    cap = cv2.VideoCapture(video_filename)

    out_cap = None
    if save_to == 'double_screen':
        out_cap = cv2.VideoWriter(out_filename.replace('.mp4', '.avi'),
                                  cv2.VideoWriter_fourcc(*"MJPG"), 60,
                                  (canvas_size[0], canvas_size[1]))
    elif save_to == 'weighted':
        out_cap = cv2.VideoWriter(out_filename.replace('.mp4', '.avi'),
                                  cv2.VideoWriter_fourcc(*"MJPG"), 60,
                                  (output_size[0], output_size[1]))

    # Check if camera opened successfully
    if cap.isOpened() == False:
        print("Error opening video stream or file")
        return

    frame_num = 0
    zf = None
    while (cap.isOpened()):

        # Capture frame-by-frame
        ret, image = cap.read()

        frame_num = frame_num + 1
        if frame_num % step != 0:
            continue
        print('Processing frame', frame_num)

        if out_cap == None and save_to != 'images':
            out_cap = cv2.VideoWriter(out_filename.replace('.mp4', '.avi'),
                                      cv2.VideoWriter_fourcc(*'MJPG'), 60,
                                      (image.shape[1], image.shape[0]))
        elif save_to == 'images' and zf == None:
            zipfile_name = out_filename.replace('.avi', '.zip')

        original_shape = image.shape
        if image.shape[2] == 1:
            image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

        if image.shape[2] == 4:
            image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (input_size[0], input_size[1]))

        preds = sess.run(pred, feed_dict={x: image})
        msk = decode_labels(preds, num_classes=num_classes)
        frame = msk[0]

        #cv2.imshow('1', frame)
        #cv2.waitKey(0)
        #print(frame.shape)

        if save_to == 'double_screen':

            canvas = np.zeros((canvas_size[1], canvas_size[0], 3),
                              dtype=np.uint8)
            #frame_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            frame_orig = cv2.resize(
                image, (int(canvas_size[0] / 2), int(canvas_size[1])))

            #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = cv2.resize(frame,
                               (int(canvas_size[0] / 2), int(canvas_size[1])))

            canvas[:, 0:int(canvas_size[0] / 2), :] = frame_orig
            canvas[:, int(canvas_size[0] / 2):, :] = frame
            #cv2.imshow('1', frame)
            #cv2.waitKey(0)
            canvas = cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)
            print('canvas shape', canvas.shape)

            out_cap.write(canvas)

        elif save_to == 'simple':

            frame = cv2.resize(frame, (original_shape[1], original_shape[0]),
                               interpolation=cv2.INTER_NEAREST)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            #cv2.imshow('1', frame)
            #cv2.waitKey(0)
            out_cap.write(frame)

        elif save_to == 'images':

            frame = cv2.resize(frame, (original_shape[1], original_shape[0]),
                               interpolation=cv2.INTER_NEAREST)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = cv2.resize(image, (original_shape[1], original_shape[0]),
                               interpolation=cv2.INTER_NEAREST)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            cv2.imwrite('/tmp/1.png', frame)
            #cv2.imwrite('/tmp/1_orig.png', image)
            zf = zipfile.ZipFile(zipfile_name, "a", zipfile.ZIP_DEFLATED)
            name = 'frame_' + '%08d' % frame_num + '.png'
            orig_name = 'frame_orig_' + '%08d' % frame_num + '.png'
            zf.write('/tmp/1.png', name)
            #zf.write('/tmp/1_orig.png', orig_name)
            zf.close()

        elif save_to == 'weighted':

            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            frame = cv2.resize(frame, output_size)
            image = cv2.resize(image, output_size)

            frame = cv2.addWeighted(image, alpha, frame, beta, 0)

            out_cap.write(frame)

        elif save_to == 'perspective':

            preds = preds.squeeze()

            img, mask = getCutedRoad(image, preds)

            mask = np.expand_dims(mask, axis=0)
            mask = np.expand_dims(mask, axis=3)

            msk = decode_labels(mask, num_classes=num_classes)
            f = msk[0]

            # h, w = frame.shape[:2]

            # src = np.float32([[x1, y1],    # br
            #           [x0, y1],    # bl
            #           [x0, y0],   # tl
            #           [x1, y0]])  # tr

            # dst = np.float32([[w, h],       # br
            #                 [0, h],       # bl
            #                 [0, 0],       # tl
            #                 [w, 0]])      # tr

            # M = cv2.getPerspectiveTransform(src, dst)
            # Minv = cv2.getPerspectiveTransform(dst, src)

            # warped = cv2.warpPerspective(image, M, (w, h), flags=cv2.INTER_LINEAR)

            # cv2.imshow('1', warped)

            #resized = cv2.resize(img[y0 : y1, x0 : x1], input_size, interpolation = cv2.INTER_NEAREST)

            print((preds == 2).sum() / (preds.shape[0] * preds.shape[1]))
            mask = np.array(mask)
            mask = mask.squeeze()
            print((mask == 2).sum() / (mask.shape[0] * mask.shape[1]))

            cv2.imshow(
                '2',
                cv2.resize(img, input_size, interpolation=cv2.INTER_NEAREST))
            cv2.imshow(
                '3', cv2.resize(f, input_size,
                                interpolation=cv2.INTER_NEAREST))
            cv2.imshow('4', image)
            cv2.waitKey(0)
            #quit()

    cap.release()
    out_cap.release()
    zf.close()
def main():
    args = get_arguments()

    if args.img_path[-4] != '.':
        files = GetAllFilesListRecusive(args.img_path,
                                        ['.jpg', '.jpeg', '.png'])
    else:
        files = [args.img_path]

    shape = INPUT_SIZE.split(',')
    shape = (int(shape[0]), int(shape[1]), 3)

    x = tf.placeholder(dtype=tf.float32, shape=shape)
    img_tf = preprocess(x)
    img_tf, n_shape = check_input(img_tf)

    # Create network.
    net = ICNet_BN({'data': img_tf},
                   is_training=False,
                   num_classes=num_classes)

    # Predictions.
    raw_output = net.layers['conv6_cls']
    output = tf.image.resize_bilinear(raw_output, tf.shape(img_tf)[1:3, ])
    output = tf.argmax(output, dimension=3)
    pred = tf.expand_dims(output, dim=3)

    # Init tf Session
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    init = tf.global_variables_initializer()

    sess.run(init)

    restore_var = tf.global_variables()

    ckpt = tf.train.get_checkpoint_state(args.snapshots_dir)
    if ckpt and ckpt.model_checkpoint_path:
        loader = tf.train.Saver(var_list=restore_var)
        load_step = int(
            os.path.basename(ckpt.model_checkpoint_path).split('-')[1])
        load(loader, sess, ckpt.model_checkpoint_path)

    for path in files:

        img, filename = load_img(path)

        preds = sess.run(pred, feed_dict={x: img})

        msk = decode_labels(preds, num_classes=num_classes)
        im = msk[0]

        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)

        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        if args.weighted:
            indx = (im == [0, 0, 0])
            im = cv2.addWeighted(im, 0.7, img, 0.7, -15)
            im[indx] = img[indx]

        cv2.imwrite(args.save_dir + filename.replace('.jpg', '.png'), im)
def GetAllFilesListRecusive(path, extensions):

    files_all = []
    for root, subFolders, files in os.walk(path):
        for name in files:
            # linux tricks with .directory that still is file
            if not 'directory' in name and sum(
                [ext in name for ext in extensions]) > 0:
                files_all.append(os.path.join(root, name))
    return files_all


# vistas valid [120.31763023 117.31701579 107.41887195]
# vistas train [119.74690619 116.83774316 106.77015853]
# apollo train [139.85109202 135.58997361 125.24092723]
# total [126.63854281333334, 123.24824418666667, 113.14331923666667]
files = GetAllFilesListRecusive(
    '/mnt/Data/Datasets/Segmentation/Apollo/remapped', ['.jpg'])

w = int(INPUT_SIZE.split(',')[0])
h = int(INPUT_SIZE.split(',')[1])
mean_img = np.zeros((w, h, 3))
i = 0

for file in files:
    print('img ', i, len(files))
    i = i + 1
    mean_img = mean_img + cv2.resize(cv2.imread(file), (w, h))

print(np.mean(mean_img / len(files), axis=(0, 1)))