tensorflow图片基本处理

一、图片的读取--使用gfile(单张图片样例)

img_raw_data=tf.gfile.FastGFile("F:\\自己研究项目\\TensorFlow\\cat.jpg","rb").read()

二、matplotlib.pyplot添加title中文乱码问题

myfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
plt.title(u"标注小猫",fontproperties=myfont)
plt.show()

三、图片添加备注,重新改变图片大小

with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(img_raw_data)
    改变图像大小,使用resize_images
    img_size=tf.image.resize_images(img_data,(300,300),method=1)
    print(img_data)
    batched=tf.expand_dims(tf.image.convert_image_dtype(img_size,tf.float32),0)添加了一个维度,在 后面显示的时候需要把维度切换回来
    boxes=tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5 ,0.56]]])指定标注的相对位置
    result=tf.image.draw_bounding_boxes(batched,boxes)
    设置中文乱码问题
    myfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
    plt.figure(1)
    图片数据实数话后被添加了一个维度,所以需要重新塑形
    plt.imshow(result.eval().reshape([300,300,3]))
    plt.title(u"标注小猫",fontproperties=myfont)
    plt.show()

四、通过提供标注框的方式来告诉随机截取图像的范围

新版的api变化了,需要加入min_object_covered参数
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(img_data2),
 bounding_boxes=boxes2,min_object_covered=0.1)

五、生成自己的TFrecord文件

cwd = os.getcwd()
classes = os.listdir(cwd + "/17flowers/jpg")
classes = os.listdir("F:\\自己研究项目\\TensorFlow\\mnist数据集\\17flowers\\category")
writer = tf.python_io.TFRecordWriter("train.tfrecords")
for index, name in enumerate(classes):
    class_path =  "F:\\自己研究项目\\TensorFlow\\mnist数据集\\17flowers\\category\\" + name + "\\"
    if os.path.isdir(class_path):
        for img_name in os.listdir(class_path):
            img_path = class_path + img_name
            img = Image.open(img_path)
            img = img.resize((224, 224))
            img_raw = img.tobytes()  将图片转化为原生bytes
            example = tf.train.Example(features=tf.train.Features(feature={
                文件夹名作为分类标签
                "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[int(name)])),
                'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
            }))
            writer.write(example.SerializeToString())  序列化为字符串
            writer.flush()
            print(img_name)
writer.close
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值