@BruceWang
2018-01-08T21:59:49.000000Z
字数 4726
阅读 1594
数据增强
调用超级API: Auto Generated Documentation
# _*_ coding:utf-8 _*_
"""
> Deep learning image augmentation
cited from https://scottontechnology.com/flip-image-opencv-python/
http://augmentor.readthedocs.io/en/master/userguide/mainfeatures.html
"""
import cv2
import glob
import random
import os
from multiprocessing import Pool as ProcessPool
from multiprocessing.dummy import Pool as ThreadPool
import Augmentor
import numpy as np
# import path_var
def img_flip():
# path = "F:/ad_samples/download_sample/14/8DB54D749B1D4A2D5FD3441C681D9A2C522453AC_s.jpg"
path = r'C:\Users\aixin\Desktop\all_my_learning\match\niu_qu\original_path\A01_8.jpg'
img = cv2.imread(path)
horizontal_img = img.copy()
vertical_img = img.copy()
both_img = img.copy()
horizontal_img = cv2.flip(img, 0)
vertical_img = cv2.flip(img, 1)
both_img = cv2.flip(img, -1)
cv2.imshow("original img", img)
cv2.imshow("horizontal img", horizontal_img)
cv2.imshow("vertical img", vertical_img)
cv2.imshow("both flip", both_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def flip_img_save2dir(file):
img = cv2.imread(file)
dst_dir = path_var.g_dst_dir
h_img = img.copy()
v_img = img.copy()
b_img = img.copy()
h_img = cv2.flip(img, 0)
v_img = cv2.flip(img, 1)
b_img = cv2.flip(img, -1)
# file like F:/ad_samples/train_samples/ad_text_artifact/base_type/type_10.jpg
# get file name "type_10"
# type_10.jpg
base_name = os.path.basename(file)
# type_10
base_name = os.path.splitext(base_name)[0]
file_name = dst_dir + base_name + "_h" + ".jpg"
cv2.imwrite(file_name, h_img)
file_name = dst_dir + base_name + "_v" + ".jpg"
cv2.imwrite(file_name, v_img)
file_name = dst_dir + base_name + '_b' + ".jpg"
cv2.imwrite(file_name, b_img)
def do_all_flip(base_dir="F:/ad_samples/train_samples/ad_web_2/"):
"""
flip all the images in dir, and then save them
to another dir
:return:
"""
# get all files
files = glob.glob(base_dir + "/*.png")
# like ['E:/img\\1.jpg', 'E:/img\\10.jpg']
# start 3 process
# pool = ProcessPool(3)
pool = ThreadPool(3)
rets = pool.map(flip_img_save2dir, files)
pool.close()
pool.join()
print ('all images accomplish flip and save to dir')
def flip_all_in_dir():
base_dir = 'F:/ad_samples/train_samples/others/'
sub_dir_lst = glob.glob(base_dir + "*")
# ['F:/dir1', 'F:/dir2']
# print sub_dir_lst
new_sub_dir = [os.path.join(base_dir, item + '_flip/') for item in os.listdir(base_dir)]
# ['F:/dir1_flip', 'F:/dir2_flip']
for dir_item, new_item in zip(sub_dir_lst[10:], new_sub_dir[10:]):
global g_dst_dir
if not os.path.exists(new_item):
os.makedirs(new_item)
# g_dst_dir = new_item
# Path.g_dst_dir = new_item
path_var.g_dst_dir = new_item
print ('flip %s, flip dir %s' % (dir_item, new_item))
do_all_flip(base_dir=dir_item)
def augmentation():
path = r'C:\Users\aixin\Desktop\all_my_learning\match\niu_qu\original_path'
output_path = r'C:\Users\aixin\Desktop\all_my_learning\match\niu_qu\niuqu_path'
p = Augmentor.Pipeline(path, output_directory=output_path)
# p.flip_left_right(probability=0.4)
# p.flip_top_bottom(probability=0.6)
# p.flip_random(probability=0.5)
# p.crop_centre(probability=0.2, percentage_area=0.8)
# p.crop_random(probability=0.6, percentage_area=0.7)
# p.rotate(probability=0.2, max_left_rotation=10, max_right_rotation=16)
# p.rotate_random_90(probability=0.5)
# p.rotate180(probability=0.4)
# p.rotate270(probability=0.3)
p.zoom(probability=0.3, min_factor=1.1, max_factor=1.5)
p.random_distortion(probability=0.5, grid_height=4, grid_width=4, magnitude=4)
p.shear(probability=0.2, max_shear_left=15, max_shear_right=15)
p.shear(probability=0.5, max_shear_left=15, max_shear_right=15)
p.skew(probability=0.1, magnitude=0.6)
p.skew_tilt(probability=0.2, magnitude=0.6)
p.skew_corner(probability=0.2, magnitude=0.6)
p.skew_top_bottom(probability=0.3, magnitude=0.6)
p.skew_left_right(probability=0.2, magnitude=0.6)
# SIZE = 4 * 5
p.sample(10)
if __name__ == '__main__':
# img_flip()
# flip_all_in_dir()
# do_all_flip()
augmentation()
# test single image flip and save
# file = 'F:/ad_samples/train_samples/ad_text_artifact/base_type/type_10.jpg'
# flip_img_save2dir(file=file)
pass
import numpy as np
import cv2
# img_path = r'C:\Users\aixin\Desktop\Phebe.jpg'
img_path = r'C:\Users\aixin\Desktop\Bruce.png'
img = cv2.imread(img_path)
rows, cols, channels = img.shape
print(img.shape)
src = np.float32([[50,50],[200,50],[50,200]])
dst = np.float32([[10,100],[200,50],[100,250]])
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M1 = cv2.getRotationMatrix2D((cols/2,rows/2),45,1)
M2 = cv2.getAffineTransform(src, dst)
M3 = cv2.getPerspectiveTransform(pts1, pts2)
dst1 = cv2.warpAffine(img,M1,(cols,rows))
dst2 = cv2.warpAffine(img,M2,(cols,rows))
dst3 = cv2.warpPerspective(img,M3,(cols,rows))
cv2.imshow('img', img)
cv2.imshow('dst1', dst1)
cv2.imshow('dst2', dst2)
cv2.imshow('dst3', dst3)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果你有什么疑问,欢迎联系我哈,我会给大家慢慢解答啦~~~
怎么联系我? 笨啊~ ~~ 你留言也行。
你关注微信公众号1.听朕给你说:2.tzgns666,3.或者扫那个二维码,后台联系我也行啦!
(爱心.gif) 么么哒~么么哒~么么哒
爱心从我做起,贫困山区捐衣服,为开源社区做贡献!码字不易啊,如果你觉得本文有帮助,三毛也是爱!真的就三毛,呜呜。。。