"""
Created on Fri May 31 13:41:00 2019
对 500* 500 的TTK 图片进行增广,因为 增广之前的信号灯都比较小
@author: haithink
"""
import cv2
import os
import imgaug as ia
from imgaug import augmenters as iaa
seq = iaa.SomeOf(2, [
iaa.Affine(scale=(0.15, 0.3)),
], random_order=True)
NewTxtPah = "E:\\CPlusDev\\TTKYOLO\\NewTag3\\"
NewJpgsPah = "E:\\CPlusDev\\TTKYOLO\\NewLight3\\"
AugJpgPath = "E:\\CPlusDev\\TTKYOLO\\TTK500Aug3\\"
AugTxtPath = "E:\\CPlusDev\\TTKYOLO\\TTK500AugTxt3\\"
files = os.listdir(NewTxtPah)
iCtrl = 0
counter = 0
NEWSIZE = 500
iCtrl = 0
m = 2
lightId = "18"
for file in files:
if not os.path.isdir(file):
srcImg = cv2.imread(NewJpgsPah + file.split(".")[0] + ".jpg")
height, width, channels = srcImg.shape
srcTxt = open(NewTxtPah+file)
iter_f = iter(srcTxt)
bbs = []
for line in iter_f:
row = line.split(" ")
print(row)
classIdx = int(row[0])
xcenter = int(float(row[1]) * width)
ycenter = int(float(row[2]) * height)
lightW = int(float(row[3]) * width)
lightH = int(float(row[4]) * height)
if(lightW > 300 or lightH > 300):
continue
leftX = max(0, int((xcenter-lightW/2)))
leftY = max(0, int((ycenter-lightH/2)))
rightX = min(width-1, int((xcenter+lightW/2)))
leftBottomY = min(height-1, int((ycenter+lightH/2)))
bb = ia.BoundingBox(x1=leftX, y1=leftY, x2=rightX, y2=leftBottomY);
bbs.append(bb)
for j in range(m):
bbsi = ia.BoundingBoxesOnImage(bbs,shape=srcImg.shape)
seq_det=seq.to_deterministic()
images = [srcImg]
images_aug = seq_det.augment_images(images)
baseName = file.split(".")[0] + "_small_" + str(j)
outputName = baseName + ".jpg"
cv2.imwrite(AugJpgPath + outputName, images_aug[0])
bbs_aug = seq_det.augment_bounding_boxes(bbsi)
newTxt = open(AugTxtPath + baseName+".txt", "w")
for i in range(len(bbs_aug.bounding_boxes)):
after = bbs_aug.bounding_boxes[i]
xcenter = (after.x1 + after.x2) / 2 / width
ycenter = (after.y1 + after.y2) / 2 / height
lightW = (after.x2 - after.x1) / width
lightH = (after.y2 - after.y1 ) / height
info = lightId + " " + str(xcenter) + " " + str(ycenter) + " " + str(lightW) + " " + str(lightH) + "\n"
newTxt.write(info)
newTxt.close()