当前位置:网站首页>手写数字体识别,用保存的模型跑自己的图片
手写数字体识别,用保存的模型跑自己的图片
2022-06-26 15:30:00 【X1996_】
原文博客:https://blog.csdn.net/X_m_w/article/details/101056156
模型训练:https://blog.csdn.net/X1996_/article/details/108883710
这里是用训练的模型直接来预测,模型训练可参考上篇文章
主要是用opencv把图片处理成需要的图片格式,然后预测
输入图片:
输出:
import cv2
import numpy as np
from keras import models
# 反相灰度图,将黑白阈值颠倒
def accessPiexl(img):
height = img.shape[0]
width = img.shape[1]
for i in range(height):
for j in range(width):
img[i][j] = 255 - img[i][j]
return img
# 反相二值化图像
def accessBinary(img, threshold=128):
img = accessPiexl(img)
# 边缘膨胀,不加也可以
kernel = np.ones((3, 3), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
_, img = cv2.threshold(img, threshold, 0, cv2.THRESH_TOZERO)
return img
# 利用轮廓找位置,不需要行对齐
def findBorderContours(path, maxArea=50):
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = accessBinary(img)
_, contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
borders = []
for contour in contours:
# 将边缘拟合成一个边框
x, y, w, h = cv2.boundingRect(contour)
if w*h > maxArea:
border = [(x, y), (x+w, y+h)]
borders.append(border)
return borders
# 根据边框转换为MNIST格式
def transMNIST(path, borders, size=(28, 28)):
imgData = np.zeros((len(borders), size[0], size[0], 1), dtype='uint8')
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = accessBinary(img)
for i, border in enumerate(borders):
borderImg = img[border[0][1]:border[1][1], border[0][0]:border[1][0]]
# 根据最大边缘拓展像素,扩大边缘边框,固定值0填充,变成正方形
# top, bottom, left, right:上下左右要扩展的像素数
extendPiexl = (max(borderImg.shape) - min(borderImg.shape)) // 2
# 扩大多少由你的原图数字大小决定,使数字处在图片中间,图片越大,扩大的就大一些
targetImg = cv2.copyMakeBorder(borderImg, 30, 30, extendPiexl + 30, extendPiexl + 30, cv2.BORDER_CONSTANT)
targetImg = cv2.resize(targetImg, size)
# 增加一个维度 28*28*1,我是这样理解的,相当于reshape吧
targetImg = np.expand_dims(targetImg, axis=-1)
imgData[i] = targetImg
return imgData
# 预测手写数字
def predict(modelpath, imgData):
model = models.load_model(modelpath)
img = imgData.astype('float32') / 255
results = model.predict(img)
result_number = []
for result in results:
result_number.append(np.argmax(result))
return result_number
# 显示结果及边框
def showResults(path, borders, results=None):
img = cv2.imread(path)
for i, border in enumerate(borders):
cv2.rectangle(img, border[0], border[1], (0, 0, 255))
if results:
cv2.putText(img, str(results[i]), border[0], cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 1)
cv2.imshow('test', img)
path = '11.png'
model = 'model.h5'
borders = findBorderContours(path)
imgData = transMNIST(path, borders)
results = predict(model, imgData)
showResults(path, borders, results)
cv2.waitKey(0)
边栏推荐
- JVM笔记
- Super double efficiency! Pycharm ten tips
- Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
- 【leetcode】48. Rotate image
- How to handle 2gcsv files that cannot be opened? Use byzer
- Solana capacity expansion mechanism analysis (1): an extreme attempt to sacrifice availability for efficiency | catchervc research
- TweenMax+SVG切换颜色动画场景
- Svg animation around the earth JS special effects
- On which platform is it safe to buy shares and open an account? Ask for guidance
- Is it safe to buy stocks and open accounts through the QR code of the securities manager? Want to open an account for stock trading
猜你喜欢

如何辨别合约问题

NFT 平台安全指南(1)

svg野人动画代码

Using restcloud ETL shell component to schedule dataX offline tasks

2Gcsv文件打不开怎么处理,使用byzer工具

Binding method of multiple sub control signal slots under QT

音视频学习(一)——PTZ控制原理
![[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing](/img/b6/d288065747425863b9af95ec6fd554.png)
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing

AbortController的使用

Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
随机推荐
SVG大写字母A动画js特效
How do I open an account on my mobile phone? Is online account opening safe?
【微信小程序】事件绑定,你搞懂了吗?
反射修改final
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
2Gcsv文件打不开怎么处理,使用byzer工具
NFT transaction principle analysis (1)
【C语言练习——打印空心上三角及其变形】
「干货」NFT 上中下游产业链全景分析
5000 word analysis: the way of container security attack and defense in actual combat scenarios
Nanopi duo2 connection WiFi
TweenMax+SVG切换颜色动画场景
JS之简易deepCopy(简介递归)
一篇博客彻底掌握:粒子滤波 particle filter (PF) 的理论及实践(matlab版)
How to configure and use the new single line lidar
JS creative icon navigation menu switch background color
9 Tensorboard的使用
NFT交易原理分析(2)
AbortController的使用
Reflection modification final