用python 30行代碼,搞定一個簡單截圖調取的百度識字功能


在做一個數據標注過程中人工需要識別文字。

想了想寫了一個小腳本,

大致過程這樣的。

截圖功能寫了好久也沒寫明白,索性直接調用第三方的截圖工具了,在采用qq或者微信截圖時,截圖完成后保存大致保存在剪切板所以直接抽取了剪切板中的文件。

使用 PIL 中ImageGrab獲取剪切板文件,並保存在當前目錄

1 image = ImageGrab.grabclipboard()
2     s= 'xxx.png'
3     image.save(s)

調用百度圖片文字識別接口。取出輸出的文字信息,可參考見百度ocr文檔文字識別OCR (baidu.com)

 request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
    f = open(s, 'rb')
    img = base64.b64encode(f.read())
    params = {"image": img}
    access_token = 'xxx'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        for i in response.json()['words_result']:
            print(i['words'])

使用tkinter模塊設計一個窗口

# 設置窗口
window = Tk()
window.title('qcc_nw0.1')
# 設置窗口大小
window.geometry('500x800')
# lable標簽
l = Label(window, text='識圖2.0', bg='green', fg='white', font=('Arial', 12), width=30, height=2)
l.pack()
# 設置文本輸出欄的文本框
E1 = Text(window,width='100',height='100')
E1.insert("insert", txt')
            E1.pack(side=RIGHT)
# 定義按鈕
img_txt = Button(window, text='識圖嗎寶貝', font=('Arial', 10), width=15, height=1, command=s)
img_txt.pack()
window.mainloop()

完成;

看下完成后的效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM