Ejemplo n.º 1
0
def process():

    input_path = generate_random_filename(upload_directory,"jpg")
    output_path = generate_random_filename(upload_directory,"jpg")

    try:

        url = request.json["url"]
        # phone: iphone, blackberr or sony
        phone = request.json["phone"]
        # resolution: orig,high,medium,small,tiny
        resolution = request.json["resolution"]

        download(url, input_path)
       
        # get the specified image resolution
        IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_SIZE = utils.get_specified_res(res_sizes, phone, resolution)

        # create placeholders for input images
        x_ = tf.placeholder(tf.float32, [None, IMAGE_SIZE])
        x_image = tf.reshape(x_, [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 3])
            
        # generate enhanced image
        enhanced = resnet(x_image)


        with tf.Session(config=config) as sess:
            saver = tf.train.Saver()
            saver.restore(sess, "models_orig/" + phone + "_orig")
            image = np.float16(misc.imresize(misc.imread(filename), res_sizes[phone])) / 255
            image_crop = utils.extract_crop(image, resolution, phone, res_sizes)
            image_crop_2d = np.reshape(image_crop, [1, IMAGE_SIZE])
            enhanced_2d = sess.run(enhanced, feed_dict={x_: image_crop_2d})
            enhanced_image = np.reshape(enhanced_2d, [IMAGE_HEIGHT, IMAGE_WIDTH, 3])
            misc.imsave(filename, enhanced_image)
    
        callback = send_file(output_path, mimetype='image/jpeg')

        return callback, 200


    except:
        traceback.print_exc()
        return {'message': 'input error'}, 400

    finally:
        clean_all([
            input_path,
            output_path
            ])
Ejemplo n.º 2
0
        # load pre-trained model
        saver = tf.train.Saver()
        saver.restore(sess, "models_orig/" + phone)

        for photo in test_photos:

            # load training image and crop it if necessary

            print("Testing original " + phone.replace("_orig", "") +
                  " model, processing image " + photo)
            image = np.float16(
                misc.imresize(misc.imread(test_dir + photo),
                              res_sizes[phone])) / 255

            image_crop = utils.extract_crop(image, resolution, phone,
                                            res_sizes)
            image_crop_2d = np.reshape(image_crop, [1, IMAGE_SIZE])

            # get enhanced image

            enhanced_2d = sess.run(enhanced, feed_dict={x_: image_crop_2d})
            enhanced_image = np.reshape(enhanced_2d,
                                        [IMAGE_HEIGHT, IMAGE_WIDTH, 3])

            before_after = np.hstack((image_crop, enhanced_image))
            photo_name = photo.rsplit(".", 1)[0]

            # save the results as .png images

            misc.imsave(
                "visual_results/" + phone + "_" + photo_name + "_enhanced.png",
Ejemplo n.º 3
0
def input_generator(filesDIR):
    batch_rois = []
    batch_inds = []
    batch_fmaps = []
    image_counter = 0

    batch_scores = []
    batch_targets = []
    scan_counter = 0
    while 1:
        print('scans: ', scan_counter)
        for f in listdir(filesDIR):
            scan_counter += 1
            data = asarray(Image.open(filesDIR + f)) / 255.0

            feature_map_for_RoIPool = CNN_model_RoI.predict(
                data.reshape(-1, data.shape[0], data.shape[1], data.shape[2]))
            feature_stride_for_ROIPool = int(image_size /
                                             feature_map_for_RoIPool.shape[1])
            # normalization
            feature_map_mean = np.mean(feature_map_for_RoIPool)
            feature_map_std = np.std(feature_map_for_RoIPool)
            feature_map_for_RoIPool = (feature_map_for_RoIPool -
                                       feature_map_mean) / feature_map_std

            del data

            true_bboxes = np.array([
                row[3] / r for row in csv_data.values
                if row[0] == f.replace('.jpg', '')
            ])
            if len(true_bboxes) == 0:
                continue

            true_bboxes = xywh_xyXY(true_bboxes)

            enlarged_bboxes = enlarge(true_bboxes, bbox_padding=15)

            rois, targets, scores = produce_batch(filesDIR + f, true_bboxes)

            if len(rois) <= 0:
                continue

            batch_fmaps.append(feature_map_for_RoIPool[0])
            image_counter += 1

            for i in range(len(rois)):
                crop = extract_crop(rois[i])
                batch_rois.append(crop)
                batch_inds.append(int(image_counter - 1))

                batch_scores.append(scores[i])
                batch_targets.append(targets[i])

                if (len(batch_rois) == BATCH_SIZE):
                    all_fmaps = np.zeros(
                        (len(batch_rois), feature_map_for_RoIPool.shape[1],
                         feature_map_for_RoIPool.shape[2],
                         feature_map_for_RoIPool.shape[3]))
                    # The input must share first dimension. We thus fill the batch of fmaps with zeros
                    for useful_ind in range(image_counter):
                        all_fmaps[useful_ind] = batch_fmaps[useful_ind]

                    batch_pooled_rois = tf.image.crop_and_resize(
                        np.asarray(all_fmaps), np.asarray(batch_rois),
                        np.asarray(batch_inds), (RoI_Pool_size, RoI_Pool_size))

                    if not a.any() or not b.any() or not c.any() or not d.any(
                    ):
                        print("empty array found.")
                    yield batch_pooled_rois, [
                        np.asarray(batch_targets),
                        to_categorical(np.asarray(batch_scores))
                    ]
                    batch_rois = []
                    batch_inds = []

                    batch_scores = []
                    batch_targets = []

                    batch_fmaps = []
                    image_counter = 0
                    # Scanning of rois in image continues if scan over rois hasnt ended. We need to keep the corresponding fmap!
                    if (i < len(rois) - 1):
                        batch_fmaps.append(feature_map_for_RoIPool[0])
                        image_counter += 1