栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

TensorFlow-从TFRecords文件读取视频帧

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TensorFlow-从TFRecords文件读取视频帧

将每个帧编码为单独的功能会导致难以动态选择帧,因为

tf.parse_example()
(和
tf.parse_single_example()
)的签名要求在图构造时固定一组已解析的功能名称。但是,您可以尝试将帧编码为
单个 功能,其中包含JPEG编码的字符串列表:

def _bytes_list_feature(values):    """Wrapper for inserting bytes features into Example proto."""    return tf.train.Feature(bytes_list=tf.train.BytesList(value=values))with tf.python_io.TFRecordWriter(output_file) as writer:  # Read and resize all video frames, np.uint8 of size [N,H,W,3]  frames = ...  features = {}  features['num_frames']  = _int64_feature(frames.shape[0])  features['height']      = _int64_feature(frames.shape[1])  features['width']       = _int64_feature(frames.shape[2])  features['channels']    = _int64_feature(frames.shape[3])  features['class_label'] = _int64_feature(example['class_id'])  features['class_text']  = _bytes_feature(tf.compat.as_bytes(example['class_label']))  features['filename']    = _bytes_feature(tf.compat.as_bytes(example['video_id']))  # Compress the frames using JPG and store in as a list of strings in 'frames'  enpred_frames = [tf.compat.as_bytes(cv2.imenpre(".jpg", frame)[1].tobytes())         for frame in frames]  features['frames'] = _bytes_list_feature(enpred_frames)  tfrecord_example = tf.train.Example(features=tf.train.Features(feature=features))  writer.write(tfrecord_example.SerializeToString())

完成此操作后,就可以

frames
使用修改后的解析代码版本来动态切片功能:

def depre(serialized_example, sess):  # Prepare feature list; read enpred JPG images as bytes  features = dict()  features["class_label"] = tf.FixedLenFeature((), tf.int64)  features["frames"] = tf.VarLenFeature(tf.string)  features["num_frames"] = tf.FixedLenFeature((), tf.int64)  # Parse into tensors  parsed_features = tf.parse_single_example(serialized_example, features)  # Randomly sample offset from the valid range.  random_offset = tf.random_uniform(      shape=(), minval=0,      maxval=parsed_features["num_frames"] - SEQ_NUM_frameS, dtype=tf.int64)  offsets = tf.range(random_offset, random_offset + SEQ_NUM_frameS)  # Depre the enpred JPG images  images = tf.map_fn(lambda i: tf.image.depre_jpeg(parsed_features["frames"].values[i]),          offsets)  label  = tf.cast(parsed_features["class_label"], tf.int64)  return images, label

(请注意,我无法运行您的代码,因此可能会有一些小错误,但希望它足以使您入门。)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/653356.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号