分享

教你用Python 百度AI接口 摄像头实现文字精准识别

 老三的休闲书屋 2020-05-24

前几期在抖音上同名号上为粉丝们送了几期福利,在收集粉丝发来的邮件地址的时候,比较痛苦,上百上千的地址,然后再输入到邮箱发送内容,比较麻烦!所以有了以下的快捷方式:

实现目标

利用python+OPENCV,再结合上百度AI接口,当然另外还配合了USB摄像头,实现了批量对群内的地址进行采集,然后保存。今天我将全部源码分享给大家:

打开pycharm开发工具,在项目中新建 demo.py 文件,文件代码如下:

from aip import AipOcrimport cv2import cv2 as cvimport reimport timeimport numpy as np''' 你的 APPID AK SK '''APP_ID = '你的ID'API_KEY = '你的KEY'SECRET_KEY = '你的SECRET'aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read()# 信息分离,只留EMAIL部分def get_emails(text): emails = re.findall(r'[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+', text) if len(emails) > 0: return emailsdef baiduApi(images): # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } # 调用通用文字识别接口 time.sleep(1) result = aipOcr.basicGeneral(get_file_content(images), options) words_result=result['words_result'] filename = 'email_list.txt' for i in range(len(words_result)): email = get_emails(words_result[i]['words']) if email != None: print(email[0]) with open(filename,'a',encoding='utf-8') as f: f.writelines(email[0] + '\n') print('完成当前识别任务')# 打开摄像头拍照cap = cv2.VideoCapture(0)num = 0while True: ret,frame=cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 灰度处理 #显示在窗口上 cv2.imshow('NO.1', frame) kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) # 锐化 dst = cv.filter2D(frame, -1, kernel=kernel) cv.imshow('NO.2', dst) if cv2.waitKey(100) & 0xff == ord('s'): #key == 's': num += 1 print('识别图片 %s' % num) path = r'J:/python/kejian/img/' cv2.imwrite('{}{}{}'.format(path, num, '.jpg'), dst, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) # 保存图片,质量为100 baiduApi('{}{}{}'.format(path, num, '.jpg')) elif cv2.waitKey(100) & 0xff == ord('q'): breakcv2.destroyAllWindows()cap.release()

以上的目录大家自行调整为自己的目录即可。

识别结果如下:

教你用Python+百度AI接口+摄像头实现文字精准识别

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多