https://teachablemachine.withgoogle.com/
가위, 바위, 보 100개씩 저장 → 주피터 노트북 데이터 업로드 → 압축풀기
## 리눅스 파일 만들고 압출 풀기 예제
$ mkdir -p ~/aiffel/rock_scissor_paper/scissor
$ cd ~/aiffel/rock_scissor_paper/scissor
$ unzip scissor.zip
mkdir -p 자동으로 중간단계의 디렉토리를 생성하면서 그 하위 디렉토리 생성
from PIL import Image
import glob
print('complete')
파이썬 프로그램 작성 시, 특정한 패턴이나 확장자를 가진 파일들의 경로나 이름이 필요할 때가 있다. glob 모듈의 glob 함수는 사용자가 제시한 조건에 맞는 파일명을 리스트 형식으로 반환
한다.
참조 : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=siniphia&logNo=221397012627
def resize_images(img_path):
images=glob.glob(img_path + "/*.jpg")
target_size=(28,28)
for img in images:
old_img=Image.open(img)
new_img=old_img.resize(target_size,Image.ANTIALIAS)
new_img.save(img, "JPEG")
print(len(images), "img resized.")
image_dir_path = os.getenv("HOME") + "/aiffel/rock_scissor_paper/scissor"
resize_images(image_dir_path)
image_dir_path = os.getenv("HOME") + "/aiffel/rock_scissor_paper/rock"
resize_images(image_dir_path)
image_dir_path = os.getenv("HOME") + "/aiffel/rock_scissor_paper/paper"
resize_images(image_dir_path)
print("이미지 resize 완료!")
Operating System약자, 파일을 복사, 디렉토리 생성, 특정 디렉토리 내 파일목록 구할때 사용
함수
os.getcwd() : 현재경로 값
os.listdir() : 현재경로 존재하는 파일,디렉토리 목록 리스트 반환
os.getenv(”HOME”), os.environ[’HOME’] : 환경변수 문자열 반환(HOME이니까 해당 환경변수)
참조 : https://wikidocs.net/3141
참조 : https://www.daleseo.com/python-os-environ/
import numpy as np
def load_data(img_path, number_of_data=300):
# 가위 : 0, 바위 : 1, 보 : 2
img_size=28
color=3
#이미지 데이터와 라벨(가위 : 0, 바위 : 1, 보 : 2) 데이터를 담을 행렬(matrix) 영역을 생성합니다.
imgs=np.zeros(number_of_data*img_size*img_size*color,dtype=np.int32).reshape(number_of_data,img_size,img_size,color)
labels=np.zeros(number_of_data,dtype=np.int32)
idx=0
for file in glob.iglob(img_path+'/scissor/*.jpg'):
img = np.array(Image.open(file),dtype=np.int32)
imgs[idx,:,:,:]=img # 데이터 영역에 이미지 행렬을 복사
labels[idx]=0 # 가위 : 0
idx=idx+1
for file in glob.iglob(img_path+'/rock/*.jpg'):
img = np.array(Image.open(file),dtype=np.int32)
imgs[idx,:,:,:]=img # 데이터 영역에 이미지 행렬을 복사
labels[idx]=1 # 바위 : 1
idx=idx+1
for file in glob.iglob(img_path+'/paper/*.jpg'):
img = np.array(Image.open(file),dtype=np.int32)
imgs[idx,:,:,:]=img # 데이터 영역에 이미지 행렬을 복사
labels[idx]=2 # 보 : 2
idx=idx+1
print("학습데이터(x_train)의 이미지 개수는", idx,"입니다.")
return imgs, labels
image_dir_path = os.getenv("HOME") + "/aiffel/rock_scissor_paper"
(x_train, y_train)=load_data(image_dir_path)
x_train_norm = x_train/255.0 # 입력은 0~1 사이의 값으로 정규화
print("x_train shape: {}".format(x_train.shape))
print("y_train shape: {}".format(y_train.shape))
데이터 참조:
https://laurencemoroney.com/datasets.html#google_vignette
텐서플로우 케라스에서 Sequential API 방법