搜尋
首頁後端開發Python教學python EasyOCR函式庫如何使用

python EasyOCR函式庫如何使用

Jun 02, 2023 pm 07:10 PM
pythoneasyocr

說明

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("<<Copy>>")

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("<Button-3>", show_menu)
# 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

root.mainloop()

以上是python EasyOCR函式庫如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

您如何在python列表中訪問元素?您如何在python列表中訪問元素?Apr 26, 2025 am 12:03 AM

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Python的科學計算中如何使用陣列?Python的科學計算中如何使用陣列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何處理同一系統上的不同Python版本?您如何處理同一系統上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

與標準Python陣列相比,使用Numpy數組的一些優點是什麼?與標準Python陣列相比,使用Numpy數組的一些優點是什麼?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中