图像识别的小程序。它能做的是将写在纸上的文字,精准地识别出来。如下图。
代码如下:
import requests
from aip import AipOcr
image = requests.get('https://static.pandateacher.com/7b5d6d8d9dea5691705d04fef2306b52.png').content
APP_ID = '16149264'
API_KEY = 'yxYg9r4OuAs4fYvfcl8tqCYd'
SECRET_KEY = 'yWg3KMds2muFsWs7MBSSFcgMQl8Wng4s'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
res = client.basicGeneral(image)
if 'words_result' in res.keys():
for item in res['words_result']:
print(item['words'])
else:
print(res)
运行需要安装 requests 和 AipOcr
进入cmd
cd C:\Python27\Scripts
安装:pip install baidu-aip
安装:pip install requests
修改下识别本地的图片
import requests
from aip import AipOcr
APP_ID = '16149264'
API_KEY = 'yxYg9r4OuAs4fYvfcl8tqCYd'
SECRET_KEY = 'yWg3KMds2muFsWs7MBSSFcgMQl8Wng4s'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
with open('e:\\12.png','rb') as f:
image = f.read() #image就是这张图片的二进制内容
text = client.basicAccurate(image) #调用百度的接口帮我们识别图片的内容
result = text['words_result'] #获取返回内容的字典
#遍历输出字典内容
for i in result:
print(i['words'])