OpenCV

크롤링 한 이미지 사진 새로운 이미지 생성

Managemnet S/W saehyeong.woo 2022. 8. 25. 14:57
SMALL

AI의 인공신경망 학습을 위해선 많은양의 데이터가 필요하다.

OpenCV라이브러리를 사용하여 여러장의 이미지를 random으로 회전을 시키거나, 상하좌우반전을 시키고 회전을 하게되면 새로운 이미지가 몇 10배로 늘어난다. 그것을 작동하는 코드이다.

import cv2
import os
import glob
import sys



img_files =glob.glob('C:\Users\User\Desktop\crawling\selenium\Scripts\openCV\\*.jpg')

if not img_files:
    print("jpg 이미지가 없습니다.")
    sys.exit()

index = 0

while True:
 for index in range(0,9): //0~10000 몇장을 할것인가 
 
   img = cv2.imread(img_files[index], cv2.IMREAD_COLOR)
 
   img90 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) # 시계방향으로 90도 회전
   img180 = cv2.rotate(img, cv2.ROTATE_180) # 180도 회전
   img270 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) # 반시계방향으로 90도 회전
                                                         # = 시계방향으로 270도 회전
   imgreversal = cv2.flip(img,-1)
 
   cv2.imwrite(f'C:\Users\User\Desktop\crawling\selenium\Scripts\openCV\(Original{index})USB type A.jpg', img)
   cv2.imwrite(f'C:\Users\User\Desktop\crawling\selenium\Scripts\openCV(90degrees{index})USB type A.jpg', img90)
   cv2.imwrite(f'C:\Users\User\Desktop\crawling\selenium\Scripts\openCV(180degrees{index})USB type A.jpg', img180)
   cv2.imwrite(f'C:\Users\User\Desktop\crawling\selenium\Scripts\openCV(270degrees{index})USB type A.jpg', img270)
   cv2.imwrite(f'C:\Users\User\Desktop\crawling\selenium\Scripts\openCV(270degrees{index})USB type A.jpg', imgreversal)
 
   cv2.waitKey(0)
   cv2.destroyAllWindows()

각각의 이미지를 회전 시키면 4,100 -> 29,120 장의 새로운 이미지가 생성된것을 확인 할 수 있다.

LIST