百度ocr详细指导

登陆https://login.bce.baidu.com/?redirect=https%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview

百度ocr详细指导_第1张图片
百度ocr详细指导_第2张图片
百度ocr详细指导_第3张图片
代码中将这块填了

最关键关键!!!!!!!!!!!!!!!!!!!!!!!
百度ocr详细指导_第4张图片
记得领取额度啊,不然程序过不了

import tkinter
from PIL import ImageGrab
from aip import AipOcr
import time
import os
import win32api,win32con

class MyCapture:

    def __init__(self):
        # 变量X和Y用来记录鼠标左键按下的位置
        self.X = tkinter.IntVar(value=0)
        self.Y = tkinter.IntVar(value=0)
        self.sel = False
        self.ocr_text = None
        self.capture_png_path = ''
        self.capture_text_box = tkinter.Text(window)  # 创建text容器用于存放截图识别的文字
        self.capture_text_box.place(x=20, y=50, anchor='nw', width=350, height=330)

        self.capture_btn = tkinter.Button(text='截图', command=self.capture_cmd)  # 创建一个按钮
        self.capture_btn.place(x=80, y=10, anchor='nw', width=60, height=20)  # 在创建的窗口的西北角x=20,y=10处放置按钮

        # 屏幕尺寸
        self.screenWidth = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
        self.screenHeight = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
        print(self.screenWidth)
        print( self.screenHeight)
        self.temp_png = 'temp.png'

    def create_canvas(self):
        time.sleep(0.2)
        im = ImageGrab.grab()
        im.save(self.temp_png)
        im.close()
        # 创建顶级组件容器
        self.top = tkinter.Toplevel(window, width=self.screenWidth, height=self.screenHeight)
        # 不显示最大化、最小化按钮
        self.top.overrideredirect(True)
        self.canvas = tkinter.Canvas(self.top, bg='white', width=self.screenWidth, height=self.screenHeight)
        # 显示全屏截图,在全屏截图上进行区域截图
        self.image = tkinter.PhotoImage(file=self.temp_png)
        self.canvas.create_image((0,0) ,anchor='nw', image=self.image)

        # 鼠标左键按下的位置
        self.canvas.bind('', self.mouse_left_down)
        # 鼠标左键移动,显示选取的区域
        self.canvas.bind('', self.mouse_move)
        # 获取鼠标左键抬起的位置,保存区域截图
        self.canvas.bind('', self.mouse_left_up)

        self.canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)

    def mouse_left_down(self, event):
        """鼠标左键按下的位置"""
        self.X.set(event.x)
        self.Y.set(event.y)
        self.sel = True  # 开始截图

    # 鼠标左键移动,显示选取的区域
    def mouse_move(self, event):
        if not self.sel:
            return
        try:
            # 删除刚画完的图形,要不然鼠标移动的时候是黑乎乎的一片矩形
            self.canvas.delete(self.lastDraw)
        except Exception as e:
            pass
        self.lastDraw = self.canvas.create_rectangle(self.X.get(), self.Y.get(), event.x, event.y, outline='red')

    def mouse_left_up(self, event):
        """获取鼠标左键抬起的位置,保存区域截图"""
        self.sel = False
        try:
            self.canvas.delete(self.lastDraw)
        except Exception as e:
            pass
        # 考虑鼠标左键从右下方按下而从左上方抬起的截图
        x1, x2 = sorted([self.X.get(), event.x])  # tkinter记录的坐标点
        y1, y2 = sorted([self.Y.get(), event.y])

        pic = ImageGrab.grab((x1+1, y1+1, x2, y2))
        # pic.show()
        self.capture_png_path = 'capture_png.png'
        pic.save(self.capture_png_path)

        # 关闭当前窗口
        self.top.destroy()

    def capture_cmd(self):
        """点击截图按钮触发函数"""
        window.iconify()  # 窗口最小化
        # 显示全屏幕截图
        self.create_canvas()
        self.capture_btn.wait_window(self.top)
        os.remove(self.temp_png)
        self.ocr_text = self.baidu_ocr(self.capture_png_path)
        # print(self.ocr_text)
        if self.ocr_text:
            self.capture_text_box.delete('1.0', tkinter.END)   # 清空文本框
            self.translate_text_box.delete('1.0', tkinter.END)
            self.capture_text_box.insert('end', self.ocr_text)
            window.deiconify()  # 窗口显示
            os.remove(self.capture_png_path)

    def baidu_ocr(self, file_path):
        """ 调用通用文字识别, 图片参数为本地图片 """
        app_id = ''
        api_key = ''
        secret_key = ''
        ocr_text = ''
        if os.path.isfile(file_path):
            with open(file_path, 'rb') as fp:
                image = fp.read()
            ocr_ret = AipOcr(app_id, api_key, secret_key).basicGeneral(image)
            words = ocr_ret.get('words_result')
            if words is not None and len(words):
                for word in words:
                    # print(word['words'], end='\n')
                    ocr_text += word['words'] + '\n'
                return ocr_text
            else:
                return None
        else:
            return None


window = tkinter.Tk()
window.title('Capture')
# 创建tkinter主窗口
window.geometry('400x400')  # 指定主窗口位置与大小
MyCapture()
window.mainloop()

你可能感兴趣的:(百度,python,pycharm)