python imgtotfrecord

import tensorflow as tf
import glob
import os
from PIL import Image
def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def img_to_tfrecord(data_path):
    rows = 20
    cols = 20
    depth = 3
    writer = tf.python_io.TFRecordWriter('test.tfrecords')
    labelfile = open("list.txt")
    lines = labelfile.readlines()
    for line in lines:
        # print line
        #img_name = line.split(" ")[0]  # name
       # label = line.split(" ")[1]  # label
        img_path =line.strip()
        print(img_path)

        img = Image.open(img_path) #type:Image.Image

        img = img.resize((rows, cols))
        # img_raw = img.tostring()

        img_raw = img.tobytes()
        example = tf.train.Example(features=tf.train.Features(feature={
            'height': _int64_feature(rows),
            'weight': _int64_feature(cols),
            'depth': _int64_feature(depth),
            'imagebytes': _bytes_feature(img_raw),
            # 'label': _bytes_feature(label)
           }))

        writer.write(example.SerializeToString())


    writer.close()

if __name__ == '__main__':

    current_dir = os.getcwd()
    print(current_dir)
    data_path = current_dir + '/smallFace/'
    # name = current_dir + '/data'
    print('Convert start')
    img_to_tfrecord(data_path)
    print('done!')
for serialized_example in tf.python_io.tf_record_iterator("test.tfrecords"):
        example = tf.train.Example()
        example.ParseFromString(serialized_example)

        image = example.features.feature['imagebytes'].bytes_list.value

        # 可以做一些预处理之类的
        print(image)

print("-------")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值