コード例 #1
0
    def display_image_id(self, image_id, json_data):
        path = str('./Dataset_COCO_5k_images')

        img_id_annotation = 0
        count = 0
        for i in json_data['annotations']:
            if (json_data['annotations'][count]['image_id'] == image_id):
                img_id_annotation = count
                break
            count += 1

        image = None  # Image-object
        image_caption = None
        for filename in os.listdir(path):
            if (int(filename[1:-4]) == json_data['annotations']
                [img_id_annotation]['image_id']):
                image = Image.open(
                    os.path.join('Dataset_COCO_5k_images', filename))
                image_caption = json_data['annotations'][img_id_annotation][
                    'caption']
                print('Image-Filename: {}'.format(filename))
                print('Image-shape: {}'.format(image.size))
                print('Image-captioning: {}'.format(image_caption))
                plt.imshow(image)
                plt.show()
                image.close()
                break
コード例 #2
0
def generate_samples_end_to_end(filename, patch_size, seed, batch_size=100):
    prng = random.Random()
    prng.seed(seed)

    batch = ([], [])
    with tarfile.open(filename, 'r:*') as tar:
        entries = [x.name for x in tar.getmembers() if x.isreg()]

        while True:
            random.shuffle(entries)

            for thing in entries:
                with tar.extractfile(thing) as jpeg:
                    image = Image.open(jpeg)
                    image.load()
                    try:
                        pix = np.array(image)

                        dynamic_range_factor = 0.99
                        offset = (1 - dynamic_range_factor) / 2

                        if 3 == len(
                                pix.shape
                        ) and 3 == pix.shape[2] and pix.shape[0] > patch_size[
                                0] and pix.shape[1] > patch_size[1]:

                            for i in range(10):
                                offset_row = math.floor(
                                    prng.uniform(0,
                                                 pix.shape[0] - patch_size[0]))
                                offset_col = math.floor(
                                    prng.uniform(0,
                                                 pix.shape[1] - patch_size[1]))
                                chip = dynamic_range_factor * pix[offset_row:(
                                    offset_row + patch_size[0]), offset_col:(
                                        offset_col + patch_size[1]), :].copy(
                                        ).astype(np.float32) / 255 + offset

                                if (patch_size[0], patch_size[1],
                                        3) == chip.shape:
                                    batch[0].append(chip)
                                    batch[1].append(chip)

                                    if len(batch[0]) == batch_size:
                                        yield np.stack(batch[0],
                                                       axis=0), np.stack(
                                                           batch[1], axis=0)
                                        batch[0].clear()
                                        batch[1].clear()
                                        gc.collect()

                        del pix
                    finally:
                        image.close()
                        del image
コード例 #3
0
 def COCO_images_segmentation(self, num_img=100):
     image_array_segmentation = []
     path = str('./Dataset_COCO_5k_images_segmentation')
     count = 0
     for filename in tqdm(os.listdir(path)):
         if (count >= num_img):
             break
         image = Image.open(
             os.path.join('Dataset_COCO_5k_images_segmentation', filename))
         image_array_segmentation.append(filename)
         image.close()
         count += 1
     return image_array_segmentation
コード例 #4
0
                                                     int(100 * scores[j]))

                        print('cropping box {0} ({1}, {2}, {3}, {4})'.format(
                            display_str, xmin_px, ymin_px, xmax_px, ymax_px))

                        cropped_img = image.crop(
                            (xmin_px, ymin_px, xmax_px, ymax_px))
                        with tf.gfile.Open(
                                os.path.join(
                                    PATH_TO_TEST_IMAGES_CROP_RES_DIR,
                                    '{0}_{1}_{2}.jpg'.format(
                                        image_path_filename, display_str, j)),
                                'w') as fid:
                            cropped_img.save(fid, 'JPEG')

                        features = get_feature(cropped_img)
                        w.writerow({
                            "filename": image_path,
                            "object_class": class_name,
                            "score": scores[j],
                            "ymin": ymin,
                            "xmin": xmin,
                            "ymax": ymax,
                            "xmax": xmax,
                            "features": ','.join(features)
                        })

                        cropped_img.close()

            image.close()