https://teachablemachine.withgoogle.com 접속한다.
Get Started 클릭한다.
학습시키고자 하는 컨텐츠를 클릭한다.
원하는 개수의 클래스(분류 기준)를 설정한다.
각각의 클래스에 이름을 부여하고 각 분류에 맞는 이미지(웹캠)를 업로드한다.
학습시킬 이미지의 개수는 많을 수록 좋다.
Training(학습)을 시킨다.
Webcam을 활성화시켜 테스트해본다.
테스트 결과가 이상이 없으면 Export Model 버튼을 눌러준다.
'Upload (Shareable link)'를 체크한다.
'Upload my model' 버튼을 눌러 생성한 모델을 Web에 업로드 시킨다.
새롭게 생성된 링크를 복사하여 인터넷 주소창에 붙여넣기 한다.
'Input' 옵션을 ON으로 바꿔준다.
'File' 또는 'Webcam'을 고른 후 검증 대상을 테스트해본다.
TensorFlow 메뉴에서 Keras를 선택한 후 'Download my model' 버튼을 눌러 생성한 모델을 다운로드한다.
다운받은 폴더를 열어보면 두 개의 파일이 생성되어 있다.
'Windows 키 + R'을 눌러 관리자 권한으로 명령 프롬프트를 실행한다.
명령 프롬프트에서 다음 명령어를 실행하여 '새로운 가상환경'을 생성한다.
C:\Project\Keras> conda create –n venvtm python=3.8
다음 명령으로 '가상환경'을 실행한다.
C:\Project\Keras> activate venvtm
다음 명령으로 가상환경에 주피터 노트북을 설치한다.
C:\Project\Keras> conda install –n venvtm ipython notebook
python의 pip를 이용하여 'TensorFlow', 'Pillow', 'Numpy', 'OpenCV', 'pySerial'을 설치한다.
pip install tensorflow==2.3.1 (Python 3.8.6 버전에서 설치)
pip install pillow
pip install numpy
pip install opencv-python 또는 pip install opencv-contrib-python
pip install pyserial
다음 명령으로 'Jupyter Notebook'을 실행한다.
C:\Project\Keras> jupyter notebook
'keras_model.h5' 파일과 같은 폴더 안에 테스트 할 그림 파일 하나(test_photo.jpg)를 복사해준다.
Jupyter Notebook에 위와 같은 코드를 넣어준다.
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
# Load the model
model = tensorflow.keras.models.load_model('keras_model.h5', compile=False)
# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is determined
# by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open('test_photo.jpg')
# resize the image to a 224x224 with the same strategy as in TM2:
# resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
# turn the image into a numpy array
image_array = np.asarray(image)
# display the resized image
image.show()
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# Load the image into the array
data[0] = normalized_image_array
# run the inference
prediction = model.predict(data)
print(prediction)
'Shift+Enter' 키를 눌러 실행하면 아래와 같은 예측 결과를 얻는다.
[[0.999841 0.00015903]]
Jupyter Notebook에 아래와 같은 코드를 넣어준다.
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np
import cv2
np.set_printoptions(suppress=True)
model = tensorflow.keras.models.load_model('keras_model.h5', compile=False)
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
image = cv2.resize(frame, dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
image_array = np.asarray(image)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
data[0] = normalized_image_array
prediction = model.predict(data)
print(prediction)
if prediction[:, 0] > 0.7 :
send = (str('a')+'\n').encode("utf-8")
print(send)
if prediction[:, 1] > 0.7 :
send = (str('b')+'\n').encode("utf-8")
print(send)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Jupyter Notebook에 아래와 같은 코드를 넣어준다.
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np
import cv2
import serial
ser = serial.Serial(
# port='/dev/cu.usbmodem141201',
port='COM3',
baudrate=9600,
)
np.set_printoptions(suppress=True)
model = tensorflow.keras.models.load_model('keras_model.h5', compile=False)
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
image = cv2.resize(frame, dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
image_array = np.asarray(image)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
data[0] = normalized_image_array
prediction = model.predict(data)
print(prediction)
# print(prediction[:, 0])
# print(prediction[:, 1])
# print(prediction[:, 2])
if prediction[:, 0] > 0.7 :
send = (str('a')+'\n').encode("utf-8")
ser.write(send)
print(send)
if prediction[:, 1] > 0.7 :
send = (str('b')+'\n').encode("utf-8")
ser.write(send)
print(send)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()