基于opencv和knn的摄像头数字识别简单代码
时间: 2023-11-04 10:17:36 浏览: 121
可以提供以下的参考代码:
```python
import cv2
import numpy as np
# 读取训练数据
with np.load('datasets/mnist.npz') as data:
train_samples = data['train_images']
train_labels = data['train_labels']
# 创建KNN算法对象并训练
knn = cv2.ml.KNearest_create()
knn.train(train_samples, cv2.ml.ROW_SAMPLE, train_labels)
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取摄像头图像
ret, frame = cap.read()
# 转换为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 进行二值化处理
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 查找轮廓并获取数字图像
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
digit = thresh[y:y+h, x:x+w]
digit_resized = cv2.resize(digit, (28, 28), interpolation=cv2.INTER_AREA)
digit_vector = digit_resized.reshape((1, 28*28)).astype(np.float32)
# 使用KNN算法进行预测
_, result, _, _ = knn.findNearest(digit_vector, k=1)
digit_prediction = int(result[0, 0])
# 在图像上绘制结果
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, str(digit_prediction), (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 显示图像并等待退出
cv2.imshow('digit recognition', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
上述代码可以实现从摄像头读取视频帧,通过KNN算法识别数字,并在图像上显示识别结果。具体方法是使用OpenCV的cv2.ml.KNearest_create()创建一个KNN分类器,然后使用train()方法进行训练,接着读取摄像头帧,将其转换为灰度图像并进行二值化处理,进而找到图像中数字的轮廓,将数字提取出并调整大小,最后使用训练好的KNN模型进行数字识别,将结果显示在图像上。
阅读全文