Pycharm开发环境
python 版本 python3.7
Anconda 集成开发环境
Python 环境
pip install opencv-python
pip install baidu-aip
Anconda 环境
conda install opencv-python
conda install baidu-aip
from aip import AipOcr #pip install baidu-aip
config = {
'appId':'',
'apiKey':'',
'secretKey':''
}
# appId apiKey secretKey 三元组,添加自己的创建应用里面的
client = AipOcr(**config)
# 获取图像内容
def get_file_content(file):
with open(file,'rb') as f:
return f.read()
# 文字 to 字符
def img_to_str(image_path):
image = get_file_content(image_path)
result = client.handwriting(image)
# print(result)
if 'words_result' in result:
return '\n'.join([w['words'] for w in result['words_result']])
import cv2 # pip install openv-python
from Handwritten import img_to_str # 导入 img_to_str
if __name__ == '__main__':
# 创建一个窗口 1表示不能改变窗口大小
cv2.namedWindow("camera",1)
# 开启ip摄像头
# http://admin:[email protected]:8081/video
# 用户名/密码默认admin @ip地址 端口
video = 'http://admin:admin@IP地址:端口/video'
# 开启摄像头
capture = cv2.VideoCapture(video)
# 按键处理
while True:
success,img = capture.read()
cv2.imshow("camera",img)
# 按键处理
key = cv2.waitKey(10)
# esc 退出
if key == 27:
print("esc break")
break
# 空格 保存图片
if key ==32:
filename = "filename.png"
cv2.imwrite(filename,img)
s = img_to_str(filename)
print(s) #显示识别内容
# 释放摄像头
capture.release()
#关闭窗口
cv2.destroyWindow('camera')