当前位置:网站首页>Handwritten numeral recognition, run your own picture with the saved model
Handwritten numeral recognition, run your own picture with the saved model
2022-06-26 15:58:00 【X1996_】
Original blog :https://blog.csdn.net/X_m_w/article/details/101056156
model training :https://blog.csdn.net/X1996_/article/details/108883710
Here, we use the training model to predict , Refer to the previous article for model training
Mainly used opencv Process the image into the desired image format , Then predict
Input picture :
Output :
import cv2
import numpy as np
from keras import models
# Inverse grayscale image , Invert the black and white threshold
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
# Inverse binary image
def accessBinary(img, threshold=128):
img = accessPiexl(img)
# Edge expansion , It's OK to
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
# Use the contour to find the position , No line alignment is required
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:
# Fit edges into a border
x, y, w, h = cv2.boundingRect(contour)
if w*h > maxArea:
border = [(x, y), (x+w, y+h)]
borders.append(border)
return borders
# Convert to... According to the border MNIST Format
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]]
# Expand pixels according to the maximum edge , Expand the edge border , Fixed value 0 fill , Into a square
# top, bottom, left, right: The number of pixels to be expanded up, down, left and right
extendPiexl = (max(borderImg.shape) - min(borderImg.shape)) // 2
# How much to enlarge depends on the size of your original figure , Put the number in the middle of the picture , The bigger the picture , The expansion is larger
targetImg = cv2.copyMakeBorder(borderImg, 30, 30, extendPiexl + 30, extendPiexl + 30, cv2.BORDER_CONSTANT)
targetImg = cv2.resize(targetImg, size)
# Add a dimension 28*28*1, That's how I understand it , amount to reshape Well
targetImg = np.expand_dims(targetImg, axis=-1)
imgData[i] = targetImg
return imgData
# Predict handwritten digits
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
# Display results and borders
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)
边栏推荐
- 4 自定义模型训练
- Don't remove custom line breaks on reformat
- 【leetcode】331. 验证二叉树的前序序列化
- 音视频学习(三)——sip协议
- Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
- NFT contract basic knowledge explanation
- 7 自定义损失函数
- 9 Tensorboard的使用
- Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
- Svg canvas canvas drag
猜你喜欢

Transformation of zero knowledge QAP problem

Reflection modification final

Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT

还存在过有键盘的kindle?

Super double efficiency! Pycharm ten tips

Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research

音视频学习(三)——sip协议

svg上升的彩色气泡动画

Solana capacity expansion mechanism analysis (1): an extreme attempt to sacrifice availability for efficiency | catchervc research

全面解析Discord安全问题
随机推荐
Selenium saves elements as pictures
Stepn novice introduction and advanced
2022 Beijing Shijingshan District specializes in the application process for special new small and medium-sized enterprises, with a subsidy of 100000-200000 yuan
PCIe Capabilities List
I want to know how to open an account through online stock? Is online account opening safe?
Reflection modification final
Seurat to h5ad summary
golang 1.18 go work 使用
JVM笔记
【leetcode】48.旋转图像
NFT transaction principle analysis (2)
全面解析Discord安全问题
svg canvas画布拖拽
JS text scrolling scattered animation JS special effect
【leetcode】701. 二叉搜索树中的插入操作
NFT transaction principle analysis (1)
简单科普Ethereum的Transaction Input Data
Application of ansible automation
反射修改final
el-dialog拖拽,边界问题完全修正,网上版本的bug修复