当前位置:网站首页>In depth learning training sample amplification and tag name modification
In depth learning training sample amplification and tag name modification
2022-07-01 08:34:00 【Harmony between man and nature Peng】
The previous one is in different folders , Modify by yourself
# -*- coding:utf-8 -*-
import sys
# sys.path.append('E:\\Anaconda\\libs')
import os # os: Operating system related information modules
import shutil
from os.path import join
import cv2
import numpy as np
import random # Import random functions
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# The address where the original picture is stored
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): # Judge whether there is a folder with this file name under the directory
os.mkdir(dest_dir)
file_list = [] # Create a list , Used to save picture information
# Read picture file , And send the picture address 、 The picture name and label write txt In file
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 # Calculate the number of image pixels
NP = int(sp * (1 - snr)) # Calculate the number of salt and pepper noise points in the image
for i in range(NP):
randx = np.random.randint(1, h - 1) # Generate a 1 to h-1 Random integer between
randy = np.random.randint(1, w - 1) # Generate a 1 to w-1 Random integer between
if np.random.random() <= 0.5: # np.random.random() Generate a 0 to 1 The floating point number between
img1[randx, randy] = 0
else:
img1[randx, randy] = 255
return img1
for file in os.listdir(data_base_dir): # file by current_dir Picture name in current directory
if file.endswith(".jpg"): # If file With jpg ending
for item in range(13):
# print(file)
imageName = "VIN" + str(item) + file[:-4] + '.jpg'
labelName = "VIN" + str(item) + file[:-4] + '.txt'
if item == 0:
# The original picture is not copied
scr_dir = join(file_dir, file)
shutil.copyfile(scr_dir, os.path.join(dest_dir, imageName))
# shutil.copyfile(scr_dir, det)
# Enhance the picture
if item == 1:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
gaussianBlur = cv2.GaussianBlur(img, (3, 3), 3)
blur = cv2.blur(gaussianBlur, (5, 5)) # Blur the picture
# 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)) # Blur the picture *2
# cv2.imshow("img2", boxblur)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur)
# Add salt and pepper and make less noise
if item == 3:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # Set the signal-to-noise ratio to 0.6
# cv2.imshow("img3", noiseimage)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage)
# # Add salt and pepper to the noise
# if item == 4:
# img = cv2.imread(scr_dir)
# noiseimage4 = noise(img, 0.90) # Set the signal-to-noise ratio to 0.6
# # cv2.imshow("img4", noiseimage4)
# # cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage4)
#
# # Add more salt and pepper
# if item == 5:
# img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
# noiseimage5 = noise(img, 0.86) # Set the signal-to-noise ratio to 0.6
# cv2.imshow("img5", noiseimage5)
# cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage5)
# Add salt and pepper noise and Gaussian Blur
if item == 6:
img = cv2.imread(scr_dir)
noiseimage = noise(img, 0.98) # Set the signal-to-noise ratio to 0.6
gaussianBlur6 = cv2.GaussianBlur(noiseimage, (3, 3), 3)
# cv2.imshow("img3", gaussianBlur6)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, gaussianBlur6)
# Add salt and pepper noise and Gaussian Blur
if item == 7:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # Set the signal-to-noise ratio to 0.6
boxblur7 = cv2.boxFilter(noiseimage, -1, (9, 9))
# cv2.imshow("img7", boxblur7)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur7)
# Gaussian blur plus salt and pepper noise
if item == 8:
img = cv2.imread(scr_dir)
gaussianBlur = cv2.GaussianBlur(img, (5, 5), 3)
noiseimage8 = noise(gaussianBlur, 0.98) # Set the signal-to-noise ratio to 0.6
# cv2.imshow("img8", noiseimage8)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage8)
# # Bilateral blur plus salt and pepper noise
# if item == 9:
# img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
#
# bilBlur = cv2.bilateralFilter(img, 1, 25, 10)
# noiseimage9 = noise(bilBlur, 0.92) # Set the signal-to-noise ratio to 0.6
# cv2.imshow("img9", noiseimage9)
# cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage9)
# Dimming
if item == 10:
img = cv2.imread(scr_dir)
# The second parameter adjusts the brightness , The bigger, the brighter , The smaller, the darker
rst = imgBrightness(img, 1.6, 1)
# cv2.imshow("img10", rst)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, rst)
# Brighten and blur
if item == 11:
img = cv2.imread(scr_dir)
# The second parameter adjusts the brightness , The bigger, the brighter , The smaller, the darker
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)
# Brighten and add salt and pepper noise
if item == 12:
img = cv2.imread(scr_dir)
# The second parameter adjusts the brightness , The bigger, the brighter , The smaller, the darker
rst = imgBrightness(img, 1.28, 4)
noiseimage12 = noise(rst, 0.98) # Set the signal-to-noise ratio to 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")
边栏推荐
- Provincial election + noi Part III tree problems
- 《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
- SPL installation and basic use (II)
- 2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
- 为什么LTD独立站就是Web3.0网站!
- 機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
- Yolov5进阶之七目标追踪最新环境搭建
- 长安链同步节点配置与启动
- Intelligent water conservancy solution
- leetcode T31:下一排列
猜你喜欢
shardingSphere
《单片机原理及应用》-片外拓展
使用 setoolkit 伪造站点窃取用户信息
Huawei machine test questions column subscription Guide
factory type_id::create过程解析
Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
MATLAB小技巧(23)矩阵分析--模拟退火
Principle and application of single chip microcomputer - principle of parallel IO port
Utiliser Beef pour détourner le navigateur utilisateur
【面试必刷101】链表
随机推荐
Provincial election + noi part I dynamic planning DP
Mavros sends a custom topic message to Px4
Utiliser Beef pour détourner le navigateur utilisateur
Qt的模型与视图
Yolov5 advanced 7 target tracking latest environment setup
CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試
On several key issues of digital transformation
Share 7 books I read in the first half of 2022
[no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
【js逆向】md5加密参数破解
The data analyst will be ruined without project experience. These 8 project resources will not be taken away
Comprehensive experiment Li
leetcode T31:下一排列
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
Adding color blocks to Seaborn clustermap matrix
一文纵览主流 NFT 市场平台版税、服务费设计
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
爬虫知识点总结
截图小妙招
深度学习训练样本扩增同时修改标签名称