Python implementation picture identification plus translation will learn [paying]


Python uses AI interface Baidu picture identification plus translation



Also a lot of people in the process of learning Python, often because there is no one good tutorial or guide leading their own easy to give up, and I built a Python exchange dress: a long time and its military while a stream of thought (digital homonym) down conversion can be found, and there are new Python tutorial projects can take, do not understand the problem more exchanges with people inside will solve Oh! Next, enter theme

The 30th anniversary of the birth of python

# encoding:utf-8

Requests Import
Import Base64
from PIL Import Image
Import pytesseract
# here about the need to install Tesseract-OCR
# link: HTTPS: //pan.baidu.com/s/1D2eODet7x9xshBVi6ZUZ_Q
# extraction code: qfef
after installed # Do not forget to Tesseract-OCR path environment variable added to the
Import JSON
Import Requests
Import monitor Keyboard keys # library
from PIL import ImageGrab # image processing library
Time Import
from the install baidu_aip AIP Import AipOcr #pip

# Print ( "start capture")

# 1. Image taken
keyboard.wait (Hotkey = 'Ctrl + Alt + A')
# Print ( "keyboard pressed 'Ctrl + Alt + A'")
keyboard.wait ( 'Enter')
# Print ( "keyboard by under the 'enter' ")

# Analog delay, to solve the problem grabclipboard cache function (grabclipboard function operating too fast, it will read the last content)
the time.sleep (0.1)

# 2. Save the image to your computer
Image = ImageGrab.grabclipboard ()
the Image.Save ( 'screen.png')

#*************************************************************************************

= request_url "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# open image files in binary mode
f = Open ( 'screen.png', 'rb')
img = base64.b64encode (f .read ())

= {params "Image": img}
the access_token = "your own access_token, Baidu AI there are tutorials, I put the URL below the"
# https://ai.baidu.com/ai-doc/OCR/vk3h7y58v
request_url = the access_token + + request_url "the access_token =?"
headers = { 'Content-type': 'file application / X-WWW-form-urlencoded'}
Response = requests.post (request_url, Data = the params, headers = headers)

print("文字识别:")
if response:
locList = response.json()['words_result']
for i in locList:
print(i['words'])

print("\n翻译:")
if response:
locList = response.json()['words_result']
for i in locList:
text = i['words']

##================================================================================##

# Translation function, word translated content
DEF Translate (Word):
# way dictionary API
URL = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
# transmission parameters, where i is the content to be translated

= {Key
'type': "the AUTO",
'I': Word,
"DOCTYPE": "JSON",
"Version": "2.1",
"keyfrom": "fanyi.web",
"UE": "UTF . 8 ",
" Action ":" FY_BY_CLICKBUTTON ",
" typoResult ":" to true "
}
# Key to be sent to the dictionary server way dictionary content
Response = requests.post (URL, Data = Key)
# server determines whether the corresponding successful
200 response.status_code == IF:
# then the corresponding result
return response.text
the else:
Print ( "way dictionary call failed")
# failure to return empty
return None

get_reuslt DEF (repsonse):
# json.loads the results returned by loading into json format
Result = json.loads (repsonse)
Print ( "% S"% Result [ 'translateResult'] [0] [0] [ 'TGT' ])


def main():
list_trans = translate(text)
get_reuslt(list_trans)


__name__ == IF '__main__':
main ()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
to save the image as follows:

Print results are as follows:

写完了,看明白了吗? 另外很多人在学习Python的过程中,往往因为没有好的教程或者没人指导从而导致自己容易放弃,为此我建了个Python交流.裙 :一久武其而而流一思(数字的谐音)转换下可以找到了,里面有最新Python教程项目可拿,不懂的问题多跟里面的人交流,都会解决哦!
————————————————
版权声明:本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12115454.html