[视觉识别]OpenCV + CNN 大神符识别
阅读原文时间:2023年07月11日阅读:1

数据集

Mnist数据集:http://yann.lecun.com/exdb/mnist/

训练

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Activation,Conv2D,MaxPooling2D,Flatten,Dropout,BatchNormalization,ZeroPadding2D
from keras.optimizers import Adam
from keras import backend as K
K.backend()
import tensorflow as tf
import cv2 as cv
import pandas as pd
#加载数据
datas=pd.read_csv("mnist_train.csv")
images=datas.iloc[:,1:].values
x_image=images.astype(np.float)
x_image=np.multiply(x_image,1.0/255.0)
labels=datas.iloc[:,0].values
x_image[0]
labels[0]
#CNN模型加载
def loadCNN():
model = Sequential()
model.add(Conv2D(32,(3,3),padding="valid",input_shape=(28,28,1)))
convout1 = Activation("relu")
model.add(convout1)
model.add(BatchNormalization(epsilon=1e-6,axis=1))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(48,(3,3)))
convout2 = Activation("relu")
model.add(convout2)
model.add(BatchNormalization(epsilon=1e-6,axis=1))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,(2,2)))
convout3 = Activation("relu")
model.add(convout3)
model.add(BatchNormalization(epsilon=1e-6,axis=1))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(3168))
model.add(Activation("relu"))
model.add(Dense(10))
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=['accuracy'])
model.summary()
model.get_config()
return model
#训练加存储
from keras.utils import to_categorical
x_input = x_image.reshape([-1,28,28,1])
y_input = to_categorical(labels, num_classes=10)
print(y_input.shape)
model = loadCNN()
hist = model.fit(x_input,y_input,batch_size = 32,epochs=15,verbose=1, validation_split=0.2)
model.save_weights("model/mnist.hdf5",overwrite=True)

Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
activation_1 (Activation) (None, 26, 26, 32) 0
_________________________________________________________________
batch_normalization_1 (Batch (None, 26, 26, 32) 104
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32) 0
_________________________________________________________________
zero_padding2d_1 (ZeroPaddin (None, 15, 15, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 13, 13, 48) 13872
_________________________________________________________________
activation_2 (Activation) (None, 13, 13, 48) 0
_________________________________________________________________
batch_normalization_2 (Batch (None, 13, 13, 48) 52
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 6, 48) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 5, 5, 64) 12352
_________________________________________________________________
activation_3 (Activation) (None, 5, 5, 64) 0
_________________________________________________________________
batch_normalization_3 (Batch (None, 5, 5, 64) 20
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 2, 2, 64) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 2, 2, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 256) 0
_________________________________________________________________
dense_1 (Dense) (None, 3168) 814176
_________________________________________________________________
activation_4 (Activation) (None, 3168) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 31690
_________________________________________________________________
activation_5 (Activation) (None, 10) 0
=================================================================
Total params: 872,586
Trainable params: 872,498
Non-trainable params: 88
_________________________________________________________________
Train on 47999 samples, validate on 12000 samples
Epoch 1/15
47999/47999 [==============================] - 1641s 34ms/step - loss: 0.2381 - acc: 0.9250 - val_loss: 0.0724 - val_acc: 0.9762
Epoch 2/15
47999/47999 [==============================] - 1634s 34ms/step - loss: 0.1009 - acc: 0.9693 - val_loss: 0.0486 - val_acc: 0.9852
Epoch 3/15
47999/47999 [==============================] - 1626s 34ms/step - loss: 0.0787 - acc: 0.9768 - val_loss: 0.0472 - val_acc: 0.9863
Epoch 4/15
47999/47999 [==============================] - 1619s 34ms/step - loss: 0.0623 - acc: 0.9805 - val_loss: 0.0358 - val_acc: 0.9892
Epoch 5/15
47999/47999 [==============================] - 1627s 34ms/step - loss: 0.0568 - acc: 0.9829 - val_loss: 0.0427 - val_acc: 0.9878
Epoch 6/15
47999/47999 [==============================] - 1631s 34ms/step - loss: 0.0496 - acc: 0.9847 - val_loss: 0.0355 - val_acc: 0.9908
Epoch 7/15
47999/47999 [==============================] - 1626s 34ms/step - loss: 0.0430 - acc: 0.9871 - val_loss: 0.0284 - val_acc: 0.9922
Epoch 8/15
47999/47999 [==============================] - 1632s 34ms/step - loss: 0.0390 - acc: 0.9877 - val_loss: 0.0269 - val_acc: 0.9922
Epoch 9/15
47999/47999 [==============================] - 1632s 34ms/step - loss: 0.0363 - acc: 0.9886 - val_loss: 0.0341 - val_acc: 0.9904
Epoch 10/15
47999/47999 [==============================] - 1634s 34ms/step - loss: 0.0315 - acc: 0.9896 - val_loss: 0.0321 - val_acc: 0.9908
Epoch 11/15
47999/47999 [==============================] - 1626s 34ms/step - loss: 0.0301 - acc: 0.9907 - val_loss: 0.0325 - val_acc: 0.9912
Epoch 12/15
47999/47999 [==============================] - 1638s 34ms/step - loss: 0.0284 - acc: 0.9906 - val_loss: 0.0280 - val_acc: 0.9928
Epoch 13/15
47999/47999 [==============================] - 1635s 34ms/step - loss: 0.0261 - acc: 0.9920 - val_loss: 0.0313 - val_acc: 0.9919
Epoch 14/15
47999/47999 [==============================] - 1642s 34ms/step - loss: 0.0246 - acc: 0.9923 - val_loss: 0.0246 - val_acc: 0.9935
Epoch 15/15
47999/47999 [==============================] - 1639s 34ms/step - loss: 0.0228 - acc: 0.9926 - val_loss: 0.0288 - val_acc: 0.9922

测试

def test():
model = loadCNN()
model.load_weights("model/mnist.hdf5")
cap = cv.VideoCapture("test.wmv")
while(cap.isOpened()):
ret,frame = cap.read()
img = draw(model,frame)
cv.imshow(",img)
if cv.waitKey(1) == 27:
break
cap.release()
cv.destroyAllWindows()

def draw(model,img):
kernel = np.ones((3,3),np.uint8)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
lower_hsv = np.array([0,0,223])
upper_hsv = np.array([198,38,255])
ROI = cv.cvtColor(img,cv.COLOR_BGR2HSV)
frame = cv.inRange(ROI,lower_hsv,upper_hsv)
image,contours, hierarchy= cv.findContours(frame,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)
i=0
for contour in contours:
if len(contour) >3:
x,y,w,h = cv.boundingRect(contour)
if w/h>=1.35 and w>15 and h >20 and w <=45:
#print(x,y,w,h)
p_img = gray[y:y+h, x+w//2-h//2:x+w//2+h//2]
p_img = cv.erode(p_img,kernel)
p_img = cv.resize(p_img,(28,28),interpolation= cv.INTER_LINEAR)
ph,pw = p_img.shape
for hx in range(ph):
for wy in range(pw):
p_img[hx][wy] = 255- p_img[hx][wy]
p_img = np.array(p_img,'f')
p_img = p_img/ 255.0
p_img = p_img.reshape([-1,28,28,1])
pdt = np.argmax(model.predict(p_img))
cv.putText(img,str(pdt),(x,y),cv.FONT_HERSHEY_COMPLEX,0.8,(0,0,255),1)
####这里用来判断
#print(x,y,w,h)
#cv.rectangle(img, (x+ w//2-h//2,y), (x+w//2+h//2, y + h), (0, 0, 225), 3)
#cv.drawContours(img, contours, -1, (0, 0, 255), 3)
#print(i)
return img
test()

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章