当前位置:网站首页>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")边栏推荐
- Leetcode T40: 组合总和II
- 使用beef劫持用户浏览器
- SPL-介绍(一)
- Field agricultural irrigation system
- SPL installation and basic use (II)
- 分享2022上半年我读过的7本书
- 7-26 word length (input and output in the loop)
- seaborn clustermap矩阵添加颜色块
- The data analyst will be ruined without project experience. These 8 project resources will not be taken away
- [untitled]
猜你喜欢

shardingSphere
![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)
Matlab [function derivation]

The data analyst will be ruined without project experience. These 8 project resources will not be taken away

MAVROS发送自定义话题消息给PX4
![[JS reverse] MD5 encryption parameter cracking](/img/06/0610045d287f646479d6eb5021a067.png)
[JS reverse] MD5 encryption parameter cracking

《单片机原理及应用》-片外拓展

Pipeline detection of UAV Based on gazebo

Adding color blocks to Seaborn clustermap matrix

SPL Introduction (I)

15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
随机推荐
Book of quantitative trading - reading notes of the man who conquers the market
Review of week 280 of leetcode
我想知道手机注册股票开户的流程?另外,手机开户安全么?
避免按钮重复点击的小工具bimianchongfu.queren()
一文纵览主流 NFT 市场平台版税、服务费设计
Provincial selection + noi Part II string
The difference between interceptors and filters
如何招到适合自己店铺的淘宝主播
CPU设计实战-第四章实践任务一简单CPU参考设计调试
【js逆向】md5加密参数破解
shardingSphere
Yolov5进阶之六目标追踪环境搭建
Utiliser Beef pour détourner le navigateur utilisateur
factory type_id::create过程解析
Mavros sends a custom topic message to Px4
Comprehensive experiment Li
Leetcode t31: next spread
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
Internet of things technology is widely used to promote intelligent water automation management
Leetcode t39: combined sum