当前位置:

首页 > 编程开发 > Python EasyOCR库的使用方法

Python EasyOCR库的使用方法

说明1、EasyOCR是一个用python编写的OCR三方库。可以在python中调用,用来识别图像中的文字,并输出为文本。2、支持80多种语言的识别,识别精度高,甚至要超过PaddleOCR。安装命令pipinstalleasyocr代码实现importeasyocr#设置识别中英文两种语言reader=easyocr.Reader(["ch_sim","en"],gpu=False)#needtorunonlyoncetoloadmodelintomemoryresult=reader.readtex

本文目录

    说明

    1、EasyOCR是一个用python编写的OCR三方库。可以在python中调用,用来识别图像中的文字,并输出为文本。

    2、支持80多种语言的识别,识别精度高,甚至要超过PaddleOCR。

    安装命令

    pip install easyocr

    代码实现

    import easyocr
     
    #设置识别中英文两种语言
    reader = easyocr.Reader(["ch_sim","en"], gpu = False) # need to run only once to load model into memory
    result = reader.readtext(r"d:Desktop4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png", detail = 0)
    print(result)

    实例扩展:

    图文提取的代码

    from pathlib import Path
    import easyocr
    
    
    file_url = r"识别图片.jpg"    # 需识别的图片
    split_symbol = " "          # 默认空格为分隔符
    row_space = 15              # 默认字符高度为15px,当识别出来的字符间距超过这个数值时会换行。
    
    
    def make_reader():
        # 将模型加载到内存中。模型文件地址 C:Users用户.EasyOCRmodel
        reader = easyocr.Reader(["ch_sim", "en"])
        return reader
    
    
    def change_to_character(file_url, reader, split_symbol=" ", row_space=15, save_dir="."):
        with open(file_url, "rb") as img:
            img_b = img.read()
        result = reader.readtext(img_b)
    
        result.sort(key=lambda x: x[0][0][1])  # 按竖直方向,进行排序==>进行分行处理。
        # for i in result:
        #     print(i)
        # print("="*100)
    
        # 按行进行分组
        content = []
        item = [result[0]]  # 首先放入第一个元素
        for i in result[1:]:
            if row_space >= i[0][0][1] - item[-1][0][0][1] >= 0:
                item.append(i)
            else:
                content.append(item)
                item = [i]
        content.append(item)
    
        filemane = Path(file_url).name.split(".")[0]
        with open(f"{save_dir}/{filemane}.txt", "w", encoding="utf8") as t:
            for i in content:                     # i 为每一行的内容
                i.sort(key=lambda x: x[0][0][0])  # 对每行的内容进行先后排序
                for r in i:
                    # print(r)
                    t.write(r[1] + split_symbol)
                t.write("
    ")
        return content
    
    
    if __name__ == "__main__":
        change_to_character(file_url,  make_reader())

    UI 界面的代码

    import tkinter as tk
    from tkinter import filedialog
    from PIL import Image, ImageTk
    from pathlib import Path
    from character import change_to_character, make_reader
    from threading import Thread
    import time
    
    # class Showing(tk.Frame):
    #     def __init__(self, master=None):
    #         super().__init__(master)
    #         self.master = master
    #         self.pack()
    #         # self.img = tk.PhotoImage(file=r"C:UsersyanhyDesktop捕获22.PNG")
    #         self.create_widgets()
    #
    #     def create_widgets(self):
    #         self.img = tk.PhotoImage(file=r"C:UsersyanhyDesktop捕获22.PNG")
    #         self.img_wig = tk.Label(self, image=self.img)
    #         self.img_wig.pack()
    
    
    # 最外层窗口设置
    root = tk.Tk()
    root.title("图片文字识别程序                    联系:410889472@qq.com")
    window_x = root.winfo_screenwidth()
    window_y = root.winfo_screenheight()
    WIDTH = 1200
    HEIGHT = 750
    x = (window_x - WIDTH) / 2  # 水平居中
    y = (window_y - HEIGHT) / 3  # 垂直偏上
    root.geometry(f"{WIDTH}x{HEIGHT}+{int(x)}+{int(y)}")
    root.resizable(width=False, height=False)
    
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    Row_space = 15
    File_url_list = []
    Img_type = [".jpg", ".jpeg", ".png", ".gif"]
    Split_symbol = " "                               # 间隔符。
    Save_dir = Path.cwd().joinpath("img_to_word")
    if Save_dir.is_dir():
        pass
    else:
        Path.mkdir(Save_dir)
    
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    
    def test():
        print(f"{Row_space=}")
    
    
    def choose_file():       # 获取导入的图片路径地址
        global show_img, img_label, text, File_url_list
        filenames = filedialog.askopenfilenames()
        if len(filenames) == 1 and len(File_url_list) == 0:       # 单张图片导入,显示图片
            if Path(filenames[0]).suffix.lower() in Img_type:     # 判断是否图片类型
                File_url_list = list(filenames)
                try:
                    if text.winfo_exists():
                        text.destroy()
                except NameError as e:
                    print(f"choose_file提示:张图片导入错误>>> {e}")
                try:
                    if img_label.winfo_exists():
                        img_label.destroy()
                except NameError as e:
                    print(f"choose_file提示:单张图片导入错误>>> {e}")
                img = Image.open(File_url_list[0]).resize((560, 660))
                # print(img.size)
                show_img = ImageTk.PhotoImage(image=img)
                img_label = tk.Label(f_left, image=show_img)
                img_label.pack()
            else:
                print("导入的是非图像格式")
        else:                                     # 多张图片导入,显示列表。
            try:
                if img_label.winfo_exists():
                    img_label.destroy()
            except NameError as e:
                print(f"提示:多张图片导入错误>>> {e}")
            try:
                if text.winfo_exists():
                    text.destroy()
            except NameError as e:
                print(f"提示:多张图片导入错误>>> {e}")
            text = tk.Text(f_left, spacing1=5, spacing3=5)
            text.pack(fill="both", expand=True)
    
    
            for i in filenames:
                if Path(i).suffix.lower() in Img_type:
                    File_url_list.append(i)
                else:
                    pass
            File_url_list = set(File_url_list)
            for i in list(File_url_list):       # 把文件写入到文本框中
                text.insert("end", str(list(File_url_list).index(i)+1) + ": " + i + "
    ")
            File_url_list = list(File_url_list)
        print(f"{File_url_list=}")
    
    
    def choose_dir():
        global show_img, img_label, text, File_url_list
        directoryname = filedialog.askdirectory()
        print(f"{directoryname=}")
        try:
            if img_label.winfo_exists():
                img_label.destroy()
        except NameError as e:
            print(f"choose_dir提示:多张图片导入错误>>> {e}")
        try:
            if text.winfo_exists():
                text.destroy()
        except NameError as e:
            print(f"choose_dir提示:多张图片导入错误>>> {e}")
        text = tk.Text(f_left, spacing1=5, spacing3=5)
        text.pack(fill="both", expand=True)
    
        for i in Path(directoryname).iterdir():       # 获取文件夹下的所有文件。
            if Path(i).suffix.lower() in Img_type:
                File_url_list.append(i.as_posix())    # as_posix() 把Path型转为字符串。
            else:
                pass
        File_url_list = set(File_url_list)
        for i in list(File_url_list):  # 把文件写入到文本框中
            text.insert("end", str(list(File_url_list).index(i) + 1) + ": " + i + "
    ")
        File_url_list = list(File_url_list)
        print(f"{File_url_list=}")
    
    
    def clear_file_list():
        global File_url_list
        File_url_list.clear()
        try:
            if img_label.winfo_exists():
                img_label.destroy()
        except NameError as e:
            print(f"clear_file_list提示:清空错误>>> {e}")
        try:
            if text.winfo_exists():
                text.destroy()
        except NameError as e:
            print(f"clear_file_list提示:清空错误错误>>> {e}")
    
    
    def get_entry1():       # 设置换行间距变量值
        global Row_space
        num = entry1.get()
        if num.isdigit():
            if int(num) > 0:
                Row_space = int(num)
        else:
            entry1.delete(0, "end")
            entry1.insert(0, 15)
            Row_space = 15
    
    
    def set_split_symbol():
        global Split_symbol
        Split_symbol = entry2.get()
        print(f"{Split_symbol=}")
    
    
    def do_change():
        if File_url_list:
            v.set("文字提取中,请稍后……")
            button_do.config(state="disable")        # 使按钮不可用。
            # ========================================
            def main():
                reader = make_reader()
                for i in File_url_list:
                    content = change_to_character(i, reader, row_space=Row_space, split_symbol=Split_symbol, save_dir=Save_dir)
                    read_text.delete(1.0, "end")
                    for c in content:  # i 为每一行的内容
                        c.sort(key=lambda x: x[0][0][0])  # 对每行的内容进行先后排序
                        for r in c:
                            # print(r)
                            read_text.insert("end", r[1] + Split_symbol)
                        read_text.insert("end", "
    ")
                v.set("文字提取结束。")
                button_do.config(state="normal")     # 恢复按钮可用。
            # ========================================
            t = Thread(target=main, daemon=True)
            t.start()
    
        else:
            v.set("请先选择图片!")
    
    
    def join_file():
        v.set("文件开始合并。")
        filst = list(Path(Save_dir).iterdir())      # 获取文件夹中所有的文本文件。
        with open(f"{Save_dir}/合并文件.txt", "w", encoding="utf8") as join_f:
            for f in filst:
                with open(f, "r", encoding="utf8") as r_f:
                    read_con = r_f.read()
                join_f.write(f.name+"
    "+read_con + "
    
    ")
        time.sleep(1)
        v.set("文件合并完毕。")
    
    
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    f_top = tk.Frame(root, height=65, width=1100, bd=1, relief="flat")  # "sunken" "raised","groove" 或 "ridge"
    f_top.pack_propagate(False)  # 如果不加这个参数,当Frame框架中加入部件时,会自动变成底层窗口,自身的特性会消失。
    f_top.pack(side="top", pady=5)
    
    f_left = tk.Frame(root, height=660, width=560, bd=1, relief="groove")
    f_left.pack_propagate(False)
    f_left.pack(side="left", padx=20)
    
    f_right = tk.Frame(root, height=660, width=560, bd=1, relief="groove")
    f_right.pack_propagate(False)
    f_right.pack(side="left", padx=20)
    
    read_text = tk.Text(f_right, spacing1=5, spacing3=5)
    read_text.pack(fill="both", expand=True)
    
    
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    button_choose_file = tk.Button(f_top, text="选择图片", command=choose_file)
    button_choose_file.pack(side="left", padx=10, ipadx=5)
    
    button_choose_file = tk.Button(f_top, text="选择文件夹", command=choose_dir)
    button_choose_file.pack(side="left", padx=10, ipadx=5)
    
    button_clear_file = tk.Button(f_top, text="清空选择", bg="#FFEF2F", command=clear_file_list)
    button_clear_file.pack(side="left", padx=5, ipadx=5)
    
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    f_row_content = tk.Frame(f_top, height=50, width=300, bg="#D1D4D0", relief="flat")  # "sunken" "raised","groove" 或 "ridge"
    f_row_content.pack_propagate(False)
    f_row_content.pack(side="left", padx=15)
    
    button_set_row_height = tk.Button(f_row_content, text="设置行间距", command=get_entry1)
    button_set_row_height.pack(side="left", ipadx=3, padx=3)
    
    entry1 = tk.Entry(f_row_content, font=("", 18), width=3)
    entry1.insert(0, 15)
    entry1.pack(padx=5, side="left")
    
    tk.Label(f_row_content, justify="left", text="填入像素值,设置换行间距。
    默认15个像素。").pack(side="left")
    
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    f_split = tk.Frame(f_top, height=50, width=215, bg="#D1D4D0", relief="flat")  # "sunken" "raised","groove" 或 "ridge"
    f_split.pack_propagate(False)
    f_split.pack(side="left", padx=4)
    
    button_split = tk.Button(f_split, text="设置分隔符", command=set_split_symbol)
    button_split.pack(side="left", ipadx=3, padx=3)
    
    entry2 = tk.Entry(f_split, font=("", 18), width=3)
    entry2.insert(0, " ")
    entry2.pack(padx=5, side="left")
    
    tk.Label(f_split, justify="left", text="默认一个空格").pack(side="left")
    
    # 《《《《《《《《《《《《《《《《《《《《《《  提取 合并文件  》》》》》》》》》》》》》》》》》》》》》》》》》
    button_do = tk.Button(f_top, text="开始提取", bg="#4AB0FF", command=do_change)
    button_do.pack(side="left", padx=10, ipadx=2)
    
    button_join = tk.Button(f_top, text="合并文件", command=join_file)
    button_join.pack(side="left", padx=5, ipadx=2)
    
    v = tk.StringVar()
    v.set("info……")
    tk.Label(f_top, bg="#2EBD1D", justify="left", textvariable=v).pack(side="left")
    
    # 《《《《《《《《《《《《《《《《《《《《《《  右键菜单  》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    def copy_text():
        read_text.event_generate("<>")
    
    menubar = tk.Menu(tearoff=False)
    # root["menu"] = menubar      # 没有把这个 菜单部件 加入到 root 窗口的菜单属性中,所以它不会在root窗口的顶部显示。
    menubar.add_command(label="复制", command=copy_text)
    
    def show_menu(event):
        """用 菜单部件 的 post 方法展示菜单"""
        menubar.post(event.x_root, event.y_root)
    
    read_text.bind("", show_menu)
    # 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    
    root.mainloop()
    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发 Python
    相关文章 更多
    python删除一个列表里的重复元素的方法与代码示例
    python删除一个列表里的重复元素的方法与代码示例

    学习如何使用Python高效删除列表中的重复元素。本文提供5种常见方法的代码示例,涵盖保持顺序与不保持顺序的场景,适合初学者和进阶开发者参考。

    Python写接口的步骤及完整代码示例
    Python写接口的步骤及完整代码示例

    详细讲解使用Python编写Web接口的核心步骤与完整代码示例,涵盖环境准备、路由定义、数据校验、服务启动及调试测试方法。

    codex怎么读取word文档及docx文件操作教程
    codex怎么读取word文档及docx文件操作教程

    介绍如何利用编程工具读取和解析Word文档(docx格式),涵盖基础文本提取代码示例、结构化数据处理逻辑,以及处理复杂排版时的技术局限与替代方案。

    怎么在Python中调用C语言代码提升性能
    怎么在Python中调用C语言代码提升性能

    详细讲解在Python中调用C语言代码提升性能的完整实践指南,包含原理分析、编译步骤、ctypes与CFFI具体实现、性能对比及常见陷阱。

    Python与C语言的性能差异及底层原理
    Python与C语言的性能差异及底层原理

    深入解析Python与C语言在执行效率上的数量级差异,从编译机制、变量内存布局、动态类型推断到GIL锁,系统阐明底层核心原理。

    Python和C语言哪个更适合编程入门
    Python和C语言哪个更适合编程入门

    客观对比Python与C语言在心智负担、底层机制与学习反馈上的核心差异,提供清晰有据的选择标准与切实的入门学习路径。

    selenium上传文件教程本地文件路径设置方法
    selenium上传文件教程本地文件路径设置方法

    深入解析Selenium处理文件上传的技术细节,涵盖标准HTML控件的路径直接注入法与非标准控件的系统级模拟方案,提供Windows与Linux环境下的路径设置最佳实践及常见异常排查。

    大数据开发语言选型:Java对比Scala与Python的优缺点及适用场景
    大数据开发语言选型:Java对比Scala与Python的优缺点及适用场景

    针对大数据工程场景,系统对比Java、Scala与Python在执行性能、生态结合、开发效率和维护成本上的核心差异,给出清晰的架构选型依据与落地方案。

    langgraph python guide中文入门教程详解
    langgraph python guide中文入门教程详解

    本篇LangGraph Python中文入门教程详解,系统梳理StateGraph状态机定义、节点编写、边与条件路由、检查点持久化(MemorySaver)等核心机制,提供从环境安装到完整循环智能体构建的代码示例与实操指导。

    Windows 安装 Python:从官方入口到虚拟环境的可复现实践
    Windows 安装 Python:从官方入口到虚拟环境的可复现实践

    面向后端开发者,基于 Python 官方文档说明如何在 Windows 上安装 Python、创建 venv 虚拟环境及使用 pip 管理依赖。包含可复制命令、验证方法及工具选择边界,避免常见配置误区。

    查看更多
    精品专题 更多
    装机必备
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows
    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程
    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    Mac软件 更多
    讯飞智作 网页版
    讯飞智作 网页版
    Windows/ macOS/ Linux

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版
    Windows/ macOS/ Linux

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    Aperty macOS版
    Aperty macOS版
    macOS

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    WINDOWS 更多
    讯飞智作 网页版
    讯飞智作 网页版
    Windows/ macOS/ Linux

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版
    Windows/ macOS/ Linux

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    PixTV 网页版
    PixTV 网页版
    Windows

    PixTV 是一款在浏览器中使用的 AI 视觉内容创作平台,提供图片、视频、音频、文字生成、AI 分镜、局部重绘、提示词反推和无限画布等工具,适合短剧、漫剧、广告及社交媒体内容制作。