OCR(Optical Character Recognition,光學字符識別),用于識別圖片中的文字。本文測試,百度OCR文字識別的接口,默認文字識別次數(shù)是每天500次,網址。
源碼參考:
https://github.com/SpikeKing/MachineLearningTutorial/blob/master/tests/ocr_test.py
OCR
創(chuàng)建應用,在應用中,百度提供默認的API Key和Secret Key,API接口文檔。
方法:get_access_token
的參數(shù)是應用的API Key
和Secret Key
,獲取特定的access_token
。
def get_access_token(app_key, secret_key):
api_key_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s'
# client_id 為官網獲取的AK, client_secret 為官網獲取的SK
host = (api_key_url % (app_key, secret_key))
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
keys = json.loads(content)
access_token = keys['access_token']
return access_token
方法recognize_image_words
識別圖片中的文字,數(shù)據(jù)源可選網絡圖片或本地圖片。
def recognize_image_words(access_token, img):
ocr_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic/?access_token=%s'
url = (ocr_url % access_token)
# 上傳的參數(shù)
data = dict()
data['languagetype'] = "CHN_ENG" # 識別中文
# 圖片數(shù)據(jù)源,網絡圖片或本地圖片
if img.startswith('http://'):
data['url'] = img
else:
image_data = open(img, 'rb').read()
data['image'] = image_data.encode('base64').replace('\n', '')
# 發(fā)送請求
decoded_data = urllib.urlencode(data)
req = urllib2.Request(url, data=decoded_data)
req.add_header("Content-Type", "application/x-www-form-urlencoded")
# 獲取請求的數(shù)據(jù),并讀取內容
resp = urllib2.urlopen(req)
content = resp.read()
# 識別出的圖片數(shù)據(jù)
words_result = json.loads(content)['words_result']
words_list = list()
for words in words_result:
words_list.append(words['words'])
return words_list
main函數(shù),將兩個函數(shù)連接在一起。
if __name__ == '__main__':
img = './data/text_img2.jpeg'
online_img = "http://www.zhaoniupai.com/hbv/upload/20150714_LiangDuiBan.jpg"
access_token = get_access_token(app_key=app_key, secret_key=secret_key)
print 'access_token: %s' % access_token
print '\nwords_list:'
words_list = recognize_image_words(access_token, online_img)
for x in batch(words_list, 5): # 每次打印5個數(shù)據(jù)
show_string(x)
數(shù)據(jù)結果,網絡圖片1次速度大約0.43秒,本地圖片(47KB)大約1.27秒。
words_list:
["上蔡縣人民醫(yī)院檢驗報告單", "科室:中醫(yī)手科門診", "送檢時間:2015", "采樣時間:2015", "費別"]
["B4x乙肝表面抗原", "201.010陽性", "乙肝表面抗體", "0-10", "31陰性"]
["BeAg乙肝e抗原", "70陰性", "0-0.25", "BeAb乙肝e抗體", "1.114陰性"]
["Bk=Al乙肝核心抗體", "49.006陽性", " cu / .", "0-2"]
time elapsed: 00:00:00.43
測試圖片:
測試圖片
OK, that's all! Enjoy it!