機(jī)器愛(ài)學(xué)習(xí)之?dāng)z像頭mnist識(shí)別手寫(xiě)字

本教程是基于python 3.5 + opencv 3.3 + tensorflow 1.3 + win10 64

1.基于mnist用keras訓(xùn)練出手寫(xiě)識(shí)別模型

from keras.models import Sequential,load_model

from keras.layers import Dense, Activation

from keras.datasets import mnist

from keras.utils import np_utils

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(-1,28*28)

x_test = x_test.reshape(-1,28*28)

y_train = np_utils.to_categorical(y_train,10)

y_test = np_utils.to_categorical(y_test,10)

model= Sequential()

model.add(Dense(input_dim=784,units=100))

model.add(Activation('sigmoid'))

model.add(Dense(units=100))

model.add(Activation('sigmoid'))

model.add(Dense(units=10))

model.add(Activation('softmax'))

model.compile(optimizer='adam',

loss='categorical_crossentropy',

metrics=['accuracy'])

model.fit(x_train, y_train,batch_size=32,epochs=10)

print("testing...")

loss, acc =model.evaluate(x_test, y_test)

print("\nloss: ", loss)

print(" acc: ", acc)

predit = model.predict(x_test[1].reshape(-1,784))

print("predit: ", predit)

print("x_test[1]: ", predit.argmax())

#保存模型,要保證根目錄下有mnist-model文件夾

model.save("mnist-model/minit_model.pb")

2.opencv打開(kāi)攝像

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')

def open_camera():

# fourcc = cv2.cv.CV_FOURCC(*'XVID')

# fourcc = cv2.VideoWriter_fourcc(*'XVID')

while True:

ret, frame = cap.read()

gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame', frame)#?

# cv2.imshow('gray', gray)? #?

ifcv2.waitKey(1) &0xFF==ord('q'):

print('quit')

break

if cv2.waitKey(1) &? 0xFF==ord('p'):

# img = cv2.resize(img, (28, 28))

# img = cv2.imread('9.png', cv2.IMREAD_GRAYSCALE).astype(np.float32)

# img = cv2.resize(frame, (28, 28), interpolation=cv2.INTER_CUBIC)

# timg = (img - (255 / 2.0)) / 255

img = cv2.resize(frame, (28,28))

3.加載模型

keras加載模型很簡(jiǎn)單

model = load_model('mnist-model/minit_model.pb')

4.以下是打開(kāi)攝像頭進(jìn)行識(shí)別的代碼,點(diǎn)擊鍵盤(pán)P進(jìn)行識(shí)別

import cv2

import numpy as np

from keras.models import Sequential, load_model

from PIL import Image, ImageFilter

cap = cv2.VideoCapture(0)

is_record =False

fourcc = cv2.VideoWriter_fourcc(*'XVID')

def open_camera():

# fourcc = cv2.cv.CV_FOURCC(*'XVID')

# fourcc = cv2.VideoWriter_fourcc(*'XVID')

while True:

ret, frame = cap.read()

gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame', frame)#

# cv2.imshow('gray', gray)? #?



ifcv2.waitKey(1) &0xFF==ord('q'):

print('quit')

break

if cv2.waitKey(1) &0xFF==ord('p'):

# img = cv2.resize(img, (28, 28))

# img = cv2.imread('9.png', cv2.IMREAD_GRAYSCALE).astype(np.float32)

# img = cv2.resize(frame, (28, 28), interpolation=cv2.INTER_CUBIC)

# timg = (img - (255 / 2.0)) / 255

img = cv2.resize(frame, (28,28))

take_photo(img)

array = imageprepare()

predit = model.predict(array)

print("num is ", predit.argmax())

if cv2.waitKey(1) &0xFF==ord('s'):

if is_record ==False:

out = cv2.VideoWriter('test.avi', fourcc,20.0, (640,480))#?

print('start record')

globalis_record

is_record =True

if is_record ==True:

start_record(frame, out)

def take_photo(frame):

cv2.imwrite("test.png", frame)

print('snap successful')

defstart_record(frame, out):

out.write(frame)#?

if cv2.waitKey(1) &0xFF==ord('e'):

if is_record ==True:

end_record(out)

defend_record(out):

out.release()

globalis_record

is_record =False

print('stop record')

defimageprepare():

img = Image.open('test.png').convert('L')

if img.size[0] !=28orimg.size[1] !=28:

img = img.resize((28,28))

arr = []

for i in range(28):

for j in range(28):

pixel =1.0-float(img.getpixel((j, i))) /255.0

arr.append(pixel)

return np.array(arr).reshape([-1,784])

if__name__ =='__main__':

model = load_model('mnist-model/minit_model.pb')

open_camera()

cap.release()

cv2.destroyAllWindows()

以上代碼就能實(shí)現(xiàn)動(dòng)態(tài)的識(shí)別手寫(xiě)字了,感覺(jué)簡(jiǎn)書(shū)對(duì)代碼太不友好了,排好版粘貼進(jìn)來(lái)全都連在以一起了,歡迎大家留言。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容