利用深度学习框架(tensorflow,keras)进行数据增强(旋转、翻转、亮度等)

# -*- coding: utf-8 -*-
# @Time : 06/12/19 下午 05:22
# @Author : yuhao
# @File : add_data2.py
# @Software: PyCharm
# @Details:
import tensorflow as tf
import os
import numpy as np
from keras_preprocessing import image

source_file = "/video_detect/phone/old/demo/"  # 原始文件地址
target_file = "/video_detect/phone/add/demo/"  # 修改后的文件地址
num = 2  # 增强倍数

if not os.path.exists(target_file):  # 如果不存在target_file,则创造一个
    os.makedirs(target_file)

file_list = os.listdir(source_file)  # 读取原始文件的路径

_HEIGHT = 256
_WIDTH = 256
_CHANNELS = 3

# preprocessing parameters
random_extend_ratio = 1.2
random_hue_max_delta = 0.1
random_contrast_lower = 0.3
random_contrast_upper = 1.0
random_saturation_lower = 0.0
random_saturation_upper = 2.0
random_brightness_max_delta = 0.5

with tf.Session() as sess:
    for i in file_list:
        new_path = source_file + i
        for j in range(num):
            # 读取图片
            image_raw_data = tf.gfile.FastGFile(new_path, "rb").read()
            print("正在处理:", new_path)
            print(type(image_raw_data))
            image_data = tf.image.decode_image(image_raw_data, channels=3)
            print(type(image_data))
            # ##尺寸
            # image_data = tf.image.resize_images(images=image_data,
            #                                     size=[tf.cast(_HEIGHT * random_extend_ratio, tf.int32),
            #                                           tf.cast(_WIDTH * random_extend_ratio, tf.int32)])
            # ##裁剪
            # image_data = tf.random_crop(image_data, [_HEIGHT, _WIDTH, _CHANNELS])


            # 随机左右翻转
            image_data = tf.image.random_flip_left_right(image_data)
            # 随机上下翻转
            image_data = tf.image.random_flip_up_down(image_data)

            # hue 色调
            image_data = tf.image.random_hue(image_data, max_delta=random_hue_max_delta)
            # contrast 对比度
            image_data = tf.image.random_contrast(image_data, lower=random_contrast_lower, upper=random_contrast_upper)
            # saturation 饱和度
            image_data = tf.image.random_saturation(image_data, lower=random_saturation_lower, upper=random_saturation_upper)
            # bright 亮度
            image_data = tf.image.random_brightness(image_data, max_delta=random_brightness_max_delta)

            image_data = tf.image.convert_image_dtype(image_data, dtype=tf.uint8)

            encode_data = tf.image.encode_jpeg(image_data)

            with tf.gfile.GFile(target_file + str(j) + "_" + i, "wb") as f:
                f.write(encode_data.eval())
print("图像增强完毕")

for i in os.listdir(target_file):
    angle = np.random.uniform(low=-1, high=1)
    # if angle > 0:
    image_data = image.load_img(target_file + i)
    image_data = image.img_to_array(image_data)
    image_data = image.random_rotation(image_data, 30, row_axis=0, col_axis=1, channel_axis=2)
    print(type(image_data))
    image.save_img(target_file + i, image_data)


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值