import sys
import numpy as np
import cv2
im = cv2.imread('train.png')
im3 = im.copy()
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
################# Now finding Contours ###################
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
samples = np.empty((0,100))
responses = []
keys = [i for i in range(44,58)]
for cnt in contours:
# if cv2.contourArea(cnt)>5: #大于像素点的区域
[x,y,w,h] = cv2.boundingRect(cnt)
# print([x,y,w,h])#对应的区域的坐标
# if (h>25 and h < 29):#筛选不需要的区域
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
roi = thresh[y:y+h,x:x+w]
roismall = cv2.resize(roi,(10,10))
cv2.imshow('norm',im)
key = cv2.waitKey(0)
print(key)
if key == 27: # (escape to quit)
sys.exit()
elif key in keys:
key = str(key)
print(key)
responses.append(int(key))#保存ascii码
sample = roismall.reshape((1,100))
samples = np.append(samples,sample,0)
responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print ("training complete")
print(samples)
print(responses)
np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)
没有合适的资源?快使用搜索试试~ 我知道了~
python数字识别-训练模型 代码+训练需要的图片
共2个文件
py:1个
png:1个
10 下载量 21 浏览量
2023-08-03
14:14:09
上传
评论
收藏 12KB ZIP 举报
温馨提示
python数字识别-训练模型 代码+训练需要的图片
资源推荐
资源详情
资源评论
收起资源包目录
归档.zip (2个子文件)
train.png 12KB
training.py 2KB
共 2 条
- 1
资源评论
时尚IT男
- 粉丝: 3517
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功