1. import numpy as np
  2. import tensorflow as tf
  3. import matplotlib.pyplot as plt
  4.  
  5. def distort_color(image, color_ordering=0):
  6. '''
  7. 随机调整图片的色彩,定义两种处理顺序。
  8. '''
  9. if color_ordering == 0:
  10. image = tf.image.random_brightness(image, max_delta=32./255.)
  11. image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  12. image = tf.image.random_hue(image, max_delta=0.2)
  13. image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
  14. else:
  15. image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  16. image = tf.image.random_brightness(image, max_delta=32./255.)
  17. image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
  18. image = tf.image.random_hue(image, max_delta=0.2)
  19.  
  20. return tf.clip_by_value(image, 0.0, 1.0)
  21.  
  22. def preprocess_for_train(image, height, width, bbox):
  23.  
  24. # 查看是否存在标注框。
  25. if image.dtype != tf.float32:
  26. image = tf.image.convert_image_dtype(image, dtype=tf.float32)
  27.  
  28. # 随机的截取图片中一个块。
  29. bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
  30. tf.shape(image), bounding_boxes=bbox)
  31. bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
  32. tf.shape(image), bounding_boxes=bbox)
  33. distorted_image = tf.slice(image, bbox_begin, bbox_size)
  34.  
  35. # 将随机截取的图片调整为神经网络输入层的大小。
  36. distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))
  37. distorted_image = tf.image.random_flip_left_right(distorted_image)
  38. distorted_image = distort_color(distorted_image, np.random.randint(2))
  39. return distorted_image
  40.  
  41. def pre_main(img,bbox=None):
  42. if bbox is None:
  43. bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
  44. with tf.gfile.FastGFile(img, "rb") as f:
  45. image_raw_data = f.read()
  46. with tf.Session() as sess:
  47. img_data = tf.image.decode_jpeg(image_raw_data)
  48. for i in range(9):
  49. result = preprocess_for_train(img_data, 299, 299, bbox)
  50.  
  51. plt.imshow(result.eval())
  52. plt.axis('off')
  53. plt.savefig("E:\\myresource\\代号{}".format(i))
  54.  
  55. pre_main("E:\\myresource\\moutance.jpg",bbox=None)
  56. 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. Execl导入系统

    文件导入功能 前台代码: Content\JS\jquery.ajaxfileupload.js<script src="~/Content/JS/jquery.ajaxfileupl ...

  2. Unity 坐标系转换

    在Unity中我们通常会用到以下几个坐标系下的点: 世界坐标系:World Space 简单来讲,我们通过 transform.position | transform.rotation 获取得到的位 ...

  3. C#设置一个控件可以鼠标拖动

    C#设置一个控件可以鼠标拖动: 新建一个C#项目, 创建一个label控件, 设置label的鼠标按下和抬起事件分别为:label1_MouseDown和label1_MouseUp. 对代码进行如下 ...

  4. 阿里云MySQL安装到centos,并链接。

    Last login: Wed Jan 22 11:21:17 on ttys001 wulaguixiaomianyangdeMacBook-Pro:~ xingwen$ ssh root@47.9 ...

  5. Python中的模块简单认识

    将自己定义的方法,变量存放在文件中,为一些脚本或者交互式的解释器实例使用,这个文件称为模块. 细说的话,模块可以分为四个通用类别: 1 使用python编写的.py文件(自定义模块) 2 已被编译为共 ...

  6. Java中boolean类型到底占用多少字节(转载)

    来源:https://www.cnblogs.com/brucecloud/p/5509921.html 虽然 Java 虚拟机定义了 boolean 这种数据类型,但是只对它提供了非常有限的支持.在 ...

  7. Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()

    今天遇到一个错误,没有定义一个openssl_cipher_iv_length()方法,可是我明明开启OpenSSL了啊 如果开启了还报错 只需要把php的目录加入环境变量 -重启电脑  就解决了 但 ...

  8. fileupload插件调用upload.parseRequest(request)解析得到空值问题

    得到的list长度是0,项目配置不能改变,没办法了,只能将HttpServletRequest强换成DefaultMultipartHttpServletRequest ,直接获取表单中的字段了.方法 ...

  9. codeforces E. The Contest(最长上升子序列)

    题目链接:https://codeforces.com/contest/1257/problem/E 题意:给三个序列k1,k2,k3,每个序列有一堆数,k1是前缀,k3是后缀,k2是中间,现可以从任 ...

  10. pycharm开发flask指定ip、端口无效

    原因分析 是因为使用了pycharm的版本的问题.并不是flask框架本身的问题(不管你是如何设置的flask配置,通过加载config也好,还是通过run的时候传入形参也好,均不影响) 可以很明显的 ...