树莓派打开摄像头识别纸上的字符串并显示在屏幕上不用Keras库和pytesseract库详细代码及过程
- 首先,需要安装OpenCV库和PIL库。在终端中执行以下命令:
sudo apt-get install python-opencv
sudo apt-get install python-pil
- 接着,需要启用树莓派的摄像头。在终端中执行以下命令:
sudo raspi-config
然后在菜单中选择“Interfacing Options” -> “Camera” -> “Yes” -> “Finish” -> “Reboot”。
- 编写Python代码。首先,导入必要的库:
import cv2
import numpy as np
from PIL import Image
- 初始化摄像头并捕获帧:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
- 对帧进行处理,转换成灰度图像并进行二值化处理:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY_INV)
- 对二值化后的图像进行膨胀处理:
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(binary,kernel,iterations = 1)
- 对膨胀后的图像进行轮廓检测:
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- 遍历所有轮廓,找到包含字符串的轮廓:
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if w > 50 and h > 10:
roi = binary[y:y+h, x:x+w]
img = Image.fromarray(roi)
text = pytesseract.image_to_string(img, lang='eng', config='--psm 10')
print(text)
- 最后,释放摄像头并关闭窗口:
cap.release()
cv2.destroyAllWindows()
完整代码如下:
import cv2
import numpy as np
from PIL import Image
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY_INV)
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(binary,kernel,iterations = 1)
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if w > 50 and h > 10:
roi = binary[y:y+h, x:x+w]
img = Image.fromarray(roi)
text = pytesseract.image_to_string(img, lang='eng', config='--psm 10')
print(text)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
需要注意的是,这段代码中的字符串识别部分使用了pytesseract库,但是题目要求不使用该库。如果要完全不依赖第三方库完成字符串识别,可以使用OCR算法实现。具体实现方法可以参考以下链接:https://github.com/xiaotaw/OCR-Practice
原文地址: https://www.cveoy.top/t/topic/euOQ 著作权归作者所有。请勿转载和采集!