[视觉识别]OpenCV + CNN 大神符识别
数据集
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 大神符识别的更多相关文章
- OPENCV条形码检测与识别
条形码是当前超市和部分工厂使用比较普遍的物品,产品标识技术,使用摄像头检测一张图片的条形码包含有两个步骤,第一是定位条形码的位置,定位之后剪切出条形码,并且识别出条形码对应的字符串,然后就可以调用网络 ...
- Java基于opencv实现图像数字识别(五)—投影法分割字符
Java基于opencv实现图像数字识别(五)-投影法分割字符 水平投影法 1.水平投影法就是先用一个数组统计出图像每行黑色像素点的个数(二值化的图像): 2.选出一个最优的阀值,根据比这个阀值大或小 ...
- Java基于opencv实现图像数字识别(四)—图像降噪
Java基于opencv实现图像数字识别(四)-图像降噪 我们每一步的工作都是基于前一步的,我们先把我们前面的几个函数封装成一个工具类,以后我们所有的函数都基于这个工具类 这个工具类呢,就一个成员变量 ...
- 手把手教你使用LabVIEW OpenCV dnn实现物体识别(Object Detection)含源码
前言 今天和大家一起分享如何使用LabVIEW调用pb模型实现物体识别,本博客中使用的智能工具包可到主页置顶博客LabVIEW AI视觉工具包(非NI Vision)下载与安装教程中下载 一.物体识别 ...
- Java基于opencv实现图像数字识别(三)—灰度化和二值化
Java基于opencv实现图像数字识别(三)-灰度化和二值化 一.灰度化 灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值:因此,灰度图像每个像素点只需一个字 ...
- Java基于opencv实现图像数字识别(二)—基本流程
Java基于opencv实现图像数字识别(二)-基本流程 做一个项目之前呢,我们应该有一个总体把握,或者是进度条:来一步步的督促着我们来完成这个项目,在我们正式开始前呢,我们先讨论下流程. 我做的主要 ...
- Java基于opencv实现图像数字识别(一)
Java基于opencv实现图像数字识别(一) 最近分到了一个任务,要做数字识别,我分配到的任务是把数字一个个的分开:当时一脸懵逼,直接百度java如何分割图片中的数字,然后就百度到了用Buffere ...
- 利用CNN进行流量识别 本质上就是将流量视作一个图像
from:https://netsec2018.files.wordpress.com/2017/12/e6b7b1e5baa6e5ada6e4b9a0e59ca8e7bd91e7bb9ce5ae89 ...
- 【从零学习openCV】IOS7人脸识别实战
前言 接着上篇<IOS7下的人脸检測>,我们顺藤摸瓜的学习怎样在IOS7下用openCV的进行人脸识别,实际上非常easy,因为人脸检測部分已经完毕,剩下的无非调用openCV的方法对採集 ...
随机推荐
- Swoole 多协议 多端口 的应用
目录 概述 网络通信协议设计 多端口监听的使用 小结 概述 这是关于 Swoole 学习的第五篇文章:Swoole 多协议 多端口 的应用. 第四篇:Swoole HTTP 的应用 第三篇:Swool ...
- HyperLedger Fabric 多机部署(一)
本文参考:http://www.lijiaocn.com/%E9%A1%B9%E7%9B%AE/2018/04/26/hyperledger-fabric-deploy.html 学习. 1.准备工 ...
- (三)siege的使用
学习: ELK——http://dockone.io/article/3655 docker——http://www.testclass.net/docker/ Android Monkey压力测试— ...
- python——字符编码
Unicode 是字符集 UTF-8 是编码规则 Unicode:给每一个字符分配一个唯一的ID(又称码位). 编码规则:将码位转换为字节序列的规则. 1.什么是字符编码:字符翻译成数字,所遵循的标准 ...
- rand()函数的用法
C++中rand() 函数的用法 1.rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数. 2.如果你要产生0~99这100个整数中的一个随机整数, ...
- 9.数据分组 ---SQL
一.创建分组 分组是使用SELECT语句的GROUP BY子句建立的.理解分组的最好办法是看一个例子: SELECT vend_id, COUNT(*) AS num_prods FROM Produ ...
- codeforces540E-树状数组求逆序对
1-1e9的数据范围 但有1e5个区间 所以可以考虑把没有涉及到的区间缩成一个点 然后树状数组求逆序对 #include<bits/stdc++.h> #define inf 0x3f3f ...
- 如何在windows下是用mysqldumpslow命令
1. 再一次点击mysql安装文件(默认是没安装mysqldumpslow这些脚本的),如图: 点击next如下图 点击Developer Components 旁边的选择this feature , ...
- StringMVC
public class FirstController implements Controller { public ModelAndView handleRequest(HttpServletRe ...
- MyBatis配置文件之概述
MyBatis配置文件所有元素 <?xml version="1.0" encoding="UTF-8"?> <configuration&g ...