Skip to content

Commit

Permalink
Add image sha256 to tfrecord (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiltsov-max authored Jul 23, 2020
1 parent f9fab24 commit a3448a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import codecs
from collections import OrderedDict
import hashlib
import logging as log
import os
import os.path as osp
Expand Down Expand Up @@ -180,15 +181,18 @@ def _make_tf_example(self, item):

features.update({
'image/encoded': bytes_feature(b''),
'image/format': bytes_feature(b'')
'image/format': bytes_feature(b''),
'image/key/sha256': bytes_feature(b''),
})
if self._save_images:
if item.has_image and item.image.has_data:
buffer, fmt = self._save_image(item, filename)
key = hashlib.sha256(buffer).hexdigest()

features.update({
'image/encoded': bytes_feature(buffer),
'image/format': bytes_feature(fmt.encode('utf-8')),
'image/key/sha256': bytes_feature(key.encode('utf8')),
})
else:
log.warning("Item '%s' has no image" % item.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def _parse_tfrecord_file(cls, filepath, subset, images_dir):
'image/width': tf.io.FixedLenFeature([], tf.int64),
'image/encoded': tf.io.FixedLenFeature([], tf.string),
'image/format': tf.io.FixedLenFeature([], tf.string),

# use varlen to avoid errors when this field is missing
'image/key/sha256': tf.io.VarLenFeature(tf.string),

# Object boxes and classes.
'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
Expand Down

0 comments on commit a3448a2

Please sign in to comment.