前提・実現したいこと
前提として
・インターネットから既存の手書きデータは取れない。
・手書きのみ
・キャンバスに描画した数字を画像⇨文字列に変換して学習モデルと比較
・scikit-learn で教師あり学習モデルを作成
現在キャンバス上に描画した数字を判定して予測結果を出力するプログラムを作成してしています。
この予測結果の正解率が40%ほどで90%にあげたいと考えています。
発生している問題・エラーメッセージ
### 該当のソースコード g
コード
module.py ———————————— from tkinter import * from PIL improt Image ,ImageGrab import tkinter.filedialog as fd import sklearn.svm import SVC import numpy class Drawing: #コンストラクタ def __init__(self): self.__window = self.__create_window() #画面を構成 def __create_window(self): window = Tk() window.geometry(‘400x400’) window.tilte(‘数字判断アプリ’) self.__label = Label(text = ‘判断結果:?’) self.__label = Label.pack() self.__canvas = Canvas(width = 200, bg = ‘white’) self.__canvas.bind(‘<ButtonPress-1>’,self__.on_pressed) self.__canvas.bind(‘<B1-Motion>’,self.__on_dragged self.__canvas.pack() judge_button= Button(text =‘判定’, command = self.__judge_digit) judge_button.pack() clear_button = Button(text =‘消去’, command = self.__clear) return window #実行 def run(self): self.__window.mainloop() #マウスを押す def __on_pressed(self,e): x = e.x y = e.y self.__canvas.create_oval(x-15,y-15,x+15,y+15,fill=‘black’ #画像データを数値リストに変換 def __convert_image(self,img): gray_image = img.convert(‘L’) gray_imgae = gray_image.resize((8,8),Image.ANTIALIAS) img_array = numpy.asarray(gray_image,dtype = float) img_array = numpy.floor(16-16*(img_array/256)) img_array = img_array.flatten() return img_array #数字判定予測 def __predict_digits(self,target_data): digits = ds.load_digits() learn_data = digits.data #学習データ result_data = digits.target #結果データ model =SVC(gamma = 0.001) model.fit(learn_data,result_data) n = model.predict([target_data]) self.__label.config(text = ‘判定結果: ‘ +str(n[0])) print(n) #判定 def __judge_digits(self): x1 = self.__window.winfo_riootx() + self.__canvas.winfo_x() y1 = self.__window.winfo_riooty() + self.__canvas.winfo_y() x2 = x1 + self.__canvas.winfo_width() y2 = y1 + self.__canvas.winfo_height() img = ImageGrap.grap() img = img.crop((x1,y1,x2,y2)) target_data = self.__convert_image(img) self.__predict_digits(target_data) #消去 def __clear(self): x1= 0 y1= 0 x2= x1 + self.__canvas.winfo_width() y2= y1 + self.__canvas.winfo_height() self.__canvas.create_rectangle(x1,y1,x2,y2,fill=‘white’,width=0) self.__label.config(text=‘判定結果: ?’) ———————————— start_up.py ———————————— from module import Drawing draw = Drawing() draw.run()
試したこと
・学習モデルに使用する手書き数字画像(0〜9)を各70枚ほど用意
・画像を8×8ピクセルにリサイズ
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。