使用OpenCV和Tesseract库来实现
import cv2 import pytesseract def recognize_text(image): # 用Tesseract进行文字识别 text = pytesseract.image_to_string(image) return text def main(): # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取摄像头画面 ret, frame = cap.read() # 将画面转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 对图像进行二值化处理 _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) # 显示二值化后的图像 cv2.imshow('Binary Image', binary) # 调用文字识别函数 text = recognize_text(binary) # 打印识别到的文字 print(text) # 按下Esc键退出程序 if cv2.waitKey(1) == 27: break # 释放摄像头 cap.release() cv2.destroyAllWindows() if __name__ == '__main__': main()