效果图
在这里插入图片描述

##调用百度AI,实现文字识别
# encoding:utf-8

import requests
import sys
import json
import base64
import tkinter as tk
from tkinter import filedialog
from tkinter import scrolledtext

# 保证兼容python2以及python3
IS_PY3 = sys.version_info.major == 3
if IS_PY3:
    from urllib.request import urlopen
    from urllib.request import Request
    from urllib.error import URLError
    from urllib.parse import urlencode
    from urllib.parse import quote_plus
else:
    import urllib2
    from urllib import quote_plus
    from urllib2 import urlopen
    from urllib2 import Request
    from urllib2 import URLError
    from urllib import urlencode

# 防止https证书校验不正确
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

API_KEY = ''###单引号内内容已删,自己注册,用自己的 

SECRET_KEY = ''###单引号内内容已删,自己注册,用自己的 


#OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
#OCR_URL ="https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
myUrl={'通用文字识别(高精度版)':"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic",
       '通用文字识别(标准版)':"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic",
       '通用文字识别(标准含位置版)':"https://aip.baidubce.com/rest/2.0/ocr/v1/general",
       '通用文字识别(高精度含位置版)':"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate",
       '手写文字识别':"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"}


"""  TOKEN start """
TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'


"""
    获取token
"""
def fetch_token():
    params = {'grant_type': 'client_credentials',
              'client_id': API_KEY,
              'client_secret': SECRET_KEY}
    post_data = urlencode(params)
    if (IS_PY3):
        post_data = post_data.encode('utf-8')
    req = Request(TOKEN_URL, post_data)
    try:
        f = urlopen(req, timeout=5)
        result_str = f.read()
    except URLError as err:
        print(err)
    if (IS_PY3):
        result_str = result_str.decode()


    result = json.loads(result_str)

    if ('access_token' in result.keys() and 'scope' in result.keys()):
        if not 'brain_all_scope' in result['scope'].split(' '):
            print ('please ensure has check the  ability')
            exit()
        return result['access_token']
    else:
        print ('please overwrite the correct API_KEY and SECRET_KEY')
        exit()

"""
    读取文件
"""
def read_file(image_path):
    f = None
    try:
        f = open(image_path, 'rb')
        return f.read()
    except:
        print('read image file fail')
        return None
    finally:
        if f:
            f.close()


"""
    调用远程服务
"""
def request(url, data):
    req = Request(url, data.encode('utf-8'))
    has_error = False
    try:
        f = urlopen(req)
        result_str = f.read()
        if (IS_PY3):
            result_str = result_str.decode()
        return result_str
    except  URLError as err:
        print(err)

def createWin():
    global wzEntry,wzBtn,sbText
    win=tk.Tk()
    win.title('《文字识别》体验软件')
    sbText=scrolledtext.ScrolledText(win,font='楷体 15')
    sbText.pack(side=tk.BOTTOM,fill=tk.BOTH,padx=10,pady=(5,10))
    label=tk.Label(win,text='请在本地选择要识别的图片',font='楷体 15')
    label.pack(side=tk.LEFT,padx=10,pady=(10,0))
    wzEntry=tk.Entry(win,font='楷体 15',width=50)
    wzEntry.pack(side=tk.LEFT)
    wzBtn=tk.Button(win,text='浏览',font='楷体 15',relief='groove',command=liulan)
    wzBtn.pack(side=tk.LEFT,padx=10,pady=(10,0))
    #通用文字识别
    tysbBtn=tk.Button(win,text='通用文字识别',font='楷体 15',relief='groove',command=shibie)
    tysbBtn.pack(side=tk.LEFT,padx=10,pady=(10,0))
    tysbBtn.bind("<Button-1>",onClick)
    #手写文字识别
    sxsbBtn=tk.Button(win,text='手写文字识别',font='楷体 15',relief='groove',command=shibie)
    sxsbBtn.pack(side=tk.LEFT,padx=10,pady=(10,0))
    sxsbBtn.bind("<Button-1>",onClick)
def onClick(event):
    global name
    name=event.widget['text']
def liulan():
    filepath=filedialog.askopenfilename(title='选择图片文件', filetypes=[('image', '*.jpg *.png *.jfif')])
    wzEntry.delete(0,tk.END)
    wzEntry.insert(tk.END,filepath)
    
def shibie():
    if wzEntry.get():
        if name=='通用文字识别':
            try:
                OCR_URL =myUrl['通用文字识别(高精度版)']
                toshibie(OCR_URL)
            except:
                try:
                    OCR_URL =myUrl['通用文字识别(高精度含位置版)']
                    toshibie(OCR_URL)
                except:
                    try:
                        OCR_URL =myUrl['通用文字识别(高精度版)']
                        toshibie(OCR_URL)
                    except:
                        try:
                            OCR_URL =myUrl['通用文字识别(标准版)']
                            toshibie(OCR_URL)
                        except:
                            tkinter.messagebox.showwarning('警告','可用api已用完!')
                            
                       
        else:
            OCR_URL =myUrl['手写文字识别']

def toshibie(OCR_URL):
    # 获取access token
    token = fetch_token()
     # 拼接通用文字识别高精度url
    image_url = OCR_URL + "?access_token=" + token

    text = ""

    # 读取测试图片
    file_content = read_file(wzEntry.get())

    # 调用文字识别服务
    result = request(image_url, urlencode({'image': base64.b64encode(file_content)}))

    #清空原来内容
    sbText.delete(1.0,tk.END)
    # 解析返回结果
    result_json = json.loads(result)
    
    for words_result in result_json["words_result"]:
        text = text + words_result["words"]
        sbText.insert(tk.END,words_result["words"])
        sbText.insert(tk.END,'\n')
    #print(words_result["words"])

if __name__ == '__main__':
    #生成界面
    createWin()


Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐