感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。
感謝參考原文-http://bjbsair.com/2020-04-01/tech-info/18286.html
為了使機器更智能,開發人員正在研究機器學習和深度學習技術。人類通過反復練習和重復執行任務來學習執行任務,從而記住了如何執行任務。然后,他大腦中的神經元會自動觸發,它們可以快速執行所學的任務。深度學習與此也非常相似。它針對不同類型的問題使用不同類型的神經網絡體系結構。對象識別,圖像和聲音分類,對象檢測,圖像分割等。
什么是手寫數字識別?
手寫數字識別是計算機識別人類手寫數字的能力。對於機器而言,這是一項艱巨的任務,因為手寫數字不是完美的,可以用多種方法。手寫數字識別是使用數字圖像並識別圖像中存在的數字的解決方案。
在本文中,我們將使用MNIST數據集實現一個手寫數字識別應用程序。我們將使用一種特殊類型的深度神經網絡,即卷積神經網絡。最后,我們將構建一個GUI,您可以在其中繪制數字並立即識別它。
在這里我們使用Keras庫和Tkinter庫進行深度學習以構建GUI。
MNIST數據集
這可能是機器學習和深度學習愛好者中最受歡迎的數據集之一。該MNIST數據集包含的手寫數字從0到9 60000個訓練圖像和10,000張進行測試。因此,MNIST數據集具有10個不同的類。手寫數字圖像表示為28×28矩陣,其中每個單元格都包含灰度像素值。
步驟如下:
1、導入庫並加載數據集
首先,我們將導入訓練模型所需的所有模塊。Keras庫已經包含一些數據集,而MNIST是其中之一。因此,我們可以輕松導入數據集並開始使用它。該mnist.load_data()方法返回訓練數據,它的標簽,也是測試數據和標簽。
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
2、預處理數據
圖像數據無法直接輸入模型中,因此我們需要執行一些操作並處理數據以使其准備好用於神經網絡。訓練數據的維數為(60000,28,28)。CNN模型將需要一維,因此我們將矩陣重塑為(60000,28,28,1)。
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
3、創建模型
現在,我們開始創建CNN模型。CNN模型通常由卷積層和池化層組成。它對於以網格結構表示的數據更有效,這就是CNN能夠很好地解決圖像分類問題的原因。dropout層用於使一些神經元失活,在訓練時,它會降低模型的擬合度。然后,我們將使用Adadelta優化器來編譯模型。
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
4、訓練模型
Keras的model.fit()函數將開始對模型的訓練。它獲取訓練數據、驗證數據、周期和批大小。
訓練模型需要一些時間。在培訓之后,我們將權重和模型定義保存在“ mnist.h5”文件中。
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
5、評估模型
我們的數據集中有10,000張圖像,這些圖像將用於評估模型的效果。測試數據不參與數據的訓練,因此,這是我們模型的新數據。MNIST數據集平衡良好,因此我們可以獲得約99%的准確性。
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
6、創建GUI來預測數字
現在,對於GUI,我們創建了一個新文件,在其中創建了一個交互式窗口以在對話框中繪制數字,並使用按鈕可以識別數字。Tkinter庫位於Python標准庫中。我們創建了一個函數predict_digit(),將圖像作為輸入,然后使用經過訓練的模型來預測數字。
然后,我們創建App類,他負責為我們的應用構建GUI。我們創建一個對話框,在其中可以通過捕獲鼠標事件進行繪制,並使用一個按鈕來觸發predict_digit()函數並顯示結果。
以下是完整的gui_digit_recognizer.py文件的代碼:
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black')
app = App()
mainloop()
結論
在本文中,我們已經在手寫數字識別應用程序上成功構建了我們的項目。我們已經構建並訓練了卷積神經網絡,該網絡對於圖像分類非常有效。稍后,我們構建GUI,在對話框上繪制一個數字,然后對數字進行分類並顯示結果。