import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt def distort_color(image, color_ordering=0):
'''
随机调整图片的色彩,定义两种处理顺序。
'''
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32./255.)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
else:
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_brightness(image, max_delta=32./255.)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2) return tf.clip_by_value(image, 0.0, 1.0) def preprocess_for_train(image, height, width, bbox): # 查看是否存在标注框。
if image.dtype != tf.float32:
image = tf.image.convert_image_dtype(image, dtype=tf.float32) # 随机的截取图片中一个块。
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
tf.shape(image), bounding_boxes=bbox)
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
tf.shape(image), bounding_boxes=bbox)
distorted_image = tf.slice(image, bbox_begin, bbox_size) # 将随机截取的图片调整为神经网络输入层的大小。
distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))
distorted_image = tf.image.random_flip_left_right(distorted_image)
distorted_image = distort_color(distorted_image, np.random.randint(2))
return distorted_image def pre_main(img,bbox=None):
if bbox is None:
bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
with tf.gfile.FastGFile(img, "rb") as f:
image_raw_data = f.read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
for i in range(9):
result = preprocess_for_train(img_data, 299, 299, bbox) plt.imshow(result.eval())
plt.axis('off')
plt.savefig("E:\\myresource\\代号{}".format(i)) pre_main("E:\\myresource\\moutance.jpg",bbox=None)
exit()

吴裕雄 python 神经网络——TensorFlow图片预处理调整图片的更多相关文章

  1. 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...

  2. 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(2)

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  3. 吴裕雄 python 神经网络——TensorFlow 图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile ...

  4. 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(1)

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  5. 吴裕雄 python 神经网络——TensorFlow训练神经网络:花瓣识别

    import os import glob import os.path import numpy as np import tensorflow as tf from tensorflow.pyth ...

  6. 吴裕雄 python 神经网络——TensorFlow图片预处理

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 使用'r'会出错,无法解码,只能以2进制形式读 ...

  7. 吴裕雄 python 神经网络——TensorFlow 卷积神经网络水果图片识别

    #-*- coding:utf- -*- import time import keras import skimage import numpy as np import tensorflow as ...

  8. 吴裕雄 python 神经网络——TensorFlow 卷积神经网络手写数字图片识别

    import os import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_N ...

  9. 吴裕雄 python 神经网络——TensorFlow 数据集高层操作

    import tempfile import tensorflow as tf train_files = tf.train.match_filenames_once("E:\\output ...

随机推荐

  1. 计蒜客 - A1633.蒜头君的数轴

    我感觉出的很好的一道题,首先不难想到(其实我刚开始没想到),加点的个数就是找已有点两两形成区间的gcd,那么问题就出在了复杂度上,每次循环哪个区间不要复杂度过高,所以运用正反两次前缀和(?好像不能这么 ...

  2. linq和转换运算符

    1.ToArray 两种常用用法 使用ILSPY查看Enumerable中的ToArray 源码分析:我们发现如果该类型可以转化为ICollection,我们最后执行CopyTo方法,如果不能转换为I ...

  3. Echarts--来自官网

    引入 ECharts ECharts 3 开始不再强制使用 AMD 的方式按需引入,代码里也不再内置 AMD 加载器.因此引入方式简单了很多,只需要像普通的 JavaScript 库一样用 scrip ...

  4. Mybatis-生成逆向工程后对数据库的模糊查询详解

    MyBatis-使用逆向工程中方法进行模糊查询 1.应用mybatis逆向工程会大大的提高我们的开发效率,如何应用mabatis 逆向生成的代码进行模糊查询那. 2.首先看一下pojo 层中examp ...

  5. python setup.py 安装和卸载 的正确姿势

    1.install python setup.py install --record files.txt 2. uninstall 删除这些文件 cat files.txt | xargs rm -r ...

  6. LitElement(二)模板编写基本语法

    原文:https://lit-element.polymer-project.org/guide/templates 1.定义一个渲染模板 1.1 基本规则 要用LitElement 组件定义一个模板 ...

  7. 集成unittest做接口测试

    unittest接口测试 上篇已经讲了接口测试的做法,利用的是postman工具,工具始终是工具,它有一定的局限性,比如测试数据的存放,断言的方法以及上下接口关联使用灵活性.python对http接口 ...

  8. python后续学习

    关于使用python输出中文字符的问题: Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错. 解决方法为只要在文件开头加入 # -*- co ...

  9. 图的最小生成树prim算法模板

    用prim算法构建最小生成树适合顶点数据较少而边较多的图(稠密图) prim算法生成连通图的最小生成树模板伪代码: G为图,一般为全局变量,数组d为顶点与集合s的最短距离 Prim(G, d[]){ ...

  10. Django_MTV和虚拟环境

    1. MVT模型 2. 虚拟环境 """ 1.安装虚拟环境的命令: 1)sudo pip install virtualenv #安装虚拟环境 2)sudo pip in ...