当前位置:网站首页>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")边栏推荐
- Codeforces Round #803 (Div. 2) VP补题
- MATLAB【函数求导】
- 2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
- Mavros sends a custom topic message to Px4
- There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
- 使用 setoolkit 伪造站点窃取用户信息
- 2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
- [deep analysis of C language] - data storage in memory
- Luogu p1088 [noip2004 popularization group] Martians
- seaborn clustermap矩阵添加颜色块
猜你喜欢

There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way

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

MD文档中插入数学公式,Typora中插入数学公式

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation

Vscode customize the color of each area

Mavros sends a custom topic message to Px4

factory type_ Id:: create process resolution

Agrometeorological environment monitoring system

vscode自定义各个区域的颜色

R语言入门
随机推荐
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
factory type_ Id:: create process resolution
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
Gateway-88
The use of word in graduation thesis
基于Gazebo的无人机管道检测
一套十万级TPS的IM综合消息系统的架构实践与思考
vscode自定义各个区域的颜色
Provincial election + noi Part VI skills and ideas
《微机原理》—总线及其形成
There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
Configuration and startup of Chang'an chain synchronization node
R语言入门
Model and view of QT
深度学习训练样本扩增同时修改标签名称
Serial port to WiFi module communication
【华为机试真题详解】判断字符串子序列【2022 Q1 Q2 | 200分】
Principle and application of single chip microcomputer - principle of parallel IO port
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
MD文档中插入数学公式,Typora中插入数学公式