import numpy as np
import tensorflow as tf
import time
import os
import cv2
from sklearn.utils import shuffle # 图片存放位置
PATH_DES = [
r'data_tfrecords/integers_tfrecords/',
r'data_tfrecords/alphabets_tfrecords/',
r'data_tfrecords/Chinese_letters_tfrecords/'
]
PATH_RES = [r'data/integers/',
r'data/alphabets/',
r'data/Chinese_letters/'] PATH = list(zip(PATH_RES, PATH_DES))
# transformation between integer <-> string
# 用于车牌识别时--数字+字母+32省份
integers = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9
}
alphabets = {
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35
}
provinces = {
'藏': 36,
'川': 37,
'鄂': 38,
'甘': 39,
'赣': 40,
'广': 41,
'桂': 42,
'贵': 43,
'黑': 44,
'沪': 45,
'吉': 46,
'冀': 47,
'津': 48,
'晋': 49,
'京': 50,
'辽': 51,
'鲁': 52,
'蒙': 53,
'闽': 54,
'宁': 55,
'青': 56,
'琼': 57,
'陕': 58,
'苏': 59,
'皖': 60,
'湘': 61,
'新': 62,
'渝': 63,
'豫': 64,
'粤': 65,
'云': 66,
'浙': 67
}
label_ref = [
integers,
alphabets,
provinces
] # 图片信息
IMG_HEIGHT = 28
IMG_WIDTH = 16
IMG_CHANNELS = 1
# NUM_TRAIN = 7000
NUM_VALIDARION = [sum([len(os.listdir(r + i))
for i in os.listdir(r)]) // 5 for r in PATH_RES] # 读取图片
def read_images(path_res, label_ref, num_validation):
imgs = []
labels = []
path_res_dirs = sorted(os.listdir(path_res))
for i in path_res_dirs:
paths_images = os.listdir(path_res + i) # 本想排序的, 但是字符串排序效果不尽人意.
t_lst = [''.join((path_res, i, '/', t)) for t in paths_images]
paths_images = t_lst.copy()
del t_lst
for j in range(len(paths_images)):
c = 0
img = cv2.imread(paths_images[j], 0)
img_blur = cv2.bilateralFilter(img, 3, 45, 45)
img_current = cv2.resize(img_blur, (28, 28))
ret, img_current_threshed = cv2.threshold(img_current,
127, 255,
cv2.THRESH_OTSU)
h, w = img_current_threshed.shape
t_c = np.array([[img_current_threshed[0][0],
img_current_threshed[0, w-1]],
[img_current_threshed[h-1, 0],
img_current_threshed[h-1, w-1]]])
c = sum([(t_c[0, 0]//255), (t_c[1, 1]//255),
(t_c[0, 1]//255), (t_c[1, 0]//255)])
if_reverse = sum([sum(img_current_threshed[0, :] // 255),
sum(img_current_threshed[:, w-1] // 255),
sum(img_current_threshed[h-1, :] // 255),
sum(img_current_threshed[:, 0] // 255)])\
/ ((h + w) * 2 + 4) > 0.5
# if c >= 1:
# img_current_threshed = 255 - img_current_threshed
if c > 2 or (c > 1 and if_reverse):
img_current_threshed = 255 - img_current_threshed
# img_current_threshed = img_current
label_current = paths_images[j].split("/")[-2]
# if i == '2':
# fig, ax = plt.subplots(1, 2, figsize=(16, 8))
# ax0, ax1 = ax.ravel()
# ax0.imshow(img_current, cmap="gray")
# ax1.imshow(img_current_threshed, cmap="gray")
# plt.title(c)
# # print([img_current_threshed[0][0],
# # img_current_threshed[0, w-1],
# # img_current_threshed[h-1, 0],
# # img_current_threshed[h-1, w-1]])
# plt.show()
imgs.append((img_current_threshed // 255).astype(np.uint8))
labels.append(np.uint8(label_ref[label_current]))
imgs = np.array(imgs)
imgs = imgs.reshape(imgs.shape[0], -1)
labels = np.array(labels)
labels = labels.reshape(labels.shape[0], -1)
data = np.hstack((labels, imgs))
data = shuffle(data)
test_labels = data[:num_validation, 0]
test_images = data[:num_validation, 1:]
train_labels = data[num_validation:, 0]
train_images = data[num_validation:, 1:]
return train_labels, train_images, test_labels, test_images # 生成整数型的属性
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 convert(images, labels, filename):
# 获取要转换为TFRecord文件的图片数目
num = images.shape[0]
print("num:", num)
print("images.shape:", images.shape)
# 输出TFRecord文件的文件名
print('Writting', filename)
# 创建一个writer来写TFRecord文件
writer = tf.python_io.TFRecordWriter(filename)
for i in range(num):
# 将图像矩阵转化为一个字符串
img_raw = images[i].tostring()
# 将一个样例转化为Example Protocol Buffer,并将所有需要的信息写入数据结构
example = tf.train.Example(features=tf.train.Features(feature={
'label': _int64_feature(int(labels[i])),
'image_raw': _bytes_feature(img_raw)}))
# 将example写入TFRecord文件
writer.write(example.SerializeToString())
writer.close()
print('Writting End') def main():
start_time = time.time()
for i in range(len(PATH)):
print('reading images from {} begin'.format(PATH_RES[i]))
data = read_images(PATH_RES[i], label_ref[i], NUM_VALIDARION[i])
train_labels, train_images, test_labels, test_images = data
# Slice data here.
print('convert to tfrecords into {} begin'.format(PATH_DES[i]))
convert(train_images, train_labels, PATH_DES[i]+"train.tfrecords")
convert(test_images, test_labels, PATH_DES[i]+"test.tfrecords")
duration = time.time() - start_time
print('Converting end , total cost = %d sec' % duration) if __name__ == '__main__':
main()

图片转tfrecords的更多相关文章

  1. Tensorflow线程和队列

    读取数据 小数量数据读取 这仅用于可以完全加载到存储器中的小的数据集有两种方法: 存储在常数中. 存储在变量中,初始化后,永远不要改变它的值. 使用常数更简单一些,但是会使用更多的内存,因为常数会内联 ...

  2. TensorFlowIO操作(二)----读取数据

    读取数据 小数量数据读取 这仅用于可以完全加载到存储器中的小的数据集有两种方法: 存储在常数中. 存储在变量中,初始化后,永远不要改变它的值. 使用常数更简单一些,但是会使用更多的内存,因为常数会内联 ...

  3. TensorFlow高效读取数据的方法——TFRecord的学习

    关于TensorFlow读取数据,官网给出了三种方法: 供给数据(Feeding):在TensorFlow程序运行的每一步,让python代码来供给数据. 从文件读取数据:在TensorFlow图的起 ...

  4. TensorFlow笔记-文件读取

    小数量数据读取 这些只用于可以完全加载到内存中的小型数据集: 1,储存在常数中 2,储存在变量中,初始化后,永远不改变它的值 使用常量 training_data = ... training_lab ...

  5. [TFRecord格式数据]利用TFRecords存储与读取带标签的图片

    利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...

  6. TFrecords读、写图片文件

    参考:https://blog.csdn.net/u014802590/article/details/68495238 参考:https://www.2cto.com/kf/201709/68057 ...

  7. 记录:将图片数据生成 tfrecords 文件并在训练使用时读取

    直接用别人的就行了: https://github.com/myCVs/GenTFRecords

  8. 【学习笔记】tensorflow图片读取

    目录 图像基本概念 图像基本操作 图像基本操作API 图像读取API 狗图片读取 CIFAR-10二进制数据读取 TFRecords TFRecords存储 TFRecords读取方法 图像基本概念 ...

  9. 图像转化成TFrecords格式并回转

    import os import tensorflow as tf from PIL import Image import numpy as np cat_image_path='D:/软件/pyc ...

随机推荐

  1. uni-app开发经验分享六:页面跳转及提示框

    在我们开发的uni-app的过程中,页面跳转及提示框往往是我们做数据交互及结果反馈所要使用的功能,这里分享下我收集的一些方法及看法. 一:页面跳转 事件跳转 :指通过tap等事件来实现页面的跳转,跳转 ...

  2. JavaScript中的异步函数

    JavaScript中的异步函数 ES8 的 async/await 旨在解决利用异步结构组织代码的问题.为此, ECMAScript 对函数进行了扩展,为其增加了两个新关键字: async 和 aw ...

  3. Hive语法小释

    阅读本文你可以获取: 1.数据库的查询 2.hive表的基本操作(建表三种常用方式.删除表.修改表.加载数据.内外表转换.添加分区.复制数据) 3.SQL到HiveQL的的一些不同点 1.   基本操 ...

  4. redis6.0多线程

    https://www.sohu.com/a/331991216_268033 执行还是单线程     读写解析多线程   6.0 https://segmentfault.com/a/1190000 ...

  5. 6到8个月如何达到三年加得前端经验,对标P7,“慕课网 Java工程师2020”

    百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f   阶段一:课程设计及前端创建脚手架开发 第1周   需求分析和架构设计 ...

  6. 最新Ceph L版与openstack Pike对接

    安装Ceph luminous   实验环境 三台服务器,每台服务器都有4块硬盘,每台服务器都将自己的第一块硬盘作为系统盘,剩下的做ceph   一.在所有服务器上操作 #使用阿里源 yum inst ...

  7. 通过模拟数据,使用js在前端实现模糊查询下拉框功能实例教程

    所谓模糊查询就是通过关键字在数据中匹配到包含关键字的数据,而得出的查询结果.本实例教程讲解在前端文本框输入关键字,显示匹配的数据列表功能. 首先得准备一个文本框和显示数据列表的div元素,html代码 ...

  8. Jenkins(3)拉取git仓库代码,执行python自动化脚本

    前言 python自动化的脚本开发完成后需提交到git代码仓库,接下来就是用Jenkins拉取代码去构建自动化代码了 新建项目 打开Jenkins新建一个自由风格的项目 源码管理 Repository ...

  9. [ICPC 2018 宁夏邀请赛] A-Maximum Element In A Stack(思维)

    >传送门< 前言 辣鸡网络赛,虽然我是个菜鸡,然而好几个队伍十几分钟就AK???我心态那会彻底崩了,后来群里炸了,话题直接上知乎热搜,都是2018ICPC宁夏网络赛原题,这怎么玩,拼手速? ...

  10. LightOJ - 1151 Snakes and Ladders(概率dp+高斯消元)

    有100个格子,从1开始走,每次抛骰子走1~6,若抛出的点数导致走出了100以外,则重新抛一次.有n个格子会单向传送到其他格子,G[i]表示从i传送到G[i].1和100不会有传送,一个格子也不会有两 ...