簡體   English   中英

Tensorflow.image.decode_jpeg 在解碼圖像 TFRecord 數據時移動值

[英]Tensorflow.image.decode_jpeg shifts values while decoding image TFRecord data

我使用SequenceExample將每個示例的可變數量的 jpg 幀存儲到 TFRecord 中:

tf.compat.as_bytes(cv2.imencode(".jpg", frame)[1].tobytes()))

然后我使用以下方法解析這些幀:

images = tf.map_fn(lambda x: tf.image.decode_jpeg(x, channels=3), sequence_features['frames'], dtype=tf.uint8)

但是圖像值以某種方式發生了變化:

在此處輸入圖片說明

當我只是解析原始字節字符串然后稍后用opencv解碼時,圖片看起來正常:

for img in images:
  img = np.frombuffer(img, dtype=np.uint8)
  img = cv2.imdecode(img, 1)

在此處輸入圖片說明

更完整的例子:

def write(videos, tfr_path):
  with tf.python_io.TFRecordWriter(tfr_path) as writer:
    for video in videos:
      label = get_label()
      frames = []
        for frame in video:
          frames.append(tf.compat.as_bytes(cv2.imencode(".jpg", prec_img)[1].tobytes()))

      feature_list = {
                  'label': (_float_list_feature_list(label),),
                  'frames': _bytes_feature_list(encoded_frames)
              }
      feature_lists = tf.train.FeatureLists(feature_list=feature_list)

      example = tf.train.SequenceExample(feature_lists=feature_lists, context=None)
      writer.write(example.SerializeToString())

def _parse_tfr_data(example, size):
  sequence_features = {
      'label': tf.FixedLenSequenceFeature([size], dtype=tf.float32),
      'frames': tf.FixedLenSequenceFeature([], dtype=tf.string)
  }

  features, sequence_features = tf.parse_single_sequence_example(example, context_features=None,
                                                             sequence_features=sequence_features)

  images = tf.map_fn(lambda x: tf.image.decode_jpeg(x, channels=3), sequence_features['frames'], dtype=tf.uint8)
  label = sequence_features['label']

  return images, label

感謝Dan Mašek tf.image.decode_jpeg 使用 RGB cv2.imencode BGR,所以事先交換它是可行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM