当前位置:网站首页>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)
边栏推荐
- NFT 平台安全指南(1)
- Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
- 手写数字体识别,用保存的模型跑自己的图片
- Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
- Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
- 如何配置使用新的单线激光雷达
- IntelliJ idea -- Method for formatting SQL files
- Everyone is a scientist free gas experience Mint love crash
- Stepn novice introduction and advanced
- 学习内存屏障
猜你喜欢

svg环绕地球动画js特效

NFT交易原理分析(1)

SQLite loads CSV files and performs data analysis

Evaluation - TOPSIS

Tweenmax+svg switch color animation scene
![[problem solving] the loading / downloading time of the new version of webots texture and other resource files is too long](/img/31/d14316dca740590c1871efe6587e04.png)
[problem solving] the loading / downloading time of the new version of webots texture and other resource files is too long

【微信小程序】事件绑定,你搞懂了吗?

svg上升的彩色气泡动画

Nanopi duo2 connection WiFi

如何辨别合约问题
随机推荐
NFT Platform Security Guide (1)
Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
SQLite loads CSV files and performs data analysis
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
8 user defined evaluation function
Svg savage animation code
svg野人动画代码
el-dialog拖拽,边界问题完全修正,网上版本的bug修复
人人都当科学家之免Gas体验mint爱死机
NFT 项目的开发、部署、上线的流程(1)
Binding method of multiple sub control signal slots under QT
Selenium saves elements as pictures
音视频学习(二)——帧率、码流和分辨率
JVM笔记
What is the difference between stm32f1 and gd32f1?
H5 close the current page, including wechat browser (with source code)
Nanopi duo2 connection WiFi
Analyse panoramique de la chaîne industrielle en amont, en aval et en aval de la NFT « Dry goods»
评价——模糊综合评价
[C language practice - printing hollow upper triangle and its deformation]