百度api识别图片文字
2021-05-30 04:22
标签:tesseract span secret 百度api ast ras arp webdriver rate 百度api识别图片文字 标签:tesseract span secret 百度api ast ras arp webdriver rate 原文地址:https://www.cnblogs.com/nickshen/p/11057247.htmlfrom selenium import webdriver
from selenium.webdriver.common.keys import Keys
from PIL import Image,ImageGrab,ImageEnhance
import pytesseract
import time,os
from pytesseract import *
from numpy import *
from scipy import *
from aip import AipOcr
#
brower = webdriver.Chrome()
url = "http://passport.hualala.com/"
brower.maximize_window()
brower.get(url)
time.sleep(1)
bbox = (1509, 485, 1612, 527) #先确定X,Y坐标,然后在X坐标上加图片的宽度,Y坐标上加图片的高度(1516, 485, 1603, 527)
im =ImageGrab.grab(bbox)
im.save(‘./image_file/a.png‘)
#
# 打开图片识别验识码
image = Image.open(‘./image_file/a.png‘)
image = image.convert("L")
sharpness = ImageEnhance.Contrast(image)
sharp_img = sharpness.enhance(9.0)
sharp_img.save(‘./image_file/a1.png‘)
k = Image.open(‘./image_file/a1.png‘)
text = pytesseract.image_to_string(k)
print(text)
app_id = ‘16575748‘
api_key = ‘cgRELw4wo2lajhaVjTXR7a9Q‘
secret_key = ‘8FTRcbn3iYp3fGyIMhAQrWmSyUGN9BdU‘
client = AipOcr(app_id, api_key, secret_key)
i = open(‘./image_file/a1.png‘, ‘rb‘)
img = i.read()
print("正在识别图片---------")
# message = client.basicGeneral(img)
message = client.basicAccurate(img)
print(message)
print(message[‘words_result‘][0][‘words‘])
i.close()
with open(‘test.txt‘, "a+")as f:
for k in message.get(‘words_result‘):
f.writelines(k.get(‘words‘))