批量处理图片 tensorflow

import numpy as np
import tensorflow as tf
import os
import random
source_file="E:/DATES/steel_photo/datas/Cr/"       #原始文件地址
target_file="E:/DATES/steel_photo/data_10/Cr/"  #修改后的文件地址
num=10                  #产生图片次数 
if not os.path.exists(target_file):  #如果不存在target_file,则创造一个
    os.makedirs(target_file)
 
file_list=os.listdir(source_file)   #读取原始文件的路径
#def preprocess_for_train(image, height, width, bbox):
#    if bbox is None:
#        bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
#    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)
#    distort_image = tf.slice(image, bbox_begin, bbox_size)
#    distort_image = tf.image.resize_images(distort_image, [height, width], method=np.random.randint(4))
#    distort_image = tf.image.random_flip_left_right(distort_image)
#    distort_image = distort_color(distort_image,color_ordering= np.random.randint(3))
#    return distort_image
with tf.Session() as sess:
    for i in range(num):
        max_random=len(file_list)-1
        a = random.randint(1, max_random)          #随机数字区间
        image_raw_data=tf.gfile.FastGFile(source_file+file_list[a],"rb").read()#读取图片
        print("正在处理:",file_list[a])
        image_data=tf.image.decode_jpeg(image_raw_data)
        filpped_le_re=tf.image.random_flip_left_right(image_data)   #随机左右翻转
        filpped_up_down=tf.image.random_flip_up_down(image_data)    #随机上下翻转
        adjust=tf.image.random_brightness(filpped_up_down,0.4)      #随机调整亮度
        
        boxes=tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])
        begin,size,bbox_for_draw=tf.image.sample_distorted_bounding_box(tf.shape(adjust),bounding_boxes=boxes,min_object_covered=0.5)
        batched=tf.expand_dims(tf.image.convert_image_dtype(adjust,tf.float32),0)
        image_with_box=tf.image.draw_bounding_boxes(batched,bbox_for_draw)
        distorted_image=tf.slice(adjust,begin,size)
        image_data=tf.image.convert_image_dtype(distorted_image,dtype=tf.uint8)
        encode_data=tf.image.encode_jpeg(image_data)
 
        with tf.gfile.GFile(target_file+str(i)+"_enhance"+".jpeg","wb") as f:
            f.write(encode_data.eval())
print("图像增强完毕")

你可能感兴趣的:(批量处理图片 tensorflow)