数据集

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()

[视觉识别]OpenCV + CNN 大神符识别的更多相关文章

  1. OPENCV条形码检测与识别

    条形码是当前超市和部分工厂使用比较普遍的物品,产品标识技术,使用摄像头检测一张图片的条形码包含有两个步骤,第一是定位条形码的位置,定位之后剪切出条形码,并且识别出条形码对应的字符串,然后就可以调用网络 ...

  2. Java基于opencv实现图像数字识别(五)—投影法分割字符

    Java基于opencv实现图像数字识别(五)-投影法分割字符 水平投影法 1.水平投影法就是先用一个数组统计出图像每行黑色像素点的个数(二值化的图像): 2.选出一个最优的阀值,根据比这个阀值大或小 ...

  3. Java基于opencv实现图像数字识别(四)—图像降噪

    Java基于opencv实现图像数字识别(四)-图像降噪 我们每一步的工作都是基于前一步的,我们先把我们前面的几个函数封装成一个工具类,以后我们所有的函数都基于这个工具类 这个工具类呢,就一个成员变量 ...

  4. 手把手教你使用LabVIEW OpenCV dnn实现物体识别(Object Detection)含源码

    前言 今天和大家一起分享如何使用LabVIEW调用pb模型实现物体识别,本博客中使用的智能工具包可到主页置顶博客LabVIEW AI视觉工具包(非NI Vision)下载与安装教程中下载 一.物体识别 ...

  5. Java基于opencv实现图像数字识别(三)—灰度化和二值化

    Java基于opencv实现图像数字识别(三)-灰度化和二值化 一.灰度化 灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值:因此,灰度图像每个像素点只需一个字 ...

  6. Java基于opencv实现图像数字识别(二)—基本流程

    Java基于opencv实现图像数字识别(二)-基本流程 做一个项目之前呢,我们应该有一个总体把握,或者是进度条:来一步步的督促着我们来完成这个项目,在我们正式开始前呢,我们先讨论下流程. 我做的主要 ...

  7. Java基于opencv实现图像数字识别(一)

    Java基于opencv实现图像数字识别(一) 最近分到了一个任务,要做数字识别,我分配到的任务是把数字一个个的分开:当时一脸懵逼,直接百度java如何分割图片中的数字,然后就百度到了用Buffere ...

  8. 利用CNN进行流量识别 本质上就是将流量视作一个图像

    from:https://netsec2018.files.wordpress.com/2017/12/e6b7b1e5baa6e5ada6e4b9a0e59ca8e7bd91e7bb9ce5ae89 ...

  9. 【从零学习openCV】IOS7人脸识别实战

    前言 接着上篇<IOS7下的人脸检測>,我们顺藤摸瓜的学习怎样在IOS7下用openCV的进行人脸识别,实际上非常easy,因为人脸检測部分已经完毕,剩下的无非调用openCV的方法对採集 ...

随机推荐

  1. 无监督学习:Deep Generative Mode(深度生成模型)

    一 前言 1.1 Creation 据说在费曼死后,人们在他生前的黑板上拍到如图画片,在左上角有道:What i cannot create ,I do not understand. Generat ...

  2. PHP SOAP 提交XML

    <?php $xmldata = <<<EOT <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap. ...

  3. CentOS 6.5 安装Clang 3.5.0

    来自引用: http://www.cnblogs.com/dudu/p/4294374.html 编译llvm几乎耗费了1个小时-.. 编译CoreCLR需要Clang 3.5,而CentOS上安装的 ...

  4. 洛谷 P2895 [USACO08FEB]流星雨Meteor Shower 解题报告

    一起来看流星雨吧(话说我还没看到过流星雨呢) 题目 Problem 小A则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一 ...

  5. php追加数组的问题

    PHP数组合并两种方法及区别 如果是关联数组,如下: 代码如下: $a = array( 'where' => 'uid=1', 'order' => 'uid', ); $b = arr ...

  6. 一个线性表中的元素为整数,设计一个算法,将正整数和负整数分开,使线性表的前一半为负整数,后一半为正整数。(C语言)

    以下为完整可运行示例代码: #include <stdio.h> #include <stdlib.h> typedef struct LNode{ int data; str ...

  7. Codeforces 1114F(欧拉函数、线段树)

    AC通道 要点 欧拉函数对于素数有一些性质,考虑将输入数据唯一分解后进行素数下的处理. 对于素数\(p\)有:\(\phi(p^k)=p^{k-1}*(p-1)=p^k*\frac{p-1}{p}\) ...

  8. 51nod1222最小公倍数计数

    51nod1222 http://210.33.19.103/contest/1113/problem/2 同学的神仙做法: 首先考虑先去掉X<=Y的限制,也就是先计算满足要求的任意有序pair ...

  9. 练习三十二:用python实现:按相反的顺序输出列表的每一位值

    用python实现:按相反的顺序输出列表的每一位值 1. 使用list[::-1] list1 = ["one","two","three" ...

  10. 去掉word文档两边的空白

    1.设置-页面布局-页边距,把左边距和右边距的数据设置到最小就好,一般为0.43CM 2.把WORD页面顶部标尺,左右拉到最底,如图: 3.在打印预览里,设置页边距,操作方法同 上述 1,如图: