Ejemplo n.º 1
0
    def create_tf_example_other(self, example, filename):
        """
        OTHER
        """
        #print(filename)
        filename = filename.encode()
        with tf.gfile.GFile(filename, 'rb') as fid:
            encoded_image = fid.read()
        image = Image.open(filename)
        (width, height) = image.size
        image_string = np.array(image).tostring()
        #image_format = 'png'.encode()
        image_format = 'jpg'.encode()
        xmins = []
        xmaxs = []
        ymins = []
        ymaxs = []
        classes_text = []
        classes = []
        for box in example['annotations']:
            box_x = box['xmin']
            box_y = box['ymin']
            box_width = box['x_width']
            box_height = box['y_height']
            xmins.append(float(box_x / width))
            xmaxs.append(float((box_x + box_width) / width))
            ymins.append(float(box_y / height))
            ymaxs.append(float((box_y + box_height) / height))
            classes_text.append(box['class'].encode('utf-8'))
            print("[", box['class'].encode('utf-8'), "]")
            classes.append(int(DICT_LABEL_OTHER[box['class']]))

        tf_example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/height':
                dataset_util.int64_feature(height),
                'image/width':
                dataset_util.int64_feature(width),
                'image/filename':
                dataset_util.bytes_feature(filename),
                'image/source_id':
                dataset_util.bytes_feature(filename),
                'image/encoded':
                dataset_util.bytes_feature(encoded_image),
                'image/format':
                dataset_util.bytes_feature(image_format),
                'image/object/bbox/xmin':
                dataset_util.float_list_feature(xmins),
                'image/object/bbox/xmax':
                dataset_util.float_list_feature(xmaxs),
                'image/object/bbox/ymin':
                dataset_util.float_list_feature(ymins),
                'image/object/bbox/ymax':
                dataset_util.float_list_feature(ymaxs),
                #'image/object/class/text' : dataset_util.bytes_list_feature(classes_text),
                'image/object/class/label':
                dataset_util.int64_list_feature(classes),
                #'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_string])),
            }))
        return tf_example
Ejemplo n.º 2
0
def create_tf_example(group, path):
    """Creates a tf.Example proto from sample buillding image tile.

    Args:
     encoded_building_image_data: The jpg encoded data of the building image.

    Returns:
     example: The created tf.Example.
    """
    with tf.gfile.GFile(op.join(path, '{}'.format(group.filename)),
                        'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size
    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for _, row in group.object.iterrows():
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(tags[row['class_num']].encode('utf8'))
        classes.append(row['class_num'])

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_jpg),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 3
0
def dict_to_tf_example(image_path,label_path):
    """Convert image and label to tf.Example proto.
    ----
    Args:
    ----
      image_path: Path to a single image.

      label_path: Path to its corresponding segmentation label.
    ----
    Returns:
    ----
      example: The converted tf.Example.

    Raises:
      ValueError: if the size of image does not match with that of label.
    """
    # Reading an image
    image = load_image(image_path, (OUTPUT_HEIGHT, OUTPUT_WIDTH)).astype(np.uint8)

    label = load_image(label_path, (OUTPUT_HEIGHT, OUTPUT_WIDTH), is_label=True)

    if image.shape[0:2] != label.shape[0:2]:
        raise ValueError('The size of image does not match with that of label.')

    # Create the TFRecord example
    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(image.shape[0]),
        'image/width': dataset_util.int64_feature(image.shape[1]),
        'image/encoded': dataset_util.bytes_feature(tf.compat.as_bytes(image.tostring())),
        'label/encoded': dataset_util.bytes_feature(tf.compat.as_bytes(label.tostring())),
    }))
    return example
Ejemplo n.º 4
0
def dict_to_tf_example(image_path, label_path):
    '''格式转换成tfrecord
    参数:
      image_path:输入图片地址
      label_path:输出label地址
      '''
    with tf.gfile.GFile(image_path, 'rb') as f:
        encoder_jpg = f.read()
    encoder_jpg_io = io.BytesIO(encoder_jpg)
    image = Image.open(encoder_jpg_io)

    if image.format != 'JPEG':
        tf.logging.info('输入图片格式错误')
        raise ValueError('输入图片格式错误')

    with tf.gfile.GFile(label_path, 'rb') as f:
        encoder_label = f.read()
    encoder_label_io = io.BytesIO(encoder_label)
    label = Image.open(encoder_label_io)

    if label.format != 'PNG':
        tf.logging.info('label图片格式错误')
        raise ValueError('label图片格式错误')

    if image.size != label.size:
        tf.logging.info('输入输出没对上')
        raise ValueError('输入输出没对上')

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image': dataset_util.bytes_feature(encoder_jpg),
            'label': dataset_util.bytes_feature(encoder_label)
        }))
    return example
def parse_test_example(f, images_path):
    height = None  # Image height
    width = None  # Image width
    filename = None  # Filename of the image. Empty if image is not from file
    encoded_image_data = None  # Encoded image bytes
    image_format = b'jpeg'  # b'jpeg' or b'png'

    filename = f.readline().rstrip()
    if not filename:
        raise IOError()

    filepath = os.path.join(images_path, filename)

    image_raw = cv2.imread(filepath)

    encoded_image_data = open(filepath, "rb").read()
    key = hashlib.sha256(encoded_image_data).hexdigest()

    height, width, channel = image_raw.shape

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': dataset_util.int64_feature(int(height)),
            'image/width': dataset_util.int64_feature(int(width)),
            'image/filename': dataset_util.bytes_feature(
                filename.encode('utf-8')),
            'image/source_id': dataset_util.bytes_feature(
                filename.encode('utf-8')),
            'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
            'image/encoded': dataset_util.bytes_feature(encoded_image_data),
            'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
        }))

    return tf_example
Ejemplo n.º 6
0
def create_tf_example(group, path, dictionary):
    with tf.io.gfile.GFile(
            os.path.join(path, '{}'.format(group.filename)) + '.jpg',
            'rb') as fid:
        # with tf.io.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:## THis is 2.0 tf version of gfile
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    filename = group.filename.encode('utf8')
    # print(filename,path)
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(row['class'].encode('utf8'))
        classes.append(class_text_to_int(row['class'], dictionary))
        # didt = {'quantity': 2, 'product': 1}
        # classes.append(didt[row['class']])
        # print(classes)

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_jpg),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
def _image_to_tfexample(image_name, annotation_name):
    """Generate a tf example by image and annotation file."""
    image_data = tf.gfile.FastGFile(image_name, 'rb').read()
    tree = ElementTree.parse(annotation_name)
    root = tree.getroot()

    # image shape
    size = root.find('size')
    height = int(size.find('height').text)
    width = int(size.find('width').text)
    channels = int(size.find('depth').text)

    # image annotations
    xmin = []
    xmax = []
    ymin = []
    ymax = []
    labels = []
    labels_text = []
    difficult = []
    truncated = []
    for obj in root.findall('object'):
        label_name = obj.find('name').text
        labels.append(int(VOC_LABELS[label_name][0]))
        labels_text.append(label_name.encode('ascii'))

        if obj.find('difficult'):
            difficult.append(int(obj.find('difficult').text))
        else:
            difficult.append(0)

        if obj.find('truncated'):
            truncated.append(int(obj.find('truncated').text))
        else:
            truncated.append(0)

        bbox = obj.find('bndbox')
        xmin.append(float(bbox.find('xmin').text) / width)
        xmax.append(float(bbox.find('xmax').text) / width)
        ymin.append(float(bbox.find('ymin').text) / height)
        ymax.append(float(bbox.find('ymax').text) / height)

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/encoded': bytes_feature(image_data),
            'image/format': bytes_feature(b'JPEG'),
            'image/height': int64_feature(height),
            'image/width': int64_feature(width),
            'image/channels': int64_feature(channels),
            'image/object/bbox/xmin': float_list_feature(xmin),
            'image/object/bbox/xmax': float_list_feature(xmax),
            'image/object/bbox/ymin': float_list_feature(ymin),
            'image/object/bbox/ymax': float_list_feature(ymax),
            'image/object/bbox/label': int64_list_feature(labels),
            'image/object/bbox/text': bytes_list_feature(labels_text),
            'image/object/bbox/difficult': int64_list_feature(difficult),
            'image/object/bbox/truncated': int64_list_feature(truncated),
        }))
    return example
Ejemplo n.º 8
0
def dict_to_tf_example(data,
                       dataset_directory,
                       ignore_difficult_instances=False,
                       image_subdirectory='JPEGImages'):
    img_path = os.path.join(data['folder'], image_subdirectory, data['filename'])
    full_path = os.path.join(dataset_directory, img_path)
    with tf.gfile.GFile(full_path, 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = PIL.Image.open(encoded_jpg_io)
    if image.format != 'JPEG':
        raise ValueError('Image format not JPEG')
    width = int(data['size']['width'])
    height = int(data['size']['height'])

    xmin = []
    ymin = []
    xmax = []
    ymax = []
    classes = []
    classes_text = []
    truncated = []
    poses = []
    difficult_obj = []
    for obj in data['object']:
        difficult = bool(int(obj['difficult']))
        if ignore_difficult_instances and difficult:
            continue

        difficult_obj.append(int(difficult))

        xmin.append(float(obj['bndbox']['xmin']) / width)
        ymin.append(float(obj['bndbox']['ymin']) / height)
        xmax.append(float(obj['bndbox']['xmax']) / width)
        ymax.append(float(obj['bndbox']['ymax']) / height)
        classes_text.append(obj['name'].encode('utf8'))
        truncated.append(int(obj['truncated']))
        poses.append(obj['pose'].encode('utf8'))

    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(
            data['filename'].encode('utf8')),
        'image/source_id': dataset_util.bytes_feature(
            data['filename'].encode('utf8')),
        'image/encoded': dataset_util.bytes_feature(encoded_jpg),
        'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
        'image/object/difficult': dataset_util.int64_list_feature(difficult_obj),
        'image/object/truncated': dataset_util.int64_list_feature(truncated),
        'image/object/view': dataset_util.bytes_list_feature(poses),
    }))
    return example
Ejemplo n.º 9
0
def create_tf_example(frame, label_map_dict):
    # TODO(user): Populate the following variables from your example.
    height = frame['height']  # Image height
    width = frame['width']  # Image width
    filename = '{}.jpg'.format(
        frame['frame_id']
    )  # Filename of the image. Empty if image is not from file
    img_path = os.path.join(FLAGS.image_dir, filename)
    filename = filename.encode()
    with tf.gfile.GFile(img_path, 'rb') as fid:
        encoded_image_data = fid.read()  # Encoded image bytes
    image_format = b'jpeg'  # b'jpeg' or b'png'

    xmins = [
        float(bbox[0]) / width for bbox in frame['bboxes']
    ]  # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = [float(bbox[2]) / width for bbox in frame['bboxes']
             ]  # List of normalized right x coordinates in bounding box
    # (1 per box)
    ymins = [
        float(bbox[1]) / height for bbox in frame['bboxes']
    ]  # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = [float(bbox[3]) / height for bbox in frame['bboxes']
             ]  # List of normalized bottom y coordinates in bounding box
    # (1 per box)
    classes_text = [name.encode() for name in frame['names']
                    ]  # List of string class name of bounding box (1 per box)
    classes = [label_map_dict[name] for name in frame['names']
               ]  # List of integer class id of bounding box (1 per box)

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_image_data),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 10
0
def create_tf_example(bboxes, img_info, category_name2id,class_mapper={}):
  # TODO(user): Populate the following variables from your example.
  

  height = img_info['height']
  width = img_info['width']
  filename = img_info['path']
  with tf.gfile.GFile(filename, 'rb') as fid:
    encoded_jpg = fid.read()
   
  image_format = img_info['format'] 
  
  

  xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
  xmaxs = [] # List of normalized right x coordinates in bounding box
             # (1 per box)
  ymins = [] # List of normalized coordinates in bounding box (1 per box)
  ymaxs = [] # List of normalized bottom y coordinates in bounding box
             # (1 per box)
  classes_text = [] # List of string class name of bounding box (1 per box)
  classes = [] # List of integer class id of bounding box (1 per box)
    
  for bbox in bboxes:
            
    xmin = float(bbox['x1']) / width
    xmax = float(bbox['x1'] + bbox['width']) / width
    ymin = float(bbox['y1']) / height 
    ymax = float(bbox['y1'] + bbox['height']) / height        
    class_text = class_mapper[bbox['label']] if bbox['label'] in class_mapper else bbox['label']
    if class_text == "__background__":
        continue
    class_id = category_name2id[class_text]

    xmins.append(xmin)
    xmaxs.append(xmax)
    ymins.append(ymin)
    ymaxs.append(ymax)    
    classes_text.append(str(class_text))        
    classes.append(class_id)

  
  tf_example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(os.path.basename(filename)),
      'image/source_id': dataset_util.bytes_feature(filename),
      'image/encoded': dataset_util.bytes_feature(encoded_jpg),
      'image/format': dataset_util.bytes_feature(image_format),
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
  }))
  return tf_example
def create_tf_example(group, path):
    with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)),
                        'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size
    # print('width, height',width, height)

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        print('row', row)
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(row['class'].encode('utf8'))

        classes.append(class_text_to_int(row['class']))
    print('classes', classes)
    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_jpg),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 12
0
def create_tf_example(group):
    encoded_img = object_storage.get_object(namespace, 'images',
                                            group.filename).data.content
    #with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
    #encoded_jpg = fid.read()
    #encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(io.BytesIO(encoded_img))
    if group.filename.endswith('.png'):
        image = image.convert('RGB')
    width, height = image.size

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(row['class'].encode('utf8'))
        classes.append(row_labels[row['class']])

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_img),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 13
0
def create_tf_example(ex_name, ex_xmins, ex_xmaxs, ex_ymins, ex_ymaxs):
    # TODO(user): Populate the following variables from your example.
    image = Image.open(os.path.join(PATH, ex_name))
    width, height = image.size
    '''
  height = ex_height # Image height
  width = ex_width # Image width
  '''
    filename = ex_name  # Filename of the image. Empty if image is not from file
    encoded_image_data = image.tobytes()  # Encoded image bytes
    image_format = b'jpg'  # b'jpeg' or b'png'

    xmins = [
        ex_xmins
    ]  # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = [ex_xmaxs
             ]  # List of normalized right x coordinates in bounding box
    # (1 per box)
    ymins = [
        ex_ymins
    ]  # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = [ex_ymaxs
             ]  # List of normalized bottom y coordinates in bounding box
    # (1 per box)
    classes_text = ['Shoes'
                    ]  # List of string class name of bounding box (1 per box)
    classes = [91]  # List of integer class id of bounding box (1 per box)

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_image_data),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 14
0
def dict_to_tf_example(depth_map_path, normal_map_path, foreground_map_path,
                       point_path, sdf_path):
    """
    Convert gray-scale depth map to tf.Example proto.
    ----
    Args:
    ----
      xxx_path: Path to a corresponding data.
    ----
    Returns:
    ----
      example: The converted tf.Example.
    """
    # Reading depth map, scale, quaternion, points and sdf
    depth_map = load_image(depth_map_path,
                           (OUTPUT_HEIGHT, OUTPUT_WIDTH)).astype(np.uint8)
    normal_map = load_image(normal_map_path,
                            (OUTPUT_HEIGHT, OUTPUT_WIDTH)).astype(np.uint8)
    foreground_map = load_image(foreground_map_path,
                                (OUTPUT_HEIGHT, OUTPUT_WIDTH)).astype(np.uint8)

    # flatten here is necessary, otherwise the multi-dimensional ndarray cannot be stored in tf records
    points = np.squeeze(load_npy(point_path))[0:NUM_POINTS, :].flatten()
    sdf = load_npy(sdf_path)[0:NUM_POINTS, :].flatten()

    # Create the TFRecord example
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'depth_map/height':
            dataset_util.int64_feature(depth_map.shape[0]),
            'depth_map/width':
            dataset_util.int64_feature(depth_map.shape[1]),
            'depth_map/encoded':
            dataset_util.bytes_feature(tf.compat.as_bytes(
                depth_map.tostring())),
            'normal_map/height':
            dataset_util.int64_feature(normal_map.shape[0]),
            'normal_map/width':
            dataset_util.int64_feature(normal_map.shape[1]),
            'normal_map/encoded':
            dataset_util.bytes_feature(
                tf.compat.as_bytes(normal_map.tostring())),
            'foreground_map/height':
            dataset_util.int64_feature(foreground_map.shape[0]),
            'foreground_map/width':
            dataset_util.int64_feature(foreground_map.shape[1]),
            'foreground_map/encoded':
            dataset_util.bytes_feature(
                tf.compat.as_bytes(foreground_map.tostring())),
            'points':
            dataset_util.float_list_feature(points),
            'sdf':
            dataset_util.float_list_feature(sdf)
        }))
    return example
Ejemplo n.º 15
0
def create_tf_example(image_path, example, image_id):
    """
    Create a tf_example using @example.
    @example is of form : ["ImageID", "XMin", "XMax", "YMin", "YMax"] which are the columns of "fish.csv".
    @example contains all bounding boxes for the image with @image_id
    """
    
    filename = image_id+'.jpg'
    
    image_path = os.path.join(image_path, filename)


    with tf.gfile.GFile(image_path, 'rb') as fid:
        encoded_image_data = fid.read()
        
    filename = filename.encode()
    image = Image.open(image_path)
    width, height = image.size
    """
    if filename == "147441736948406.jpg":
      image.show()
    """
    del image
    
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    
    for bbox in np.array(example[['XMin', 'XMax', 'YMin', 'YMax']]):
        xmins += [bbox[0]]
        xmaxs += [bbox[1]]
        ymins += [bbox[2]]
        ymaxs += [bbox[3]]
    
    classes_text = [b'fish']*len(example)
    classes = [1]*len(example)

    tf_example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(filename),
      'image/source_id': dataset_util.bytes_feature(filename),
      'image/encoded': dataset_util.bytes_feature(encoded_image_data),
      'image/format': dataset_util.bytes_feature(image_format),
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example
Ejemplo n.º 16
0
def dict_to_tf_example(image_path, label_path):
    """Convert image and label to tf.Example proto.

    Args:
      image_path: Path to a single PASCAL image.
      label_path: Path to its corresponding label.

    Returns:
      example: The converted tf.Example.

    Raises:
      ValueError: 
    #               if the image pointed to by image_path is not a valid JPEG or
    #               if the label pointed to by label_path is not a valid PNG or
                  if the size of image does not match with that of label.
    """
    with tf.gfile.GFile(image_path, 'rb') as fid:
        encoded_img = fid.read()
    encoded_img_io = io.BytesIO(encoded_img)
    image = PIL.Image.open(encoded_img_io)
    # if image.format != 'JPEG':
    #     raise ValueError('Image format not JPEG')
    img_ext = image_path.split('.')[-1]

    with tf.gfile.GFile(label_path, 'rb') as fid:
        encoded_label = fid.read()
    encoded_label_io = io.BytesIO(encoded_label)
    label = PIL.Image.open(encoded_label_io)
    # if label.format != 'PNG':
    #     raise ValueError('Label format not PNG')
    lbl_ext = label_path.split('.')[-1]

    if image.size != label.size:
        print('[Info]')
        print('    image_path : ', image_path)
        print('    label_path : ', label_path)
        print('    image.size, label.size :  {}, {}'.format(
            image.size, label.size))
        raise ValueError(
            'The size of image does not match with that of label.')

    width, height = image.size

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': dataset_util.int64_feature(height),
            'image/width': dataset_util.int64_feature(width),
            'image/encoded': dataset_util.bytes_feature(encoded_img),
            'image/format': dataset_util.bytes_feature(img_ext.encode('utf8')),
            'label/encoded': dataset_util.bytes_feature(encoded_label),
            'label/format': dataset_util.bytes_feature(lbl_ext.encode('utf8')),
        }))
    return example
Ejemplo n.º 17
0
def create_tf_example(entry):
    height = entry['height']  # Image height
    width = entry['width']  # Image width
    filename = entry['file_name'].encode(
    )  # Filename of the image. Empty if image is not from file
    image_format = b'jpeg'  # b'jpeg' or b'png'
    encoded_image_data = open(BASE_DIR + filename.decode('ascii'),
                              'rb').read()  # Encoded image bytes
    xmins = [
        float(entry['bbox'][0] / width)
    ]  # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = [
        float((entry['bbox'][0] + entry['bbox'][2]) / width)
    ]  # List of normalized right x coordinates in bounding box # (1 per box)
    ymins = [
        float(entry['bbox'][1] / height)
    ]  # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = [
        float((entry['bbox'][1] + entry['bbox'][3]) / height)
    ]  # List of normalized bottom y coordinates in bounding box # (1 per box)
    classes_text = [entry['name'].encode()
                    ]  # List of string class name of bounding box (1 per box)
    classes = [entry['category_id']
               ]  # List of integer class id of bounding box (1 per box)

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_image_data),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 18
0
def create_tf_example(name, image_dir, annot_dir):
    image_path = os.path.join(image_dir, name+'.jpg')
    annot_path = os.path.join(annot_dir, name+'.mat')
    annot_mat = parse_coordinates(annot_path)
    with tf.gfile.GFile(image_path, 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    print (encoded_jpg)
    exit(1)
    image = Image.open(encoded_jpg_io)
    width, height = image.size
    filename = name.encode('utf8')
    image_format = b'jpg'
    # check if the image format is matching with your images.
    label = 'hand'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for coord in annot_mat:

        x_max, x_min, y_max, y_min = 0, float('inf'), 0, float('inf')
        for y, x in coord:
            x_max, x_min = max(x, x_max), min(x, x_min)
            y_max, y_min = max(y, y_max), min(y, y_min)
        # normalized cordinates
        # box cordinates in faster rcnn uses 0 and 1 to define the position of the bounding boxes.
        # so if my value is greater than 1, select 1
        xmins.append(max(float(x_min) / width, 0.0))
        ymins.append(max(float(y_min) / height, 0.0))
        xmaxs.append(min(float(x_max) / width, 1.0))
        ymaxs.append(min(float(y_max) / height, 1.0))
        classes_text.append(label.encode('utf8'))
        classes.append(class_text_to_int(label))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(encoded_jpg),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example
Ejemplo n.º 19
0
def dict_to_tf_example(image_af_path, image_bf_path, label_path):
    """Convert image and label to tf.Example proto.

  Args:
    image_path: Path to a single PASCAL image.
    label_path: Path to its corresponding label.

  Returns:
    example: The converted tf.Example.

  Raises:
    ValueError: if the image pointed to by image_path is not a valid JPEG or
                if the label pointed to by label_path is not a valid PNG or
                if the size of image does not match with that of label.
  """
    with tf.gfile.GFile(image_af_path, 'rb') as fid:
        encoded_jpg_af = fid.read()
    encoded_jpg_af_io = io.BytesIO(encoded_jpg_af)
    image_af = PIL.Image.open(encoded_jpg_af_io)
    if image_af.format != 'JPEG':
        raise ValueError('Image format not JPEG')

    with tf.gfile.GFile(image_bf_path, 'rb') as fid:
        encoded_jpg_bf = fid.read()
    encoded_jpg_bf_io = io.BytesIO(encoded_jpg_bf)
    image_bf = PIL.Image.open(encoded_jpg_bf_io)
    if image_bf.format != 'JPEG':
        raise ValueError('Image format not JPEG')
    with tf.gfile.GFile(label_path, 'rb') as fid:
        encoded_label = fid.read()
    encoded_label_io = io.BytesIO(encoded_label)
    label = PIL.Image.open(encoded_label_io)
    if label.format != 'PNG':
        raise ValueError('Label format not PNG')

    if image_af.size != label.size:
        raise ValueError(
            'The size of image does not match with that of label.')

    width, height = image_af.size

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': dataset_util.int64_feature(height),
            'image/width': dataset_util.int64_feature(width),
            'image_af/encoded': dataset_util.bytes_feature(encoded_jpg_af),
            'image_bf/encoded': dataset_util.bytes_feature(encoded_jpg_bf),
            'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
            'label/encoded': dataset_util.bytes_feature(encoded_label),
            'label/format': dataset_util.bytes_feature('png'.encode('utf8')),
        }))
    return example
Ejemplo n.º 20
0
def create_tf_example(example):
    filename = example['filename'] # Filename of the image. Empty if image is not from file
    filename = filename.encode()

    with tf.gfile.GFile(example['filename'], 'rb') as fid:
        encoded_image = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_image)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    image_format = 'jpg'.encode()

    xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = [] # List of normalized right x coordinates in bounding box
                # (1 per box)
    ymins = [] # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = [] # List of normalized bottom y coordinates in bounding box
                # (1 per box)
    classes_text = [] # List of string class name of bounding box (1 per box)
    classes = [] # List of integer class id of bounding box (1 per box)

    for box in example['annotations']:
        # adding box, one image may have multiple detected boxes
        if box['xmin'] + box['x_width'] > width or box['ymin']+ box['y_height'] > height:
            continue

        xmins.append(float(box['xmin']) / width)
        xmaxs.append(float(box['xmin'] + box['x_width']) / width)
        ymins.append(float(box['ymin']) / height)
        ymaxs.append(float(box['ymin']+ box['y_height']) / height)
        classes_text.append(box['class'].encode())
        classes.append(int(LABEL_DICT[box['class']]))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(encoded_image),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))

    return tf_example
Ejemplo n.º 21
0
def create_tf_feature(image_file_path: pathlib.PosixPath,
                      camera_token: str,
                      corner_list: np.ndarray,
                      image_width: int,
                      image_height: int, boxes: List[Box]) -> tf.train.Example:
    box_feature_list = [(box.name, box.token, object_idx_dict[box.name]) for box in boxes]
    box_feature_list = list(map(list, zip(*box_feature_list)))

    BOX_NAME_INDEX = 0
    BOX_TOKEN_INDEX = 1
    BOX_NAME_ID_INDEX = 2
    classes_text_list = [s.encode('utf-8') for s in box_feature_list[BOX_NAME_INDEX]]
    anns_token_list = [s.encode('utf-8') for s in box_feature_list[BOX_TOKEN_INDEX]]

    with tf.gfile.GFile(image_file_path.as_posix(), 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = PIL.Image.open(encoded_jpg_io)
    if image.format != 'JPEG':
        raise ValueError('Image format not JPEG')
    key = hashlib.sha256(encoded_jpg).hexdigest()

    file_basename = image_file_path.as_posix()

    feature_dict = {
        'image/height': dataset_util.int64_feature(image_height),
        'image/width': dataset_util.int64_feature(image_width),
        'image/filename': dataset_util.bytes_feature(
            file_basename.encode('utf8')),
        'image/source_id': dataset_util.bytes_feature(
            camera_token.encode('utf8')),
        'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
        'image/encoded': dataset_util.bytes_feature(encoded_jpg),
        'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
        'image/object/bbox/xmin': dataset_util.float_list_feature(corner_list[:, 0] / float(image_width)),
        'image/object/bbox/xmax': dataset_util.float_list_feature(corner_list[:, 1] / float(image_width)),
        'image/object/bbox/ymin': dataset_util.float_list_feature(corner_list[:, 2] / float(image_height)),
        'image/object/bbox/ymax': dataset_util.float_list_feature(corner_list[:, 3] / float(image_height)),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text_list),
        'image/object/class/label': dataset_util.int64_list_feature(box_feature_list[2]),
        'image/object/class/anns_id': dataset_util.bytes_list_feature(anns_token_list)
    }

    example = tf.train.Example(features=tf.train.Features(feature=feature_dict))

    return example
def create_tf_example(row):
    full_path = os.path.join(os.getcwd(), 'images',
                             '{}'.format(row['filename']))
    with tf.gfile.GFile(full_path, 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    filename = row['filename'].encode('utf8')
    image_format = b'jpg'
    xmins = [row['xmin'] / width]
    xmaxs = [row['xmax'] / width]
    ymins = [row['ymin'] / height]
    ymaxs = [row['ymax'] / height]
    classes_text = [row['class'].encode('utf8')]
    classes = [class_text_to_int(row['class'])]
    # print(classes)
    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename),
            'image/source_id':
            dataset_util.bytes_feature(filename),
            'image/encoded':
            dataset_util.bytes_feature(encoded_jpg),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
Ejemplo n.º 23
0
def create_tf_example(encoded_image_data, filename, x_min, x_max, y_min, y_max,
                      classes_text, classes):
    """Creates a tf.Example proto from sample cat image.

    Args:
    encoded_cat_image_data: The jpg encoded data of the cat image.

    Returns:
    example: The created tf.Example.
    """
    image_format = b'jpg'

    xmins = [x_min / width]
    xmaxs = [x_max / width]
    ymins = [y_min / height]
    ymaxs = [y_max / height]

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(filename.encode()),
            'image/source_id':
            dataset_util.bytes_feature(filename.encode()),
            'image/encoded':
            dataset_util.bytes_feature(encoded_image_data.tobytes()),
            'image/format':
            dataset_util.bytes_feature(image_format),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmins),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmaxs),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymins),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymaxs),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
        }))
    return tf_example
def dict_to_tf_example(image_path1, image_path2, label_path):
    """Convert image and label to tf.Example proto.

  Args:
    image_path1: Path 1 to a single PASCAL image.
    image_path2: Path 2 to a single PASCAL image.
    label_path: Path to its corresponding label.

  Returns:
    example: The converted tf.Example.

  Raises:
    ValueError: if the image pointed to by image_path is not a valid JPEG or
                if the label pointed to by label_path is not a valid PNG or
                if the size of image does not match with that of label.
  """
    def read_image(image_path):
        with tf.gfile.GFile(image_path, 'rb') as fid:
            encoded_jpg = fid.read()
        encoded_jpg_io = io.BytesIO(encoded_jpg)
        image = PIL.Image.open(encoded_jpg_io)
        return encoded_jpg, image

    encoded_jpg1, image1 = read_image(image_path1)
    encoded_jpg2, image2 = read_image(image_path2)
    encoded_label, label = read_image(label_path)

    if (image1.size != label.size) or (image1.size != image2.size):
        raise ValueError(
            'The size of image does not match with that of label.')

    width, height = image1.size

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': dataset_util.int64_feature(height),
            'image/width': dataset_util.int64_feature(width),
            'image/encoded1': dataset_util.bytes_feature(encoded_jpg1),
            'image/encoded2': dataset_util.bytes_feature(encoded_jpg2),
            'image/format': dataset_util.bytes_feature('png'.encode('utf8')),
            'label/encoded': dataset_util.bytes_feature(encoded_label),
            'label/format': dataset_util.bytes_feature('png'.encode('utf8')),
        }))
    return example
Ejemplo n.º 25
0
def create_tf_example(df, img_id):
    image_path = 'data/images/'
    filename = '%06d.png'%img_id
    
    image_path = os.path.join(image_path, filename)
    with tf.gfile.GFile(image_path, 'rb') as fid:
        encoded_image_data = fid.read()
        
    filename = filename.encode()
    image = Image.open(image_path)
    width, height = image.size
    del image
    
    image_format = b'png'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []

    for bbox in np.array(df[['XMin', 'XMax', 'YMin', 'YMax']]):
        xmins += [bbox[0]/width]
        xmaxs += [bbox[1]/width]
        ymins += [bbox[2]/height]
        ymaxs += [bbox[3]/height]
    
    classes_text = [b'Car']*len(df)
    classes = [1]*len(df)

    tf_example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(filename),
      'image/source_id': dataset_util.bytes_feature(filename),
      'image/encoded': dataset_util.bytes_feature(encoded_image_data),
      'image/format': dataset_util.bytes_feature(image_format),
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example
Ejemplo n.º 26
0
def parse_test_example(f, images_path):
    height = None  # Image height
    width = None  # Image width
    filename = None  # Filename of the image. Empty if image is not from file
    encoded_image_data = None  # Encoded image bytes
    image_format = b'jpeg'  # b'jpeg' or b'png'

    filename = f.readline().rstrip()
    if not filename:
        raise FileNameIsNone()

    filepath = os.path.join(images_path, filename)

    image_raw = cv2.imread(filepath)
    if config.RESIZE:
        image_raw = cv2.resize(image_raw, (config.RESIZE, config.RESIZE))

    is_success, buffer = cv2.imencode(".jpg", image_raw)
    encoded_image_data = buffer.tobytes()
    # encoded_image_data = io.BytesIO(buffer)
    # encoded_image_data = open(filepath, "rb").read()
    # key = hashlib.sha256(encoded_image_data).hexdigest()
    key = ''

    height, width, channel = image_raw.shape

    tf_example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': dataset_util.int64_feature(int(height)),
            'image/width': dataset_util.int64_feature(int(width)),
            'image/filename': dataset_util.bytes_feature(
                filename.encode('utf-8')),
            'image/source_id': dataset_util.bytes_feature(
                filename.encode('utf-8')),
            'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
            'image/encoded': dataset_util.bytes_feature(encoded_image_data),
            # 'image/array': dataset_util.float_list_feature(
            #   (cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB) / 255.).flatten().tolist()),
            'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
        }))

    return tf_example
Ejemplo n.º 27
0
def create_tf_example(f,inputpath): # inputpath+filename->example
    # TODO(user): Populate the following variables from your example.
    height = 720 # Image height
    width = 1280 # Image width
    filename = f.split('.')[0].encode('utf8') # Filename of the image. Empty if image is not from file
    image_format = b'jpg' # b'jpeg' or b'png'

    # encoded_image_data = None # Encoded image bytes
    with tf.gfile.GFile(os.path.join(inputpath, f), 'rb') as fid:
        encoded_image_data = fid.read()
    
    image=Image.open(inputpath+f)
    width,height=image.size 




    xmins = [0] # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = [0] # List of normalized right x coordinates in bounding box
                # (1 per box)
    ymins = [0] # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = [0] # List of normalized bottom y coordinates in bounding box
                # (1 per box)
    classes_text = ['Human'.encode('utf8')] # List of string class name of bounding box (1 per box)
    classes = [1] # List of integer class id of bounding box (1 per box)

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(encoded_image_data),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example
Ejemplo n.º 28
0
def dict_to_tf_example(left_path, semantic_path, disparity_path):
    """Convert image and label to tf.Example proto.

  Args:
    image_path: Path to a single PASCAL image.
    label_path: Path to its corresponding label.

  Returns:
    example: The converted tf.Example.

  Raises:
    ValueError: if the image pointed to by image_path is not a valid JPEG or
                if the label pointed to by label_path is not a valid PNG or
                if the size of image does not match with that of label.
  """
    with tf.gfile.GFile(left_path, 'rb') as fid:
        left_png = fid.read()
    left_png_io = io.BytesIO(left_png)
    left = PIL.Image.open(left_png_io)

    with tf.gfile.GFile(semantic_path, 'rb') as fid:
        semantic_png = fid.read()
    semantic_png_io = io.BytesIO(semantic_png)
    semantic = PIL.Image.open(semantic_png_io)

    with tf.gfile.GFile(disparity_path, 'rb') as fid:
        disparity_png = fid.read()
    disparity_png_io = io.BytesIO(disparity_png)
    disparity = PIL.Image.open(disparity_png_io)

    if left.format != 'PNG' or semantic.format != 'PNG' or disparity.format != 'PNG':
        raise ValueError('The format not PNG')

    if left.size != disparity.size or left.size != semantic.size:
        raise ValueError(
            'The size of image does not match with that of label.')

    width, height = left.size

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'left/height': dataset_util.int64_feature(height),
            'left/width': dataset_util.int64_feature(width),
            'left/encoded': dataset_util.bytes_feature(left_png),
            'left/format': dataset_util.bytes_feature('png'.encode('utf8')),
            'semantic/encoded': dataset_util.bytes_feature(semantic_png),
            'semantic/format': dataset_util.bytes_feature('png'.encode(
                'utf8')),
            'disparity/encoded': dataset_util.bytes_feature(disparity_png),
            'disparity/format': dataset_util.bytes_feature('png'.encode(
                'utf8'))
        }))
    return example
def create_mock_tfrecord():
    pil_image = Image.fromarray(np.array([[[123, 0, 0]]], dtype=np.uint8),
                                'RGB')
    image_output_stream = StringIO.StringIO()
    pil_image.save(image_output_stream, format='png')
    encoded_image = image_output_stream.getvalue()

    feature_map = {
        'test_field':
        dataset_util.float_list_feature([1, 2, 3, 4]),
        standard_fields.TfExampleFields.image_encoded:
        dataset_util.bytes_feature(encoded_image),
    }

    tf_example = tf.train.Example(features=tf.train.Features(
        feature=feature_map))
    with tf.python_io.TFRecordWriter(get_mock_tfrecord_path()) as writer:
        writer.write(tf_example.SerializeToString())
Ejemplo n.º 30
0
def dict_to_tf_example(data,
                       label_map_dict,
                       image_subdirectory,
                       ignore_difficult_instances=False):
    """Convert XML derived dict to tf.Example proto.

  Notice that this function normalizes the bounding box coordinates provided
  by the raw data.

  Args:
    data: dict holding PASCAL XML fields for a single image (obtained by
      running dataset_util.recursive_parse_xml_to_dict)
    label_map_dict: A map from string label names to integers ids.
    image_subdirectory: String specifying subdirectory within the
      Pascal dataset directory holding the actual image data.
    ignore_difficult_instances: Whether to skip difficult instances in the
      dataset  (default: False).

  Returns:
    example: The converted tf.Example.

  Raises:
    ValueError: if the image pointed to by data['filename'] is not a valid JPEG
  """
    img_path = os.path.join(image_subdirectory, data['filename'])
    with tf.gfile.GFile(img_path, 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = PIL.Image.open(encoded_jpg_io)
    if image.format != 'JPEG':
        raise ValueError('Image format not JPEG')
    key = hashlib.sha256(encoded_jpg).hexdigest()

    width = int(data['size']['width'])
    height = int(data['size']['height'])

    xmin = []
    ymin = []
    xmax = []
    ymax = []
    classes = []
    classes_text = []
    truncated = []
    poses = []
    difficult_obj = []
    for obj in data['object']:
        difficult_obj.append(int(0))

        xmin.append(float(obj['bndbox']['xmin']) / width)
        ymin.append(float(obj['bndbox']['ymin']) / height)
        xmax.append(float(obj['bndbox']['xmax']) / width)
        ymax.append(float(obj['bndbox']['ymax']) / height)

        class_name = obj['name']
        classes_text.append(class_name.encode('utf8'))
        classes.append(label_map_dict[class_name])
        truncated.append(int(0))
        poses.append('Unspecified'.encode('utf8'))

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height':
            dataset_util.int64_feature(height),
            'image/width':
            dataset_util.int64_feature(width),
            'image/filename':
            dataset_util.bytes_feature(data['filename'].encode('utf8')),
            'image/source_id':
            dataset_util.bytes_feature(data['filename'].encode('utf8')),
            'image/key/sha256':
            dataset_util.bytes_feature(key.encode('utf8')),
            'image/encoded':
            dataset_util.bytes_feature(encoded_jpg),
            'image/format':
            dataset_util.bytes_feature('jpeg'.encode('utf8')),
            'image/object/bbox/xmin':
            dataset_util.float_list_feature(xmin),
            'image/object/bbox/xmax':
            dataset_util.float_list_feature(xmax),
            'image/object/bbox/ymin':
            dataset_util.float_list_feature(ymin),
            'image/object/bbox/ymax':
            dataset_util.float_list_feature(ymax),
            'image/object/class/text':
            dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label':
            dataset_util.int64_list_feature(classes),
            'image/object/difficult':
            dataset_util.int64_list_feature(difficult_obj),
            'image/object/truncated':
            dataset_util.int64_list_feature(truncated),
            'image/object/view':
            dataset_util.bytes_list_feature(poses),
        }))
    return example