數據增強3

# # fw = open('trainyolov3_pigs.txt','w')
# # with open('trainyolov3_pigs.txt') as f:
# #     datas = f.readlines()
# #     for data in datas:
# #         data = data.split()
# #         name = data[0]
#         # cls = '0'
# #         x1,y1,x2,y2 = int(data[2]),int(data[3]),int(data[4]),int(data[5])
# #         cx,cy = (x2 + x1) // 2,(y2 + y1) // 2
# #         w,h = (x2 - x1),(y2 - y1)
# #         fw.write(name + ' ' + cls + ' ' + str(cx) + ' ' + str(cy) + ' ' + str(w) + ' ' + str(h) + '\n')
# # fw.close()
#
from math import *
def rota(img,degree):
    height, width = img.shape[:2]
    # 旋轉後的尺寸
    heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
    widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
    matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
    matRotation[0, 2] += (widthNew - width) / 2  # 重點在這步,目前不懂爲什麼加這步
    matRotation[1, 2] += (heightNew - height) / 2  # 重點在這步
    imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
    return imgRotation

def r180(cx,cy,CX,CY):
    dw, dh = abs(cx - CX), abs(CY - cy)
    if cx > CX:
        cx = CX - dw
    elif cx < CX:
        cx = CX + dw
    if cy > CY:
        cy = CY - dh
    elif cy < CY:
        cy = CY + dh
    # x1, y1 = cx - w // 2, cy - h // 2
    # x2, y2 = x1 + w, y1 + h
    im180 = rota(image, 180)
    # cv2.rectangle(im180, (x1, y1), (x2, y2), (255, 0, 0))
    # cv2.imshow('a', im180)
    # cv2.waitKey(0)
    return im180,cx,cy,w,h

def r90(cx,cy,w,h):
    d = W - cx
    cx = cy
    cy = d
    w,h = h,w
    # x1, y1 = cx - w // 2, cy - h // 2
    # x2, y2 = x1 + w, y1 + h
    im90 = rota(image, 90)
    # cv2.rectangle(im90, (x1, y1), (x2, y2), (255, 0, 0))
    # cv2.imshow('a', im90)
    # cv2.waitKey(0)
    return im90, cx, cy, w, h

def r270(cx,cy,w,h):
    d = H - cy
    cy = cx
    cx = d
    w, h = h, w
    # x1, y1 = cx - w // 2, cy - h // 2
    # x2, y2 = x1 + w, y1 + h
    im270 = rota(image, 270)
    # cv2.rectangle(im270, (x1, y1), (x2, y2), (255, 0, 0))
    # cv2.imshow('a', im270)
    # cv2.waitKey(0)
    return im270, cx, cy, w, h


import cv2
import os
fw = open('重新訓練YOLOV3.txt','w')
n = 0
with open('trainyolov3_pigs.txt') as f:
    datas = f.readlines()
    for data in datas:
        data = data.split()
        name = data[0]
        cx, cy, w, h = int(data[2]), int(data[3]), int(data[4]), int(data[5])
        image = cv2.imread(os.path.join(r'D:\trainpigs', name))
        cv2.imwrite(r'D:\trainsYOLOV3sNewDatas\{}.jpg'.format(n),image)
        fw.write('{}.jpg'.format(n) + ' ' + '0' + ' ' + str(cx) + ' ' + str(cy) + ' ' + str(w) + ' ' + str(h) + '\n')
        n += 1
        H, W, _ = image.shape
        CX, CY = W // 2, H // 2
        im180, cx1, cy1, w1, h1 = r180(cx,cy,CX,CY)
        cv2.imwrite(r'D:\trainsYOLOV3sNewDatas\{}.jpg'.format(n),im180)
        fw.write('{}.jpg'.format(n) + ' ' + '0' + ' '+ str(cx1) + ' ' + str(cy1) + ' ' + str(w1) + ' ' + str(h1) + '\n')
        n += 1
        im90, cx2, cy2, w2, h2 = r90(cx,cy,w,h)
        cv2.imwrite(r'D:\trainsYOLOV3sNewDatas\{}.jpg'.format(n),im90)
        fw.write('{}.jpg'.format(n) + ' ' + '0' + ' ' + str(cx2) + ' ' + str(cy2) + ' ' + str(w2) + ' ' + str(h2) + '\n')
        n += 1
        im270, cx3, cy3, w3, h3 = r270(cx,cy,w,h)
        cv2.imwrite(r'D:\trainsYOLOV3sNewDatas\{}.jpg'.format(n),im270)
        fw.write('{}.jpg'.format(n) + ' ' + '0' + ' ' + str(cx3) + ' ' + str(cy3) + ' ' + str(w3) + ' ' + str(h3) + '\n')
        n += 1
fw.close()

# import cv2
# import os
# with open('重新訓練YOLOV3.txt') as f:
#     datas = f.readlines()
#     for data in datas:
#         data = data.split()
#         name = data[0]
#         cx,cy,w,h = int(data[2]),int(data[3]),int(data[4]),int(data[5])
#         x1, y1 = cx - w // 2, cy - h // 2
#         x2, y2 = x1 + w, y1 + h
#         image = cv2.imread(os.path.join(r'D:\trainsYOLOV3sNewDatas',name))
#         image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0))
#         cv2.imshow('a',image)
#         cv2.waitKey(0)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章