Пример #1
0
    def __getitem__(self, idx):
        clean_fn = random.choice(self.clean_fns[idx])

        clean_img = read_img(clean_fn)
        noise_img = read_img(clean_fn.replace('GT_SRGB', 'NOISY_SRGB'))

        if self.patch_size > 0:
            [clean_img, noise_img] = get_patch([clean_img, noise_img],
                                               self.patch_size)

        return hwc_to_chw(noise_img), hwc_to_chw(clean_img), np.zeros(
            (3, self.patch_size, self.patch_size)), np.zeros(
                (3, self.patch_size, self.patch_size))
Пример #2
0
    def __getitem__(self, idx):
        clean_fn = random.choice(self.clean_fns[idx])

        clean_img = read_img(clean_fn)
        noise_img = read_img(clean_fn.replace('GT_SRGB', 'NOISY_SRGB'))

        if self.patch_size > 0:
            clean_img, noise_img = get_patch(clean_img, noise_img,
                                             self.patch_size)

        clean_img_chw = hwc_to_chw(clean_img)
        noise_img_chw = hwc_to_chw(noise_img)

        return noise_img_chw, clean_img_chw
Пример #3
0
    def __getitem__(self, idx):
        clean_fn = random.choice(self.clean_fns[idx])

        clean_img = read_img(clean_fn)
        noise_img = read_img(clean_fn.replace('GT_SRGB', 'NOISY_SRGB'))
        sigma_img = read_img(clean_fn.replace(
            'GT_SRGB', 'SIGMA_SRGB')) / 15.  # inverse scaling

        if self.patch_size > 0:
            [clean_img, noise_img,
             sigma_img] = get_patch([clean_img, noise_img, sigma_img],
                                    self.patch_size)

        return hwc_to_chw(noise_img), hwc_to_chw(clean_img), hwc_to_chw(
            sigma_img), np.ones((3, self.patch_size, self.patch_size))
Пример #4
0
	def __getitem__(self, idx):
		if not len(self.origin_imgs[idx]):
			origin_img = read_img(self.filenames[idx])

			if not (len(self.sigma_s) and len(self.sigma_c)):
				# all random
				min_log = np.log([0.000001])
				sigma_s = min_log + np.random.rand(1) * (np.log([0.002]) - min_log)
				sigma_s = np.exp(sigma_s)
				sigma_c = min_log + np.random.rand(1) * (np.log([0.001]) - min_log)
				sigma_c = np.exp(sigma_c)
			else:
				sigma_s = self.sigma_s
				sigma_c = self.sigma_c

			self.origin_imgs[idx], self.noise_imgs[idx] = self.isp.cbdnet_noise_generate_srgb(origin_img, sigma_s, sigma_c)

		origin_img = self.origin_imgs[idx]
		noise_img = self.noise_imgs[idx]

		patch_origin_img, patch_noise_img = get_patch(origin_img, noise_img, self.patch_size, self.random)

		patch_origin_img_chw = hwc_to_chw(patch_origin_img)
		patch_noise_img_chw = hwc_to_chw(patch_noise_img)

		return patch_noise_img_chw, patch_origin_img_chw
Пример #5
0
def inference(model_path, img_id):
    model = load_model(model_path)
    img = read_img(img_id)
    normalized = normalize_img(img)

    repeats_x = math.ceil(img.shape[0] / INPUT_SIZE)
    repeats_y = math.ceil(img.shape[1] / INPUT_SIZE)
    input_shape = (1, INPUT_SIZE, INPUT_SIZE, img.shape[2])
    result = np.zeros((img.shape[0], img.shape[1], len(CLASSES)))

    for idx_x in range(repeats_x):
        for idx_y in range(repeats_y):
            s_x = idx_x * INPUT_SIZE
            s_y = idx_y * INPUT_SIZE

            patch = normalized[s_x:s_x + INPUT_SIZE, s_y:s_y + INPUT_SIZE]
            input_img = np.zeros(input_shape)
            input_img[0, 0:patch.shape[0], 0:patch.shape[1]] = patch

            pred = model.predict(input_img)
            result[s_x:s_x + patch.shape[0], s_y:s_y + patch.shape[1]] \
                = pred[0, 0:patch.shape[0], 0:patch.shape[1]]

    result = np.where(result > INFERENCE_THRESHOLD, 1, 0)

    return result
Пример #6
0
def main(flags):
    model = flags.model
    image = flags.image

    saver = tf.train.import_meta_graph(model + ".meta")
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.InteractiveSession(config=config)
    saver.restore(sess, model)
    X, mode = tf.get_collection("inputs")
    pred = tf.get_collection("outputs")[0]

    print('loading image')
    proj, geotrans, input_img = read_img(image)
    input_img = input_img[:, :, :3]
    input_img = np.asarray(input_img, dtype='uint8')

    label_pred = predict_img_with_smooth_windowing(
        input_img,
        window_size=256,
        subdivisions=2,
        batch_size=256,
        pred_func=(
            lambda img: sess.run(pred, feed_dict={X: img, mode: False})
        )
    )
    label_pred = label_pred[:, :, 0]
    label_pred[np.where(label_pred >= 0.5)] = 1
    label_pred[np.where(label_pred < 0.5)] = 0
    label_pred = label_pred.astype(np.uint8)

    prd_name = "%s_%s_m%s_prd.tif" % (image[:-4], model.split('/')[0], model[-7:-5])
    write_img(prd_name, proj, geotrans, label_pred)
Пример #7
0
def create_data_record(out_filename, images_addrs, labels_addrs):
    # open the TFRecords file
    writer = tf.python_io.TFRecordWriter(out_filename)
    for i in range(len(images_addrs)):
        # print how many images are saved every 100 images
        if  i % 100==0:
            print('Train data: {}/{}'.format(i, len(images_addrs)))
            sys.stdout.flush()
        # Load the image
        img = read_img(images_addrs[i])

        label = read_label(labels_addrs[i])
        if img  is None or label is None:
            print("erroe null image")
            continue

        # Create a feature
        feature = {
            'image_raw': _bytes_feature(img.tostring()),
            'label': _bytes_feature(label.tostring())
        }
        # Create an example protocol buffer
        example = tf.train.Example(features=tf.train.Features(feature=feature))

        # Serialize to string and write on the file
        writer.write(example.SerializeToString())

    writer.close()
    sys.stdout.flush()
Пример #8
0
def smart_split_img_at_middle_by_x_axis(img_path, resize_radio=0.1):
    im_raw = utils.read_img(img_path)
    im_resize = utils.resize_by_percent(im_raw, resize_radio)

    bin_img = utils.rgb2binary(im_resize)
    ry, rx = bin_img.shape
    img_mtx0 = np.asarray(bin_img)
    y_sum_array0 = img_mtx0.sum(axis=0)  # y轴求和
    subtracts_arr = np.abs(array_latter_subtracts_precious(y_sum_array0 /
                                                           ry))  # 长度减1
    subtracts_arr_index = np.argsort(subtracts_arr,
                                     kind='quicksort',
                                     order=None)
    subtracts_arr_index = subtracts_arr_index[-10:]

    index_middle_distance_list = list(np.abs(subtracts_arr_index -
                                             int(rx / 2)))
    split_index = subtracts_arr_index[index_middle_distance_list.index(
        min(index_middle_distance_list))] + 1
    split_index = int(split_index / resize_radio)
    img_left, img_right = split_by_index(im_raw, split_index)
    left_path = img_path.replace('.jpg', '_left.jpg')
    right_path = img_path.replace('.jpg', '_right.jpg')
    cv2.imencode('.jpg', img_left)[1].tofile(left_path)
    cv2.imencode('.jpg', img_right)[1].tofile(right_path)
    print(left_path)
    print(right_path)
Пример #9
0
    def __getitem__(self, index):
        img_id = self.imgs[index]
        
        img = read_img(self.root / img_id)
        mask = np.zeros_like(img)[..., [0]]

        return dev_transform(img, mask)[0], img_id
Пример #10
0
def get_residual_map(img_path, cfg):
    test_img = read_img(img_path, cfg.grayscale)

    if test_img.shape[:2] != (cfg.im_resize, cfg.im_resize):
        test_img = cv2.resize(test_img, (cfg.im_resize, cfg.im_resize))

    test_img_ = test_img / 255.

    if test_img.shape[:2] == (cfg.patch_size, cfg.patch_size):
        test_img_ = np.expand_dims(test_img_, 0)
        decoded_img = autoencoder.predict(test_img_)
    else:
        patches = get_patch(test_img_, cfg.patch_size, cfg.stride)
        patches = autoencoder.predict(patches)
        decoded_img = patch2img(patches, cfg.im_resize, cfg.patch_size,
                                cfg.stride)

    rec_img = np.reshape((decoded_img * 255.).astype('uint8'), test_img.shape)

    if cfg.grayscale:
        ssim_residual_map = 1 - ssim(
            test_img, rec_img, win_size=cfg.ssim_win_size, full=True)[1]
        l1_residual_map = np.abs(test_img / 255. - rec_img / 255.)
    else:
        ssim_residual_map = ssim(test_img,
                                 rec_img,
                                 win_size=cfg.ssim_win_size,
                                 full=True,
                                 multichannel=True)[1]
        ssim_residual_map = 1 - np.mean(ssim_residual_map, axis=2)
        l1_residual_map = np.mean(np.abs(test_img / 255. - rec_img / 255.),
                                  axis=2)

    return test_img, rec_img, ssim_residual_map, l1_residual_map
    def super_resolution_(LR_path, model_list, out_dir):

        LR_arr3d = read_img(LR_path)
        nrow = LR_arr3d.shape[0]
        ncol = LR_arr3d.shape[1]

        xs = np.repeat(list(range(nrow)), ncol)
        ys = np.array(list(range(ncol)) * nrow)
        coordinates = pd.DataFrame({'x': xs, 'y': ys})

        feat_mat = coordinates.apply(get_feature_helper,
                                     LR_array3d=LR_arr3d,
                                     axis=1)
        feat_mat = np.dstack(feat_mat).transpose(
            2, 0, 1)  # shape = (nrow * ncol, 8, 3)

        pred_mat = np.empty((nrow * ncol, 4, 3))

        for i in range(12):
            pred_mat[:, i % 4,
                     i // 4] = model_list[i].predict(feat_mat[:, :, i // 4])

        HR_array3d = reshape_helper(nrow, ncol, pred_mat)
        LR_arr3d_resize = np.apply_along_axis(np.repeat, 1, LR_arr3d, 2)
        LR_arr3d_resize = np.apply_along_axis(np.repeat, 0, LR_arr3d_resize, 2)
        HR_array3d = HR_array3d + LR_arr3d_resize

        HR_array3d = HR_array3d.astype('uint8')

        HR_img = Image.fromarray(HR_array3d)
        HR_img.save(out_dir)
    def __getitem__(self, idx):
        batch_x = self.filenames[idx * self.batch_size:(idx + 1) *
                                 self.batch_size]
        batch_x = np.array(
            [read_img(filename, self.grayscale) for filename in batch_x])

        batch_x = batch_x / 255.
        return batch_x, batch_x
Пример #13
0
def make_forward_scanner(dset_name, data_dir, input_spec, scan_spec,
                         scan_params, **params):
    """ Creates a DataProvider ForwardScanner from a dset name """

    # Reading lightsheet image
    if os.path.isfile(os.path.join(data_dir, dset_name + "_img.h5")):
        img = utils.read_img(os.path.join(data_dir, dset_name + "_img.h5"))
    elif os.path.isfile(os.path.join(data_dir, dset_name + "_img.tif")):
        img = utils.read_img(os.path.join(data_dir, dset_name + "_img.tif"))

    img = (img / 255.).astype("float32")

    # Creating DataProvider Dataset
    vd = dp.Dataset(spec=input_spec)

    vd.add_data(key="input", data=img)

    # Returning DataProvider ForwardScanner
    return dp.ForwardScanner(vd, scan_spec, **scan_params)
Пример #14
0
def get_train_data(train_img_ids, df, gs):
    x_train_list = []
    y_train_list = []

    for img_id in train_img_ids:
        img = read_img(img_id)
        x_train = normalize_img(img)
        y_train = generate_mask(x_train, img_id, df, gs)
        x_train_list.append(x_train)
        y_train_list.append(y_train)

    return (x_train_list, y_train_list)
Пример #15
0
	def __getitem__(self, idx):
		batch_path = os.path.join(self.root_dir, self.batches[idx])

		if not len(self.origin_imgs[idx]):
			origin_fns = glob.glob(batch_path + '/Reference.png')
			noise_fns = glob.glob(batch_path + '/Noisy.png')

			self.origin_imgs[idx] = read_img(origin_fns[0])

			for noise_fn in noise_fns:
				self.noise_imgs[idx].append(read_img(noise_fn))
				
		origin_img = self.origin_imgs[idx]
		noise_img = self.noise_imgs[idx][np.random.randint(len(self.noise_imgs[idx]))]

		patch_origin_img, patch_noise_img = get_patch(origin_img, noise_img, self.patch_size, self.random)

		patch_origin_img_chw = hwc_to_chw(patch_origin_img)
		patch_noise_img_chw = hwc_to_chw(patch_noise_img)

		return patch_noise_img_chw, patch_origin_img_chw
Пример #16
0
def vgg19_test():
    vgg19 = VGG19('data/checkpoints/vgg_19.ckpt')
    inputs = tf.placeholder(tf.float32, [None, 224, 224, 3])
    vgg19.build_model(inputs, is_train=False)

    test_img_path = 'data/test_imgs/ILSVRC2012_val_00000003.JPEG'
    test_img = utils.read_img(test_img_path, expand_dims=True)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    output = sess.run(vgg19.fc8, feed_dict={inputs: test_img})
    prob_lable = int(np.argmax(output, axis=1))
    print(prob_lable, utils.get_class_name_by_id(prob_lable))
Пример #17
0
def get_polygons_list(img_id, df, gs):
    img = read_img(img_id)
    x_scaler, y_scaler = get_img_scalers(img, img_id, gs)

    polygons_list = []

    for class_type in range(1, len(CLASSES) + 1):
        polygons = get_polygons(img_id, class_type, df)
        polygons = affinity.scale(polygons,
                                  xfact=x_scaler,
                                  yfact=y_scaler,
                                  origin=(0, 0, 0))
        polygons_list.append(polygons)

    return polygons_list
Пример #18
0
def generator(samples, batch_size=1, augment_data=True):
    
    num_samples = len(samples)
    while 1: # Loop forever so the generator never terminates
        random.shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset+batch_size]

            images = []
            y_train = []
            for batch_sample in batch_samples:
                name = batch_sample[0].split('/')[-1]
                correction = 0
                if augment_data and random.random() > 0.75: # Augment left / right camera
                    if random.random() > 0.5: # left
                        name = batch_sample[1].split('/')[-1]
                        correction = 0.2
                    else: # right
                        name = batch_sample[2].split('/')[-1]
                        correction = -0.2
                
                filepath = os.path.join("/opt/test/drive2", "IMG", name)
                assert os.path.isfile(filepath), "Is not path: {}".format(filepath)
                center_image = utils.read_img(filepath)
                center_angle = float(batch_sample[3]) # Steering
                center_angle += correction
                throttle = float(batch_sample[4])
                brake = float(batch_sample[5])
                speed = float(batch_sample[6])
                if augment_data and random.random() > 0.5:
                    center_image = np.fliplr(center_image)
                    center_angle = - center_angle
                
                images.append(center_image)
                y_train.append([center_angle, throttle])
            
            # trim image to only see section with road
            X_train = np.array(images)
            y_train = np.array(y_train)
            y_train = y_train[:, 0]
            #X_train = (X_train - mean) / std
            # Reshape from BGR to RGB
            #print(X_train.shape, IMAGE_MEAN.shape)
            X_train = normalize_img(X_train)
            #X_train = crop_images(X_train)
            #print(X_train.shape)
            yield sklearn.utils.shuffle(X_train, y_train)
    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        img_name = self.allpaths[idx]
        image = utils.read_img(img_name)
        label = self.labels[idx]

        if self.transform:
            image = self.transform(image)

        return (image, label)


# data = ConceptData("box_above", None)

# for ix in data :
# print (ix[1])
Пример #20
0
def split_img_at_middle_by_y_axis(img_path, radio=0.10, thresh_std=5000):
    im_raw = utils.read_img(img_path)
    im_resize = utils.resize_by_percent(im_raw, radio)
    ry, rx, _ = im_resize.shape
    img_mtx0 = np.asarray(utils.rgb2binary(im_resize))
    y_sum_array0 = img_mtx0.sum(axis=0)
    tmp = array_latter_subtracts_precious(y_sum_array0 / ry)
    std0 = np.std(tmp)  # 计算标准差

    # # plt.bar(range(len(y_sum_array0)), y_sum_array0)
    # # plt.show()
    # plt.plot(range(len(y_sum_array0)-1), tmp)
    # plt.show()

    y, x, _z = im_resize.shape
    x_bias = int(x * 0.15)
    y_bias = int(y * 0.30)
    middle_x = int(x / 2)
    middle_area_img = im_resize[y_bias:y, middle_x - x_bias:middle_x + x_bias]
    img_mtx = np.asarray(utils.rgb2binary(middle_area_img))
    y_sum_array = img_mtx.sum(axis=0)
    std = np.std(y_sum_array)  # 计算标准差
    y_sum_list = list(y_sum_array)

    if std <= thresh_std:
        index = y_sum_list.index(max(y_sum_list))
    else:
        index = y_sum_list.index(min(y_sum_list))
    split_index = middle_x + index - int(len(y_sum_list) / 2)
    split_index = int(split_index / radio)

    y_raw, x_raw, _ = im_raw.shape
    img_left = im_raw[1:y_raw, 1:split_index]
    img_right = im_raw[1:y_raw, split_index + 1:x_raw]
    left_path = img_path.replace('.jpg', '_left.jpg')
    right_path = img_path.replace('.jpg', '_right.jpg')
    cv2.imencode('.jpg', img_left)[1].tofile(left_path)
    cv2.imencode('.jpg', img_right)[1].tofile(right_path)
    print(left_path)
    print(right_path)
Пример #21
0
    def __getitem__(self, index):
        item = self.df.iloc[index]
        img = read_img(item.filename, page=self.config.train.page)
        img, _ = get_tiles(
            img,
            tile_size=self.config.train.tile_size,
            n_tiles=self.config.train.n_tiles,
            mode=0,
        )
        img = concat_tiles(
            img,
            n_tiles=self.config.train.n_tiles,
            image_size=self.config.train.image_size,
            rand=self.is_train,
            transform=self.transform,
        )

        img = to_tensor(img)

        label = np.zeros(self.config.network.num_classes, dtype="float32")
        label[:item[self.config.data.labels].values.argmax()] = 1.0

        return img, torch.from_numpy(label)
Пример #22
0
def main(img_path, side_num):
    img = read_img(img_path)
    opened = pre_process(img)

    # find poly or circle
    if side_num == 0:
        approx_contours = find_circle(img, opened)
    else:
        approx_contours = find_poly(img, opened, side_num=side_num)  # temp

    # find momentums
    momentum_img = get_blank_img(img.shape)
    cv2.drawContours(momentum_img, approx_contours, -1, (0, 0, 0), 2)
    for contour in approx_contours:
        center_x, center_y = get_momentum(contour)
        print("x,y: {},{}".format(center_x, center_y))
        cv2.circle(momentum_img, (center_x, center_y), 7, 128, -1)  # 绘制中心点

    # show result
    show_img(momentum_img, 'momentums')
    cv2.imwrite("./image/momentum.png", momentum_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Пример #23
0
def main(net, args):
    img_path = args.image_path.replace("file://", "")
    img_fn = img_path.split("/")[-1]
    assert os.path.exists(img_path)
    img_origin, h_origin, w_origin = read_img(img_path)
    tmp_img = test_preprocess(img_origin, to_tensor=True,
                              pad=False).to(args.device)

    net.eval()
    torch.cuda.empty_cache()
    gc.collect()

    start = time.time()
    with torch.no_grad():
        preds = net(tmp_img)
    print(">>> Inference took {}'s".format(time.time() - start))

    if args.heatmap:
        visualize_heatmap(args, img_fn, tmp_img, preds.to('cpu')[0].numpy())
    else:
        batch = {'shape': [(h_origin, w_origin)]}
        visualize_polygon(args, img_fn, (img_origin, h_origin, w_origin),
                          batch, preds)
Пример #24
0
    def train(self, img_dir, epochs, batch_size=64, save_interval=50):
        # Load the dataset
        X_train = utils.read_img(img_dir, with_resize=True)
        # Rescale -1 to 1
        X_train = X_train * 2 - 1.
        # X_train = np.expand_dims(X_train, axis=3)

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs + 1):

            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            # Sample noise and generate a batch of new images
            noise = np.random.normal(0, 1, (batch_size, self.len_z))
            gen_imgs = self.generator.predict(noise)

            # Train the discriminator (real classified as ones and generated as zeros)
            d_loss_real = self.discriminator.train_on_batch(imgs, valid)
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # Train the generator (wants discriminator to mistake images as real)
            g_loss = self.combined.train_on_batch(noise, valid)

            # Plot the progress
            if epoch % save_interval == 0:
                print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" %
                      (epoch // save_interval, d_loss[0], 100 * d_loss[1],
                       g_loss))

            # If at save interval => save generated image samples
            if epoch % save_interval == 0:
                self.save_imgs(epoch)
Пример #25
0
outfile_path = args.out
content_image_path = args.input
gpu = args.gpu

if not args.ckpt and not args.mdl:
    utils.fail('Please provide the folder path of either checkpoint or savedmodel')

if gpu > -1:
    device = '/gpu:{}'.format(gpu)
else:
    device = '/cpu:0'

if args.ckpt:
    # Get the input shape
    original_image = utils.read_img(content_image_path).astype(np.float32) / 255.0
    shaped_input = original_image.reshape((1,) + original_image.shape)

    with tf.device(device):
        # Construct inference graph based on the input shape
        inputs = tf.placeholder(tf.float32, shaped_input.shape, name='input')
        net = stylenet.net(inputs)
        saver = tf.train.Saver(restore_sequentially=True)

        # Transfer image style
        with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:                
            input_checkpoint = tf.train.get_checkpoint_state(args.ckpt)
            saver.restore(sess, input_checkpoint.model_checkpoint_path)           
            out = sess.run(net, feed_dict={inputs: shaped_input})
else:
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
    sc.addFile("/nfs/paper-big-data-engines/utils.py")
    sc.addFile("/nfs/paper-big-data-engines/histogram/Histogram.py")
    from utils import benchmark, crawl_dir, read_img
    from Histogram import (
        calculate_histogram,
        combine_histogram,
        flatten,
        save_histogram,
    )

    print("Connected")

    # Read images
    paths = crawl_dir(os.path.abspath(args.bb_dir))
    paths = sc.parallelize(paths, len(paths))
    img_rdd = paths.map(lambda p: read_img(p, start=start, args=args))

    img_rdd = img_rdd.map(
        lambda x: flatten(x[1], start=start, args=args, filename=x[0]))

    partial_histogram = img_rdd.map(lambda x: calculate_histogram(
        x[1], args=args, start=start, filename=x[0]))

    histogram = partial_histogram.fold(
        np.array([0] * (2**16 - 1)),
        lambda x, y: combine_histogram(x, y, args=args, start=start),
    )

    save_histogram(histogram, args=args, start=start)
Пример #27
0
            # print(mn_idx)
            # mn_img_tmp = utils.to_img(mn_img)
            # utils.preview_img(mn_img_tmp)
            new_img[r_idx:r_idx_lim, c_idx:c_idx_lim] = mn_img
            gen_img = utils.to_img(new_img)
            utils.write_img(out_img_path, gen_img)

            # break
        # break

    gen_img = utils.to_img(new_img)
    return gen_img


# ------------------------------------------------------------------------
img = utils.read_img(inp_img_path)

imgs = utils.read_small_imgs(assets_dir=assets_dir,
                             num=small_imgs_num,
                             format_str="{:05d}.png")
# indexes = []
# for i in range(64):
#     indexes.append([np.random.randint(10000) for i in range(64)])

# imgt = utils.Image(imgs=imgs, index=indexes)

# img = imgt.construct_img()

# img = utils.to_img(img)

# print(img.size)
Пример #28
0
def helper(filename):
    # from matplotlib import pyplot as plt
	from utils import cv2, read_img
	import subprocess
	from gtts import gTTS
	from playsound import playsound
	max_val = 8
	max_pt = -1
	max_kp = 0

	orb = cv2.ORB_create(nfeatures=2500)
	# orb is an alternative to SIFT

    #test_img = read_img('files/test_100_2.jpg')
    #test_img = read_img('files/test_50_2.jpg')
    #test_img = read_img('files/500test.jpg')
    #test_img = read_img('files/test_100_3.jpg')
    #test_img = read_img('files/10test.jpg')
	test_img = read_img(filename)
    # resizing must be dynamic
    # original = resize_img(test_img, 0.4)
    # display('original', original)
    # keypoints and descriptors
    # (kp1, des1) = orb.detectAndCompute(test_img, None)

	(kp1, des1) = orb.detectAndCompute(test_img, None)
	
	training_set = ['files/10.1.jpg','files/10.2.jpg','files/10.3.jpg','files/10.4.jpg','files/10.5.jpg','files/10.6.jpg','files/20.1.jpg','files/20.2.jpg','files/20.3.jpg','files/20.4.jpg','files/20.5.jpg','files/20.6.jpg','files/50.1.jpg','files/50.2.jpg','files/50.3.jpg','files/50.4.jpg','files/50.5.jpg','files/50.6.jpg','files/100.1.jpg','files/100.2.jpg','files/100.3.jpg','files/100.4.jpg','files/100.5.jpg','files/100.6.jpg','files/200.1.jpg','files/200.2.jpg','files/200.3.jpg','files/200.4.jpg','files/200.5.jpg','files/200.6.jpg','files/500.1.jpg','files/500.2.jpg','files/500.3.jpg','files/500.4.jpg','files/500.5.jpg','files/500.6.jpg','files/2000.1.jpg','files/2000.2.jpg','files/2000.3.jpg','files/2000.4.jpg','files/2000.5.jpg','files/2000.6.jpg','files/10.jpg','files/10_1.jpeg','files/10back.jpg','files/10_new.jpg','files/10_newback.jpg','files/20.jpg','files/20back.jpg','files/20_new.jpg' ,'files/20_newback.jpg','files/50.jpg','files/50back.jpg', 'files/50_new.jpg', 'files/50_newback.jpg','files/100.jpg','files/100back.jpg', 'files/100_new.jpg','files/100_newback.jpg','files/200.jpg','files/500.jpg','files/500back.jpg','files/500_1.jpg','files/500_2.jpg','files/2000.jpg','files/2000back.jpg', 'files/Rs200.jpg']


	for i in range(0, len(training_set)):
        # train image
	    train_img = cv2.imread(training_set[i])

	    (kp2, des2) = orb.detectAndCompute(train_img, None)

        # brute force matcher
	    bf = cv2.BFMatcher()
	    all_matches = bf.knnMatch(des1, des2, k=2)

	    good = []
        # give an arbitrary number -> 0.789
        # if good -> append to list of good matches
	    for (m, n) in all_matches:
	        if m.distance < 0.789 * n.distance:
	            good.append([m])

	    if len(good) > max_val:
	        max_val = len(good)
	        max_pt = i
	        max_kp = kp2

	    print(i, ' ', training_set[i], ' ', len(good))

	if max_val != 8:
	    print(training_set[max_pt])
	    print('good matches ', max_val)

	    train_img = cv2.imread(training_set[max_pt])
	    img3 = cv2.drawMatchesKnn(test_img, kp1, train_img, max_kp, good, 4)

	    note = str(training_set[max_pt])[6:-4]
	    print('\nDetected denomination: Rs. ', note)

	    audio_file = 'audio/' + note + '.mp3'
	    return note
	else:
	    print('No Matches')
	    return "-1"
        frame_dir = os.path.join(opts.data_dir, opts.phase, opts.method,
                                 opts.task, opts.dataset, video)
        occ_dir = os.path.join(opts.data_dir, opts.phase, "fw_occlusion",
                               opts.dataset, video)
        flow_dir = os.path.join(opts.data_dir, opts.phase, "fw_flow",
                                opts.dataset, video)

        frame_list = glob.glob(os.path.join(frame_dir, "*.jpg"))

        err = 0
        for t in range(1, len(frame_list)):

            ### load input images
            filename = os.path.join(frame_dir, "%05d.jpg" % (t - 1))
            img1 = utils.read_img(filename)
            filename = os.path.join(frame_dir, "%05d.jpg" % (t))
            img2 = utils.read_img(filename)

            print("Evaluate Warping Error on %s-%s: video %d / %d, %s" %
                  (opts.dataset, opts.phase, v + 1, len(video_list), filename))

            ### load flow
            filename = os.path.join(flow_dir, "%05d.flo" % (t - 1))
            flow = utils.read_flo(filename)

            ### load occlusion mask
            filename = os.path.join(occ_dir, "%05d.png" % (t - 1))
            occ_mask = utils.read_img(filename)
            noc_mask = 1 - occ_mask
        input_dir = os.path.join(opts.data_dir, opts.phase, "input",
                                 opts.dataset, video)
        process_dir = os.path.join(opts.data_dir, opts.phase, "processed",
                                   opts.task, opts.dataset, video)
        output_dir = os.path.join(opts.data_dir, opts.phase, opts.method,
                                  opts.task, opts.dataset, video)

        frame_list = glob.glob(os.path.join(input_dir, "*.jpg"))

        dist = 0
        for t in range(1, len(frame_list)):

            ### load processed images
            filename = os.path.join(process_dir, "%05d.jpg" % (t))
            P = utils.read_img(filename)

            ### load output images
            filename = os.path.join(output_dir, "%05d.jpg" % (t))
            O = utils.read_img(filename)

            print("Evaluate LPIPS on %s-%s: video %d / %d, %s" %
                  (opts.dataset, opts.phase, v + 1, len(video_list), filename))

            ### convert to tensor
            P = utils.img2tensor(P)
            O = utils.img2tensor(O)

            ### scale to [-1, 1]
            P = P * 2.0 - 1
            O = O * 2.0 - 1