Python, using the Baidu API to replicate the text in the screenshot

Source: Internet
Author: User

Baidu Cloud Text recognition technical documents:

Https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E6.96.B0.E5.BB.BAAipOcr

 fromAipImportAipocr#Baidu AIP fromPILImportImagegrab#working with clipboard pictures fromPILImportImageImportPILImportKeyboard#Monitor KeyboardImportSYSImportTime,datetimeImportRandomImportWin32clipboard as W#Handling Shear PlatesImportWin32condefscreenshot ():" "Monitor keyboard events and save pictures" "    #Monitor Keyboard, enter QQ default shortcut key    ifKeyboard.wait (hotkey='Ctrl+alt+a') ==None: whileTrue:time.sleep (3)#waitim = Imagegrab.grabclipboard ()#get the picture in the Clipboard            ifIsinstance (Im,pil. Bmpimageplugin.dibimagefile):#if the contents of the Clipboard can be resolved into a picture                #file namei = Datetime.datetime.now (). Strftime ('%y%m%d%h%m%s') R= str (Random.randint (100,1000))                #Save PictureIm.save (i+r+'. PNG')                                         #Baidu Cloud Account Settingsapp_id ="'Api_key="'Secret_key="'                #Baidu Cloud API ObjectClient =AIPOCR (app_id, Api_key, Secret_key)#Reading PicturesImage = Get_file_content (i+r+'. PNG')                #get the text content in a picturedata =client.basicgeneral (image) Words_result= data['Words_result'] Data=""  #what you need to save                 forWordsinchWords_result:data+=words['words']                                Print(data) setText (data)#Reading Picturesdefget_file_content (FilePath): With open (FilePath,'RB') as FP:returnFp.read ()#Writing Clipboard contentsdefSetText (astring): W.openclipboard () W.emptyclipboard () W.setclipboardtext (astring) W.closeclipboard () if __name__=="__main__":#For _ in range (sys.maxsize): #修改成在screenShot中用while循环Screenshot ()

Python, using the Baidu API to implement the text in the copy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.