提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
程序参考这位大神
但是这篇文章里只有大致思路没有对程序的具体讲解,另外也有一些小bug,完善后的程序放在这实时手写数字识别。具体程序思路可以看注释,注释非常完善。
先修改需要输入视频文件的路径
# 主程序
knn, train, trainLabel = readinit() # 前两句根据需要选择加载训练好的knn或者是用MINIST重新训练knn
#knn, train, trainLabel = init()
knn, train, trainLabel = updateKnn(knn, train, trainLabel)
# 输入需要处理的视频文件路径或者是摄像头,笔记本内置摄像头0,外置1
cap = cv2.VideoCapture(r'./test2.mp4') # 改为自己需要处理的视频文件名
width = 426*2
height = 480
videoFrame = cv2.VideoWriter('frame.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 25, (int(width), int(height)), True) # 将处理结果保存成视频
count = 0
while(cap.isOpened()):
ret, frame = cap.read()
# frame = frame[:,:426]
# 读取每一帧画面
rois = []
# 转为灰度图
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 进行形态学膨胀和腐蚀,然后通过cv2.adsdiff(A, B)两幅图像作差,找到边
gray2 = cv2.dilate(gray, None, iterations=2)
gray2 = cv2.erode(gray2, None, iterations=2)
edges = cv2.absdiff(gray, gray2)
# 运用Sobel算子边缘检测
x = cv2.Sobel(edges, cv2.CV_16S, 1, 0)
y = cv2.Sobel(edges, cv2.CV_16S, 0, 1)
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
# 设置一个阈值来对图像进行二值化处理,阈值为50
ret, dst = cv2.threshold(dst, 50, 255, cv2.THRESH_BINARY)
cv2.imshow('dst', dst)
# 找图片的轮廓
contours, hierarchy = cv2.findContours(dst, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
x, y, w, h = cv2.boundingRect(c)
if w > 10 and h > 20: # 满足width 和 height 条件的为轮廓
rois.append((x, y, w, h))
digits = []
for r in rois:
x, y, w, h = r
digit, th = shibie_num(knn, edges[y:y+h, x:x+w], 50) # th为resize到20x20的图,digit为knn结果
digits.append(cv2.resize(th, (20,20)))
# 用矩形画出这个识别数字再写出这个识别数字
cv2.rectangle(frame, (x,y), (x+w,y+h), (200,200,0), 2) # 绘制矩形,(200,200,0)是颜色,2是线条类型
cv2.putText(frame, str(digit), (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (127,0,255), 2) # 结果标在矩形上
print(str(digit))
newEdges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
newFrame = np.hstack((frame, newEdges)) # 将两个画面拼接成newframe
cv2.imshow('frame', newFrame)
videoFrame.write(newFrame) # 保存成视频
key = cv2.waitKey(1) & 0xff # 输入按键,s保存,u手动训练
if key == ord(' '):
break
# 按s保存当前的数据
elif key == ord('s'):
np.savez('data.npz', train=train, trainLabel=trainLabel)
print('保存数据成功')
elif key == ord('u'):
Nd = len(digits)
output = np.zeros(20 * 20 * Nd).reshape(-1, 20)
for i in range(Nd):
output[20 * i:20 * (i + 1), :] = digits[i]
print(output)
showDigits = cv2.resize(output, (60, 60 * Nd))
cv2.imshow('digits', showDigits) # 显示需要手动训练的数字,从上到下
# cv2.imwrite(str(count)+'.png', showDigits)
# cv2.imwrite(r'C:\Users\win\Desktop\test\test3.JPG', frame)
count += 1
if cv2.waitKey(0) & 0xff == ord('e'):
pass
print('输入{}个数字并以空格分开:'.format(Nd))
numbers = input().split(' ')
Nn = len(numbers)
print(Nn)
if Nd != Nn: # 输入数字个数与需要训练的数字个数不等
print('update fail!')
continue
try:
for i in range(Nn):
numbers[i] = int(numbers[i])
except:
continue
knn, train, trainLabel = updateKnn(knn, train, trainLabel, output, numbers) # 更新knn
print('update Done!')
if cv2.waitKey(1) & 0xFF == ord('q'): # 按q退出
break
cap.release()
cv2.destroyAllWindows()
按u进行可以手动训练,再按enter从上到下输入digits对应的数字,并以空格分开,再按s可以保存新的knn,手动训练两次后,可以达到稳定状态