____tz_zs
Image fragment interception, image resizing, image rollover and color adjustment for the entire image preprocessing process
Case source "TensorFlow actual Google Depth Learning framework"
Original
After processing the picture
#-*-Coding:utf-8-*-"" "@author: Tz_zs Image preprocessing Sample" "" Import TensorFlow as TF import numpy as NP import MATPLOTLIB.PYP
Lot as PLT # random processing order can further reduce the influence of extraneous factors def distort_color (image, color_ordering=0): if color_ordering = = 0: # random brightness
Image = Tf.image.random_brightness (image, max_delta=32./255.) # random saturation image = Tf.image.random_saturation (image, lower=0.5, upper=1.5) # random hue image = Tf.image.ra
Ndom_hue (Image, max_delta=0.2) # random contrast image = Tf.image.random_contrast (image, lower=0.5, upper=1.5) elif Color_ordering = = 1: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 no comment box is provided, focus on the entire diagram Like ifBbox is None:bbox = tf.constant ([0.0, 0.0, 1.0, 1.0], Dtype=tf.float32, shape=[1, 1, 4]) # [[0. 0.1. 1.]]] # Convert the type of image tensor if image.dtype!= tf.float32:image = Tf.image.convert_image_dtype (image, Dtype=tf.floa T32) # randomly intercepts image # print (Tf.shape (image). Eval ()) # [232 3] Bbox_begin, bbox_size, _ = Tf.image.sample_dis
Torted_bounding_box (Tf.shape (image), bounding_boxes=bbox) Distorted_image = Tf.slice (image, Bbox_begin, bbox_size) # Size Distorted_image = Tf.image.resize_images (distorted_image, [height, width], method=np.random.randint (4)) # Flip Distorted_image = Tf.image.random_flip_left_right (distorted_image) # random Color distorted_image = Distort_color ( Distorted_image, Np.random.randint (2)) return distorted_image Image_raw_data = Tf.gfile.FastGFile ("Picture.jpg", "R B "). Read () with TF. Session () as Sess:img_data = Tf.image.decode_jpeg (image_raw_data) boxes = Tf.constant ([[[0.05, 0.05, 0.9, 0.7], [ 0.35, 0.47, 0.5, 0.56]]) for I in range (6): result = Preprocess_for_train (img_data, 299, 299, boxes) Plt.imsho
W (Result.eval ()) Plt.show ()