본문 바로가기

Study/class note

딥러닝 / 신경망 활용 사용자 인터페이스 만들기

13  신경망 활용 사용자 인터페이스 만들기

https://data-flair.training/blogs/cats-dogs-classification-deep-learning-project-beginners/

import tkinter as tk  # Gui 모듈 
from tkinter import filedialog
from tkinter import *

#이미지 데이터 전처리하는 모듈과 텐서 플로우 케라스 load_model모듈을 불러옴
from PIL import ImageTk, Image
import numpy
from keras.models import load_model
import cv2


# 모델을 불러옵니다.
model = load_model('c:\\deep\\new_leaf_model2.h5')

# 화면을 띄웁니다.
top=tk.Tk() #TK클래스 객체 생성
top.geometry('800x600')  #사용자 화면 가로 세로 사이즈 지정
top.title('Leaf Classification') #상단바 타이틀
top.configure(background='#CDCDCD')
label=Label(top,background='#CDCDCD', font=('arial',15,'bold'))
sign_image = Label(top)

def classify(file_path):
#     global label_packed
    #image = Image.open(file_path)
    #image = image.resize((32,32))  #학습할때 cv2는 리사이즈했기 때문에 cv2로 리사이즈해야함.
    
    image = cv2.imread(file_path)  # 파일 경로에 있는 이미지 불러옴
    resize_img = cv2.resize(image, (32 , 32), interpolation=cv2.INTER_CUBIC)
    image = numpy.expand_dims(resize_img, axis=0)
    image = numpy.array(image) # 신경망에 넣기 전에 numpy array로 변경
    image = image/255  #정규화
    results = model.predict(image,batch_size=1) #예측, batch_size = 1 한 장 넣고 예측
    a = np.argmax(results)  #[0.99 0.01]의 확률벡터에서 가장 큰 원소의 인덱스 번호를 1에 담음
    if a ==1:
        sign = '정상'
    else:
        sign = '질병'

    label.configure(foreground='#011638', text=sign) 
    
def show_classify_button(file_path):  #분류 버튼 
    classify_b=Button(top,text="Classify Image",
    command=lambda: classify(file_path), padx=10,pady=5)
    classify_b.configure(background='#364156', foreground='white',font=('arial',10,'bold'))
    classify_b.place(relx=0.79,rely=0.46)
    
def upload_image():  #이미지 업로드 버튼
    try:
        file_path=filedialog.askopenfilename()
        uploaded=Image.open(file_path)
        uploaded.thumbnail(((top.winfo_width()/2.25),(top.winfo_height()/2.25)))
        im=ImageTk.PhotoImage(uploaded)
        sign_image.configure(image=im)
        sign_image.image=im
        label.configure(text='')
        show_classify_button(file_path)
    except:
        pass
    
upload=Button(top,text="Upload an image",command=upload_image,padx=10,pady=5)
upload.configure(background='#364156', foreground='white',font=('arial',10,'bold'))
upload.pack(side=BOTTOM,pady=50)
sign_image.pack(side=BOTTOM,expand=True)
label.pack(side=BOTTOM,expand=True)
heading = Label(top, text="이파리 질병 유무 분류",pady=20, font=('arial',20,'bold'))
heading.configure(background='#CDCDCD',foreground='#364156')
heading.pack()



top.mainloop()

 

반응형