python语言简单界面选择苏康码图片文字识别

 ocr接口推荐

        https://ocr.space/OCRAPI
        支持多种语言 示例 ocrspace.py 支持本地路径或网络路径

def ocr_space_file(filename, overlay=False, api_key='helloworld', language='eng'):
    """ OCR.space API request with local file.
        Python3.5 - not tested on 2.7
    :param filename: Your file path & name.
    :param overlay: Is OCR.space overlay required in your response.
                    Defaults to False.
    :param api_key: OCR.space API key.
                    Defaults to 'helloworld'.
    :param language: Language code to be used in OCR.
                    List of available language codes can be found on https://ocr.space/OCRAPI
                    Defaults to 'en'.
    :return: Result in JSON format.
    """

    payload = {'isOverlayRequired': overlay,
               'apikey': api_key,
               'language': language,
               }
    with open(filename, 'rb') as f:
        r = requests.post('https://api.ocr.space/parse/image',
                          files={filename: f},
                          data=payload,
                          )
    return r.content.decode()

设计界面用vb插件

https://github.com/cdhigh/tkinter-designer

安装程序注册或手动注册
    运行regsvr32 /s TkinerDesigner.dll
    在C:\Windows\VBADDIN.INI的段[Add-Ins32]增加一行:
    TkinterDesigner.Connect=3
language 语言文件有的话可以切换语言,没有的话就一种默认语言
TextBox Python文本框有两种:Entry和Text,如果VB的TextBox的MultiLine=False,则 生成Entry,否则生成Text。
ComboBox 组合框在Tkinter中没有对应的控件,比较类似的只有OptionMenu,类似ComboBox 的Style=2 (Dropdown List)时的表现,一个下拉列表,只能在列表中选择一个值, 不能直接输入。所以建议在VB的ComboBox中写下所有的下拉列表值。 如果启用了TTK主题扩展库支持,则直接对应到TTK的Combobox,外形和行为基本 一致

准备框架

在vb中画好界面输出py文件使用
为了以后升级,可以把ui部分和按钮调用部分分到2个文件 ui.py 和 ocrspacevb.py
本程序很简单,就是选择一个文件,调用space的ocr接口,得到返回json展示到文本框
from tkinter import *
from tkinter.ttk import *

class Application_ui(Frame):
    # 这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('测试ocr')
        w=800
        h=340
        ws = self.master.winfo_screenwidth()
        hs = self.master.winfo_screenheight()
        x = (ws / 2) - (w / 2)
        y = (hs / 2) - (h / 2)
        self.master.geometry('%dx%d+%d+%d' % (w,h,x,y))
        # self.master.geometry('800x339')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.Label1Var = StringVar(value='文件')
        self.style.configure('TLabel1.TLabel', anchor='w', font=('宋体', 9))
        self.Label1 = Label(self.top, text='Label1', textvariable=self.Label1Var, style='TLabel1.TLabel')
        self.Label1.setText = lambda x: self.Label1Var.set(x)
        self.Label1.text = lambda: self.Label1Var.get()
        self.Label1.place(relx=0.056, rely=0.024, relwidth=0.15, relheight=0.074)

        self.Text1Var = StringVar(value='Text1')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体', 9))
        self.Text1.setText = lambda x: self.Text1Var.set(x)
        self.Text1.text = lambda: self.Text1Var.get()
        self.Text1.place(relx=0.241, rely=0.024, relwidth=0.521, relheight=0.074)

        self.Command1Var = StringVar(value='选择')
        self.style.configure('TCommand1.TButton', font=('宋体', 9))
        self.Command1 = Button(self.top, text='选择', textvariable=self.Command1Var, command=self.Command1_Cmd, style='TCommand1.TButton')
        self.Command1.setText = lambda x: self.Command1Var.set(x)
        self.Command1.text = lambda: self.Command1Var.get()
        self.Command1.place(relx=0.796, rely=0.024, relwidth=0.123, relheight=0.097)

        self.Command2Var = StringVar(value='退出')
        self.style.configure('TCommand2.TButton', font=('宋体', 9))
        self.Command2 = Button(self.top, text='退出', textvariable=self.Command2Var, command=self.Command2_Cmd, style='TCommand2.TButton')
        self.Command2.setText = lambda x: self.Command2Var.set(x)
        self.Command2.text = lambda: self.Command2Var.get()
        self.Command2.place(relx=0.796, rely=0.826, relwidth=0.123, relheight=0.097)


        self.Text2 = Text(self.top,  font=('宋体', 9))
        self.Text2.setText = lambda x: self.Text2.insert(INSERT,x)
        self.Text2.text = lambda: self.Text2.get(1.0,END).strip().replace("\n","")
        self.Text2.place(relx=0.037, rely=0.142, relwidth=0.688, relheight=0.805)

        self.Command3Var = StringVar(value='识别')
        self.style.configure('TCommand3.TButton', font=('宋体', 9))
        self.Command3 = Button(self.top, text='识别', textvariable=self.Command3Var, command=self.Command3_Cmd, style='TCommand3.TButton')
        self.Command3.setText = lambda x: self.Command3Var.set(x)
        self.Command3.text = lambda: self.Command3Var.get()
        self.Command3.place(relx=0.796, rely=0.142, relwidth=0.123, relheight=0.097)

如上是界面,如下是调用处理

import json
import requests
from ui import *
from tkinter import *
from tkinter.messagebox import *
import tkinter.filedialog as tkFileDialog

class Application(Application_ui):
    # 这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
    def __init__(self, master=None):
        Application_ui.__init__(self, master)

    def Command1_Cmd(self, event=None):
        filepath = tkFileDialog.askopenfilename(filetypes=[("JPG", ".jpg"), ("PNG", ".png"), ("GIF", ".gif")], title="选择图片文件")
        self.Text1.setText(filepath)
        pass

    def Command2_Cmd(self, event=None):
        e = askyesno(title='退出确认', message='请问是否要退出?')
        if e:
            self.quit()

    def Command3_Cmd(self, event=None):
        # test_file = ocrspace.ocr_space_file(filename=self.Text1.text(), language='chs')
        test_file = '{"ParsedResults":[{"TextOverlay":{"HasOverlay":false,"Message":"Text overlay is not provided as it is not requested"},"TextOrientation":"0","FileParseExitCode":1,' \
                    '"ParsedText":"中国电信\r\n《16:04\r\n海航移动\r\n苏康码\r\n0苏康码\r\n上官雄哥\r\n320******517\r\n若您有近14天匚\r\n04一0916:03:26\r\n绿色:勤洗手、常通风、戴口罩,出现发热咳嗽等\r\n症状请及时就医\r\n","ErrorMessage":"",' \
                    '"ErrorDetails":""}],"OCRExitCode":1,"IsErroredOnProcessing":false,"ProcessingTimeInMilliseconds":"640","SearchablePDFURL":"Searchable PDF not generated as it was not requested."}'
        test_file = test_file.replace('\r\n', '#')
        contents = json.loads(test_file)["ParsedResults"]
        for content in contents:  # 遍历所有结果
            txt = content['ParsedText'].strip()
            txt = txt.split('#')
            name=''
            color=''
            for line in txt:
                if line.find('***') > 0 and len(name) < 1:
                    name=color
                color=line
                if color.find('色')>-1:
                    color=color[:color.find('色')]
                    print(color)  # strip去除空格 他返回的结果自带一个换行
                    self.Text2.setText(name+' '+color)


if __name__ == "__main__":
    top = Tk()
    Application(top).mainloop()

其中test_file就是识别到字符串,得到后就保存成字符串调试了,没有反复调用http接口,节省资源

顺便记录一下手动安装python的方法吧

wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
(这里的链接以实际链接为准)
然后等到下载完成 在终端输入:
tar -zxvf Python-3.8.2.tgz (这里以实际文件名为准)
第三步,配置makefile文件并编译
(以下$皆表示终端,不用输入$)
$ cd Python-3.8.2 (这是解压出来的目录)
$ ./configure --enable-optimizations
(在下面这步嘛,过个5-10分钟左右就可以看到要输入密码了)
$ make -j8 && sudo make altinstall
注:需要重新编译时...
$ sudo make clean
最后文件在/usr/local/bin/python3.7
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值