当前位置:网站首页>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")
边栏推荐
- Download jackson codehaus. org jar - downloading jackson. codehaus. org jar
- Comprehensive experiment Li
- factory type_ Id:: create process resolution
- 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
- Matlab [function derivation]
- Using settoolkit to forge sites to steal user information
- Leetcode T40: 组合总和II
- Yolov5 advanced 7 target tracking latest environment setup
- MAVROS发送自定义话题消息给PX4
- Review of week 280 of leetcode
猜你喜欢
MD文档中插入数学公式,Typora中插入数学公式
What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
一套十万级TPS的IM综合消息系统的架构实践与思考
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation
《单片机原理及应用》-片外拓展
Comprehensive experiment Li
Hijacking a user's browser with beef
factory type_id::create过程解析
NIO-零拷贝
随机推荐
Anddroid text to speech TTS implementation
Koltin35, headline Android interview algorithm
截图小妙招
Review of week 280 of leetcode
seaborn clustermap矩阵添加颜色块
如何招到适合自己店铺的淘宝主播
网关gateway-88
Intelligent water supply system solution
分享2022上半年我读过的7本书
MAVROS发送自定义话题消息给PX4
2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
factory type_id::create过程解析
【无标题】
Leetcode T34: 在排序数组中查找元素的第一个和最后一个位置
MD文档中插入数学公式,Typora中插入数学公式
Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
R语言入门
Rumtime 1200 upgrade: London upgrade support, pledge function update and more