**python代码实现目标检测数据增强**

python代码实现目标检测数据增强
tif格式影像数据最好先转成jpg格式,批量转换代码在我的博客中有,尝试过直接用tif格式数据增强,结果翻车厉害!!
目标检测数据增强
疫情期间在家也要科研,碰上了数据增强,找了很多代码,但是还是没跑通,最后选择了这种处理方式来完成数据增强处理。同时特别感谢csdn上给我提供帮助的大佬们,虽然未曾谋面,但是每一步的学习真的感恩~~
##项目里需要做一个数据增强预处理试验,由于我用的YOLOv3的pytorch框架,博客找了一圈没有找到内置的代码,最后找到了一篇比较好的数据增强的办法,现在记录下来方便以后查看,其实之前总是碰到一些技术性的问题总没有时间去写博客,我觉得这样非常不好,以后碰到类似的又要去调bug,还是养成随手记录博客的习惯,也可以帮助更多需要的童鞋。我参考的数据增强代码是这个链接,

此代码用于实现图像数据增强,对图片进行批量处理。包括图片旋转、翻转、模糊、增加噪声、亮度几种处理。运行需要安装python、opencv、numpy等。
使用时将图片统一放在img文件夹中,并将img文件夹和下载的py文件放在一起。

童鞋们可以先下载下来,但是可能会报错,我是将代码放在pycharm中运行。代码中有镜像,旋转,变亮变暗,噪声处理,具体原理你们可以自行百度一下相关博客。
主要修改一下图片的路径
file_dir = “img/”
for img_name in os.listdir(file_dir):
img_path = file_dir + img_name
print(img_path)
img = cv2.imread(img_path)
cv2.imshow("/data/1/img", img)
这里需要将图片文件夹img放在文件夹1下面,具体路径的方法你们按照你们的系统来决定,我是ubuntn系统,所以我每次用路径就是ctrl+L查看的,我第一次运行就是这里出了问题,你们运行要小心点。
下面我来贴下详细的代码:
import cv2
import numpy as np
import os.path
import copy

def SaltAndPepper(src,percetage):
SP_NoiseImg=src.copy()
SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
for i in range(SP_NoiseNum):
randR=np.random.randint(0,src.shape[0]-1)
randG=np.random.randint(0,src.shape[1]-1)
randB=np.random.randint(0,3)
if np.random.randint(0,1)==0:
SP_NoiseImg[randR,randG,randB]=0
else:
SP_NoiseImg[randR,randG,randB]=255
return SP_NoiseImg

def addGaussianNoise(image,percetage):
G_Noiseimg = image.copy()
w = image.shape[1]
h = image.shape[0]
G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
for i in range(G_NoiseNum):
temp_x = np.random.randint(0,h)
temp_y = np.random.randint(0,w)
G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
return G_Noiseimg

def darker(image,percetage=0.9):
image_copy = image.copy()
w = image.shape[1]
h = image.shape[0]
#get darker
for xi in range(0,w):
for xj in range(0,h):
image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
return image_copy

def brighter(image, percetage=1.5):
image_copy = image.copy()
w = image.shape[1]
h = image.shape[0]
#get brighter
for xi in range(0,w):
for xj in range(0,h):
image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
return image_copy

def rotate(image, angle, center=None, scale=1.0):
(h, w) = image.shape[:2]
# If no rotation center is specified, the center of the image is set as the rotation center
if center is None:
center = (w / 2, h / 2)
m = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, m, (w, h))
return rotated

def flip(image):
flipped_image = np.fliplr(image)
return flipped_image

file_dir = “img/”
for img_name in os.listdir(file_dir):
img_path = file_dir + img_name
print(img_path)
img = cv2.imread(img_path)
cv2.imshow("/data/1/img", img)
cv2.waitKey(5000)
# 旋转
rotated_90 = rotate(img, 90)
cv2.imwrite(file_dir + img_name[0:-4] + ‘_r90.jpg’, rotated_90)
rotated_180 = rotate(img, 180)
cv2.imwrite(file_dir + img_name[0:-4] + ‘_r180.jpg’, rotated_180)

for img_name in os.listdir(file_dir):
img_path = file_dir + img_name
img = cv2.imread(img_path)
# 镜像
flipped_img = flip(img)
cv2.imwrite(file_dir +img_name[0:-4] + ‘_fli.jpg’, flipped_img)

# 增加噪声
# img_salt = SaltAndPepper(img, 0.3)
# cv2.imwrite(file_dir + img_name[0:7] + '_salt.jpg', img_salt)
img_gauss = addGaussianNoise(img, 0.3)
cv2.imwrite(file_dir + img_name[0:-4] + '_noise.jpg',img_gauss)

#变亮、变暗
img_darker = darker(img)
cv2.imwrite(file_dir + img_name[0:-4] + '_darker.jpg', img_darker)
img_brighter = brighter(img)
cv2.imwrite(file_dir + img_name[0:-4] + '_brighter.jpg', img_brighter)

blur = cv2.GaussianBlur(img, (7, 7), 1.5)
#      cv2.GaussianBlur(图像,卷积核,标准差)
cv2.imwrite(file_dir + img_name[0:-4] + '_blur.jpg',blur)

在pycharm中运行完了以上代码以后会出现很多图片,同时opencv会将图片显示出来,如下图:
**python代码实现目标检测数据增强**_第1张图片
怎么样,是不是很简单,我这里只是实现了python数据增强的预处理步骤,如果要做目标增强还需要自己手动标注一下xml文件,由于基础有限,内置在yolov3的数据增强代码还需要研究研究,项目急只能用这个笨方法了,大神还是绕步啦,,这个图片预处理完可以增强好多倍数据呀,真是棒~

你可能感兴趣的:(深度学习,编程,深度学习,机器学习,数据挖掘,pytorch)