当前位置:网站首页>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)
边栏推荐
- 有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
- Is it safe to open an account for mobile stock registration? Is there any risk?
- 9 Tensorboard的使用
- Selenium saves elements as pictures
- Use of abortcontroller
- 【leetcode】331. 验证二叉树的前序序列化
- Development, deployment and online process of NFT project (1)
- High frequency interview 𞓜 Flink Shuangliu join
- Svg canvas canvas drag
- Interview pit summary I
猜你喜欢

手写数字体识别,用保存的模型跑自己的图片

11 cnn简介

SVG大写字母A动画js特效

HW安全响应

Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)

Comprehensive analysis of discord security issues

9 use of tensorboard

Development, deployment and online process of NFT project (2)

NFT 项目的开发、部署、上线的流程(2)

svg野人动画代码
随机推荐
Evaluate:huggingface detailed introduction to the evaluation index module
【微信小程序】事件绑定,你搞懂了吗?
Don't remove custom line breaks on reformat
selenium将元素保存为图片
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
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
2 三种建模方式
Svg savage animation code
C语言读取数据
js创意图标导航菜单切换背景色
9 use of tensorboard
Svg rising Color Bubble animation
音视频学习(一)——PTZ控制原理
在哪个平台买股票开户安全?求指导
5 模型保存与加载
「幹貨」NFT 上中下遊產業鏈全景分析
OpenSea上如何创建自己的NFT(Polygon)
Summary of students' learning career (2022)
1 张量的简单使用
全面解析Discord安全问题