当前位置:网站首页>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")
边栏推荐
- 使用beef劫持用戶瀏覽器
- Audio-AudioRecord create(一)
- Leetcode t39: combined sum
- Adding color blocks to Seaborn clustermap matrix
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- vscode自定义各个区域的颜色
- 3、Modbus通讯协议详解
- [深度剖析C语言] —— 数据在内存中的存储
- How to recruit Taobao anchor suitable for your own store
- Provincial election + noi Part VI skills and ideas
猜你喜欢
截图小妙招
内存大小端
Comprehensive experiment Li
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
SPL-介绍(一)
MD文档中插入数学公式,Typora中插入数学公式
MAVROS发送自定义话题消息给PX4
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
随机推荐
Book of quantitative trading - reading notes of the man who conquers the market
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
Leetcode t31: prochain arrangement
3、Modbus通讯协议详解
SPL-安装与基本使用(二)
华为机试真题专栏订阅指引
Pipeline detection of UAV Based on gazebo
Why are some Wills made by husband and wife invalid
[深度剖析C语言] —— 数据在内存中的存储
MATLAB小技巧(23)矩阵分析--模拟退火
明明设计的是高带宽,差点加工成开路?
《单片机原理及应用》—定时器、串行通信和中断系统
《微机原理》—总线及其形成
7-26 word length (input and output in the loop)
Count number of rows per group and add result to original data frame
MATLAB【函数求导】
手工挖XSS漏洞
[untitled]
网关gateway-88