当前位置:网站首页>深度学习训练样本扩增同时修改标签名称
深度学习训练样本扩增同时修改标签名称
2022-07-01 08:16:00 【天人合一peng】
之前的是在不同文件夹,自己手动修改
# -*- coding:utf-8 -*-
import sys
# sys.path.append('E:\\Anaconda\\libs')
import os # os:操作系统相关的信息模块
import shutil
from os.path import join
import cv2
import numpy as np
import random # 导入随机函数
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# 存放原始图片地址
data_base_dir = '../switch/images/train2017'
label_path = '../switch/labels/train2017'
file_dir = '../switch/images/train2017'
dest_dir = "./VINS"
# dest_dir1 = r"E:\VINS\train01"
# dest_dir2 = r"E:\VINS\train02"
# dest_dir3 = r"E:\VINS\train03"
# dest_dir4 = r"E:\VINS\train04"
# dest_dir5 = r"E:\VINS\train05"
# dest_dir6 = r"E:\VINS\train06"
# dest_dir7 = r"E:\VINS\train07"
# dest_dir8 = r"E:\VINS\train08"
# dest_dir9 = r"E:\VINS\train09"
# dest_dir10 = r"E:\VINS\train10"
# dest_dir11 = r"E:\VINS\train11"
# dest_dir12 = r"E:\VINS\train12"
if not os.path.exists(dest_dir): #判断所在目录下是否有该文件名的文件夹
os.mkdir(dest_dir)
file_list = [] # 建立列表,用于保存图片信息
# 读取图片文件,并将图片地址、图片名和标签写到txt文件中
def imgBrightness(img1, c, b):
rows, cols, channels = img1.shape
blank = np.zeros([rows, cols, channels], img1.dtype)
rst = cv2.addWeighted(img1, c, blank, 1 - c, b)
return rst
def noise(img, snr):
h = img.shape[0]
w = img.shape[1]
img1 = img.copy()
sp = h * w # 计算图像像素点个数
NP = int(sp * (1 - snr)) # 计算图像椒盐噪声点个数
for i in range(NP):
randx = np.random.randint(1, h - 1) # 生成一个 1 至 h-1 之间的随机整数
randy = np.random.randint(1, w - 1) # 生成一个 1 至 w-1 之间的随机整数
if np.random.random() <= 0.5: # np.random.random()生成一个 0 至 1 之间的浮点数
img1[randx, randy] = 0
else:
img1[randx, randy] = 255
return img1
for file in os.listdir(data_base_dir): # file为current_dir当前目录下图片名
if file.endswith(".jpg"): # 如果file以jpg结尾
for item in range(13):
# print(file)
imageName = "VIN" + str(item) + file[:-4] + '.jpg'
labelName = "VIN" + str(item) + file[:-4] + '.txt'
if item == 0:
# 原图不复制了
scr_dir = join(file_dir, file)
shutil.copyfile(scr_dir, os.path.join(dest_dir, imageName))
# shutil.copyfile(scr_dir, det)
# 增强图片
if item == 1:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
gaussianBlur = cv2.GaussianBlur(img, (3, 3), 3)
blur = cv2.blur(gaussianBlur, (5, 5)) # 模糊图片
# cv2.imshow("img1", blur)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, blur)
if item == 2:
img = cv2.imread(scr_dir)
boxblur = cv2.boxFilter(img, -1, (9, 9)) # 模糊图片*2
# cv2.imshow("img2", boxblur)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur)
# 加椒盐噪声小
if item == 3:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
# cv2.imshow("img3", noiseimage)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage)
# # 加椒盐噪声中
# if item == 4:
# img = cv2.imread(scr_dir)
# noiseimage4 = noise(img, 0.90) # 将信噪比设定为0.6
# # cv2.imshow("img4", noiseimage4)
# # cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage4)
#
# # 加椒盐噪声多
# if item == 5:
# img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
# noiseimage5 = noise(img, 0.86) # 将信噪比设定为0.6
# cv2.imshow("img5", noiseimage5)
# cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage5)
# 加椒盐噪声加高斯模糊
if item == 6:
img = cv2.imread(scr_dir)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
gaussianBlur6 = cv2.GaussianBlur(noiseimage, (3, 3), 3)
# cv2.imshow("img3", gaussianBlur6)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, gaussianBlur6)
# 加椒盐噪声加高斯模糊
if item == 7:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
boxblur7 = cv2.boxFilter(noiseimage, -1, (9, 9))
# cv2.imshow("img7", boxblur7)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur7)
# 高斯模糊加椒盐噪声
if item == 8:
img = cv2.imread(scr_dir)
gaussianBlur = cv2.GaussianBlur(img, (5, 5), 3)
noiseimage8 = noise(gaussianBlur, 0.98) # 将信噪比设定为0.6
# cv2.imshow("img8", noiseimage8)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage8)
# # 双边模糊加椒盐噪声
# if item == 9:
# img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
#
# bilBlur = cv2.bilateralFilter(img, 1, 25, 10)
# noiseimage9 = noise(bilBlur, 0.92) # 将信噪比设定为0.6
# cv2.imshow("img9", noiseimage9)
# cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage9)
# 调亮
if item == 10:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.6, 1)
# cv2.imshow("img10", rst)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, rst)
# 调亮并模糊
if item == 11:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.4, 4)
boxblur11 = cv2.boxFilter(rst, -1, (3, 3))
# cv2.imshow("img11", boxblur11)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur11)
# 调亮加椒盐噪声
if item == 12:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.28, 4)
noiseimage12 = noise(rst, 0.98) # 将信噪比设定为0.6
cv2.imshow("img12", noiseimage12)
cv2.waitKey(3)
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage12)
shutil.copyfile(os.path.join(label_path, file[:-4] + '.txt'), os.path.join(dest_dir, labelName))
print("all down")边栏推荐
- CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
- XX攻击——反射型 XSS 攻击劫持用户浏览器
- OJ输入输出练习
- Comprehensive experiment Li
- 基于Gazebo的无人机管道检测
- 【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
- Use threejs simple Web3D effect
- 【Redis】一气呵成,带你了解Redis安装与连接
猜你喜欢
![[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order](/img/87/07783593dbabcf29700fa207ecda08.png)
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order

谈谈数字化转型的几个关键问题

毕业论文中word的使用1-代码域标公式

Conception et mise en service du processeur - chapitre 4 tâches pratiques

【Redis】一气呵成,带你了解Redis安装与连接
![Thread safety analysis of [concurrent programming JUC] variables](/img/f9/a3604bec6f7e5317dd2c578da73018.jpg)
Thread safety analysis of [concurrent programming JUC] variables

Suivi des cibles de manoeuvre - - mise en oeuvre du modèle statistique actuel (modèle CS) filtre Kalman étendu / filtre Kalman sans trace par MATLAB
![[getting started] intercepting strings](/img/16/363baa4982408f55493057200bcba5.png)
[getting started] intercepting strings

《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
![[untitled]](/img/d9/5e97f2de256b9749131b5bf1437d24.png)
[untitled]
随机推荐
使用 setoolkit 伪造站点窃取用户信息
【入门】取近似值
Intelligent water supply system solution
SPL-安装与基本使用(二)
Intelligent water conservancy solution
To prevent "activation" photos from being muddled through, databao "live detection + face recognition" makes face brushing safer
Internet of things technology is widely used to promote intelligent water automation management
Count number of rows per group and add result to original data frame
Instead of houses, another kind of capital in China is rising
[getting started] input n integers and output the smallest K of them
軟鍵盤高度報錯
How can beginners correctly understand Google's official suggested architectural principles (questions?)
量化交易之读书篇 - 《征服市场的人》读书笔记
Huawei machine test questions column subscription Guide
Leetcode t34: find the first and last positions of elements in a sorted array
Hijacking a user's browser with beef
Tita OKR: a dashboard to master the big picture
程序员养生宝典
[introduction] approximate value
Codeworks round 803 (Div. 2) VP supplement