当前位置:网站首页>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)
边栏推荐
- svg环绕地球动画js特效
- Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
- el-dialog拖拽,边界问题完全修正,网上版本的bug修复
- 音视频学习(一)——PTZ控制原理
- Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
- JS text scrolling scattered animation JS special effect
- Development, deployment and online process of NFT project (2)
- 3 keras版本模型训练
- Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
- A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
猜你喜欢
随机推荐
NFT 平台安全指南(1)
Selenium chrome disable JS disable pictures
[problem solving] the loading / downloading time of the new version of webots texture and other resource files is too long
9 Tensorboard的使用
HW safety response
OpenSea上如何创建自己的NFT(Polygon)
SQLite loads CSV files and performs data analysis
Golang 1.18 go work usage
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
golang 临时对象池优化
How to identify contractual issues
AbortController的使用
Solana capacity expansion mechanism analysis (1): an extreme attempt to sacrifice availability for efficiency | catchervc research
Why are encoder and decoder structures often used in image segmentation tasks?
如何辨别合约问题
A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
PCIe Capabilities List
手机上怎么开户?在线开户安全么?
golang 1.18 go work 使用
Svg savage animation code









