首页 分享 调用百度api实现ocr识别整理错

调用百度api实现ocr识别整理错

来源:花匠小妙招 时间:2024-11-26 13:47

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

import tkinter

import tkinter.filedialog

import os

from PIL import ImageGrab

from time import sleep

from aip import AipOcr

from os import path

root = tkinter.Tk()

root.geometry('100x40+1000+300')

root.resizable(False, False)

def baiduOCR(picfile,outfile='result.txt'):

    filename = path.basename(picfile)

    APP_ID = '你的AppID'

    API_KEY = '你的API Key' 

    SECRECT_KEY = '你的Secret Key' 

    client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)

    i = open(picfile, 'rb')

    img = i.read()

    print("正在识别图片:t" + filename)

    options = {}

    options["detect_direction"] = "true"

    message = client.basicGeneral(img, options)  

    print("识别成功!")

    i.close()

    with open(outfile, 'a+') as fo:

        for text in message.get('words_result'):

            fo.writelines(text.get('words') + 'n')

        fo.writelines('n')

    print("文本导出成功!")

class MyCapture:

    def __init__(self, png):

        self.X = tkinter.IntVar(value=0)

        self.Y = tkinter.IntVar(value=0)

        screenWidth = root.winfo_screenwidth()

        screenHeight = root.winfo_screenheight()

        self.top = tkinter.Toplevel(root, width=screenWidth, height=screenHeight)

        self.top.overrideredirect(True)

        self.canvas = tkinter.Canvas(self.top,bg='white', width=screenWidth, height=screenHeight)

        self.image = tkinter.PhotoImage(file=png)

        self.canvas.create_image(screenWidth//2, screenHeight//2, image=self.image)

        def onLeftButtonDown(event):

            self.X.set(event.x)

            self.Y.set(event.y)

            self.sel = True

        self.canvas.bind('<Button-1>', onLeftButtonDown)

        def onLeftButtonMove(event):

            if not self.sel:

                return

            global lastDraw

            try:

                self.canvas.delete(lastDraw)

            except Exception as e:

                pass

            lastDraw = self.canvas.create_rectangle(self.X.get(), self.Y.get(), event.x, event.y, outline='black')

        self.canvas.bind('<B1-Motion>', onLeftButtonMove)

        def onLeftButtonUp(event):

            self.sel = False

            try:

                self.canvas.delete(lastDraw)

            except Exception as e:

                pass

            sleep(0.1)

            left, right = sorted([self.X.get(), event.x])

            top, bottom = sorted([self.Y.get(), event.y])

            pic = ImageGrab.grab((left+1, top+1, right, bottom))

            fileName = 'tmp2.png'

            if fileName:

                pic.save(fileName)

            print("图片识别...")

            baiduOCR(fileName)

            sleep(0.1)

            os.remove(fileName)

            self.top.destroy()

        self.canvas.bind('<ButtonRelease-1>', onLeftButtonUp)

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

def buttonCaptureClick():

    root.state('icon')

    sleep(0.2)

    filename = 'temp.png'

    im = ImageGrab.grab()

    im.save(filename)

    im.close()

    w = MyCapture(filename)

    buttonCapture.wait_window(w.top)

    root.state('normal')

    os.remove(filename)

buttonCapture = tkinter.Button(root, text='截图识别', command=buttonCaptureClick)

buttonCapture.place(x=10, y=10, width=80, height=20)

root.mainloop()

相关知识

手把手教你调用百度api(以花卉识别为例)
百度接口实现花卉识别
微信小程序之植物识别demo(百度开发接口)
Python识别花卉种类,并自动整理分类!
Python 识别花卉种类,并自动整理分类!
Python实现识别花卉种类的示例代码
Python 识别花卉种类,并自动整理分类
在线识别汉字
[图像识别]水果农作物病虫害识别API
百度AI攻略:植物识别

网址: 调用百度api实现ocr识别整理错 https://www.huajiangbk.com/newsview741796.html

所属分类:花卉
上一篇: 用AUTOIT来管理升级分发公司
下一篇: 汉仪花农体简详情页

推荐分享