1. 程式人生 > >OpenCV + CNN 大神符識別

OpenCV + CNN 大神符識別

rpo cat test oca pool output ros label entropy

數據集

Mnist數據集:http://yann.lecun.com/exdb/mnist/

訓練

 1 import numpy as np
 2 from keras.datasets import mnist
 3 from keras.models import Sequential
 4 from keras.layers import Dense,Activation,Conv2D,MaxPooling2D,Flatten,Dropout,BatchNormalization,ZeroPadding2D
 5 from keras.optimizers import Adam
 6 from
keras import backend as K 7 K.backend() 8 import tensorflow as tf 9 import cv2 as cv 10 import pandas as pd 11 #加載數據 12 datas=pd.read_csv("mnist_train.csv") 13 images=datas.iloc[:,1:].values 14 x_image=images.astype(np.float) 15 x_image=np.multiply(x_image,1.0/255.0) 16 labels=datas.iloc[:,0].values
17 x_image[0] 18 labels[0] 19 #CNN模型加載 20 def loadCNN(): 21 model = Sequential() 22 model.add(Conv2D(32,(3,3),padding="valid",input_shape=(28,28,1))) 23 convout1 = Activation("relu") 24 model.add(convout1) 25 model.add(BatchNormalization(epsilon=1e-6,axis=1)) 26 model.add(MaxPooling2D(pool_size=(2,2)))
27 model.add(ZeroPadding2D((1,1))) 28 model.add(Conv2D(48,(3,3))) 29 convout2 = Activation("relu") 30 model.add(convout2) 31 model.add(BatchNormalization(epsilon=1e-6,axis=1)) 32 model.add(MaxPooling2D(pool_size=(2,2))) 33 model.add(Conv2D(64,(2,2))) 34 convout3 = Activation("relu") 35 model.add(convout3) 36 model.add(BatchNormalization(epsilon=1e-6,axis=1)) 37 model.add(MaxPooling2D(pool_size=(2,2))) 38 model.add(Dropout(0.5)) 39 model.add(Flatten()) 40 model.add(Dense(3168)) 41 model.add(Activation("relu")) 42 model.add(Dense(10)) 43 model.add(Activation("softmax")) 44 model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=[accuracy]) 45 model.summary() 46 model.get_config() 47 return model 48 #訓練加存儲 49 from keras.utils import to_categorical 50 x_input = x_image.reshape([-1,28,28,1]) 51 y_input = to_categorical(labels, num_classes=10) 52 print(y_input.shape) 53 model = loadCNN() 54 hist = model.fit(x_input,y_input,batch_size = 32,epochs=15,verbose=1, validation_split=0.2) 55 model.save_weights("model/mnist.hdf5",overwrite=True)
 1 Layer (type)                 Output Shape              Param #   
 2 =================================================================
 3 conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
 4 _________________________________________________________________
 5 activation_1 (Activation)    (None, 26, 26, 32)        0         
 6 _________________________________________________________________
 7 batch_normalization_1 (Batch (None, 26, 26, 32)        104       
 8 _________________________________________________________________
 9 max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
10 _________________________________________________________________
11 zero_padding2d_1 (ZeroPaddin (None, 15, 15, 32)        0         
12 _________________________________________________________________
13 conv2d_2 (Conv2D)            (None, 13, 13, 48)        13872     
14 _________________________________________________________________
15 activation_2 (Activation)    (None, 13, 13, 48)        0         
16 _________________________________________________________________
17 batch_normalization_2 (Batch (None, 13, 13, 48)        52        
18 _________________________________________________________________
19 max_pooling2d_2 (MaxPooling2 (None, 6, 6, 48)          0         
20 _________________________________________________________________
21 conv2d_3 (Conv2D)            (None, 5, 5, 64)          12352     
22 _________________________________________________________________
23 activation_3 (Activation)    (None, 5, 5, 64)          0         
24 _________________________________________________________________
25 batch_normalization_3 (Batch (None, 5, 5, 64)          20        
26 _________________________________________________________________
27 max_pooling2d_3 (MaxPooling2 (None, 2, 2, 64)          0         
28 _________________________________________________________________
29 dropout_1 (Dropout)          (None, 2, 2, 64)          0         
30 _________________________________________________________________
31 flatten_1 (Flatten)          (None, 256)               0         
32 _________________________________________________________________
33 dense_1 (Dense)              (None, 3168)              814176    
34 _________________________________________________________________
35 activation_4 (Activation)    (None, 3168)              0         
36 _________________________________________________________________
37 dense_2 (Dense)              (None, 10)                31690     
38 _________________________________________________________________
39 activation_5 (Activation)    (None, 10)                0         
40 =================================================================
41 Total params: 872,586
42 Trainable params: 872,498
43 Non-trainable params: 88
44 _________________________________________________________________
45 Train on 47999 samples, validate on 12000 samples
46 Epoch 1/15
47 47999/47999 [==============================] - 1641s 34ms/step - loss: 0.2381 - acc: 0.9250 - val_loss: 0.0724 - val_acc: 0.9762
48 Epoch 2/15
49 47999/47999 [==============================] - 1634s 34ms/step - loss: 0.1009 - acc: 0.9693 - val_loss: 0.0486 - val_acc: 0.9852
50 Epoch 3/15
51 47999/47999 [==============================] - 1626s 34ms/step - loss: 0.0787 - acc: 0.9768 - val_loss: 0.0472 - val_acc: 0.9863
52 Epoch 4/15
53 47999/47999 [==============================] - 1619s 34ms/step - loss: 0.0623 - acc: 0.9805 - val_loss: 0.0358 - val_acc: 0.9892
54 Epoch 5/15
55 47999/47999 [==============================] - 1627s 34ms/step - loss: 0.0568 - acc: 0.9829 - val_loss: 0.0427 - val_acc: 0.9878
56 Epoch 6/15
57 47999/47999 [==============================] - 1631s 34ms/step - loss: 0.0496 - acc: 0.9847 - val_loss: 0.0355 - val_acc: 0.9908
58 Epoch 7/15
59 47999/47999 [==============================] - 1626s 34ms/step - loss: 0.0430 - acc: 0.9871 - val_loss: 0.0284 - val_acc: 0.9922
60 Epoch 8/15
61 47999/47999 [==============================] - 1632s 34ms/step - loss: 0.0390 - acc: 0.9877 - val_loss: 0.0269 - val_acc: 0.9922
62 Epoch 9/15
63 47999/47999 [==============================] - 1632s 34ms/step - loss: 0.0363 - acc: 0.9886 - val_loss: 0.0341 - val_acc: 0.9904
64 Epoch 10/15
65 47999/47999 [==============================] - 1634s 34ms/step - loss: 0.0315 - acc: 0.9896 - val_loss: 0.0321 - val_acc: 0.9908
66 Epoch 11/15
67 47999/47999 [==============================] - 1626s 34ms/step - loss: 0.0301 - acc: 0.9907 - val_loss: 0.0325 - val_acc: 0.9912
68 Epoch 12/15
69 47999/47999 [==============================] - 1638s 34ms/step - loss: 0.0284 - acc: 0.9906 - val_loss: 0.0280 - val_acc: 0.9928
70 Epoch 13/15
71 47999/47999 [==============================] - 1635s 34ms/step - loss: 0.0261 - acc: 0.9920 - val_loss: 0.0313 - val_acc: 0.9919
72 Epoch 14/15
73 47999/47999 [==============================] - 1642s 34ms/step - loss: 0.0246 - acc: 0.9923 - val_loss: 0.0246 - val_acc: 0.9935
74 Epoch 15/15
75 47999/47999 [==============================] - 1639s 34ms/step - loss: 0.0228 - acc: 0.9926 - val_loss: 0.0288 - val_acc: 0.9922

測試

 1 def test():
 2     model = loadCNN()
 3     model.load_weights("model/mnist.hdf5")
 4     cap = cv.VideoCapture("test.wmv")
 5     while(cap.isOpened()):
 6         ret,frame = cap.read()
 7         img = draw(model,frame)
 8         cv.imshow("2",img)
 9         if cv.waitKey(1) == 27:
10             break
11     cap.release()
12     cv.destroyAllWindows()
13 
14     
15 def draw(model,img):
16     kernel = np.ones((3,3),np.uint8)
17     gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
18     lower_hsv = np.array([0,0,223])
19     upper_hsv = np.array([198,38,255])
20     ROI = cv.cvtColor(img,cv.COLOR_BGR2HSV)
21     frame = cv.inRange(ROI,lower_hsv,upper_hsv)
22     image,contours, hierarchy= cv.findContours(frame,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)
23     i=0
24     for contour in contours:
25         if len(contour) >3:
26             x,y,w,h = cv.boundingRect(contour)
27             if w/h>=1.35 and w>15 and h >20 and w <=45:
28                 #print(x,y,w,h)
29                 p_img = gray[y:y+h, x+w//2-h//2:x+w//2+h//2]
30                 p_img = cv.erode(p_img,kernel)
31                 p_img = cv.resize(p_img,(28,28),interpolation= cv.INTER_LINEAR)
32                 ph,pw = p_img.shape
33                 for hx in range(ph):
34                     for wy in range(pw):
35                         p_img[hx][wy] = 255- p_img[hx][wy]
36                 p_img = np.array(p_img,f)
37                 p_img = p_img/ 255.0
38                 p_img = p_img.reshape([-1,28,28,1])
39                 pdt = np.argmax(model.predict(p_img))
40                 cv.putText(img,str(pdt),(x,y),cv.FONT_HERSHEY_COMPLEX,0.8,(0,0,255),1)
41                 ####這裏用來判斷
42                 #print(x,y,w,h)
43                 #cv.rectangle(img, (x+ w//2-h//2,y), (x+w//2+h//2, y + h), (0, 0, 225), 3)
44     #cv.drawContours(img, contours, -1, (0, 0, 255), 3)
45     #print(i)
46     return img
47 test()

技術分享圖片

OpenCV + CNN 大神符識別