pythonEasyOCR库实例用法介绍

更新时间:2023-05-18 20:23:48 阅读: 评论:0

pythonEasyOCR库实例⽤法介绍
说明
1、EasyOCR是⼀个⽤python编写的OCR三⽅库。可以在python中调⽤,⽤来识别图像中的⽂字,并输出为⽂本。
2、⽀持80多种语⾔的识别,识别精度⾼,甚⾄要超过PaddleOCR。
安装命令数独口诀
pip install easyocr
代码实现
import easyocr
#设置识别中英⽂两种语⾔
reader = easyocr.Reader(['ch_sim','en'], gpu = Fal) # need to run only once to load model into memory
result = adtext(r"d:\Desktop\4A34A16F-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:\Urs\⽤户\.EasyOCR\model
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 = ad()
result = adtext(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)
el:
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("\n")
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__(lf, master=None):
#        super().__init__(master)
#        lf.master = master
#        lf.pack()
#        # lf.img = tk.PhotoImage(file=r"C:\Urs\yanhy\Desktop\捕获22.PNG")
#        lf.create_widgets()
#
#    def create_widgets(lf):
#        lf.img = tk.PhotoImage(file=r"C:\Urs\yanhy\Desktop\捕获22.PNG")
#        lf.img_wig = tk.Label(lf, image=lf.img)
#        lf.img_wig.pack()
# 最外层窗⼝设置
root = tk.Tk()
root.title('图⽚⽂字识别程序联系:')
window_x = root.winfo_screenwidth()
window_y = root.winfo_screenheight()
WIDTH = 1200
HEIGHT = 750
x = (window_x - WIDTH) / 2  # ⽔平居中
y = (window_y - HEIGHT) / 3  # 垂直偏上
# 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》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
el:
Path.mkdir(Save_dir)
# 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》def test():
print(f'{Row_space=}')
def choo_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'choo_file提⽰:张图⽚导⼊错误>>> {e}')
try:
if img_label.winfo_exists():
img_label.destroy()
except NameError as e:
print(f'choo_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()
el:
print('导⼊的是⾮图像格式')
el:                                    # 多张图⽚导⼊,显⽰列表。
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)
el:
pass
File_url_list = t(File_url_list)
for i in list(File_url_list):      # 把⽂件写⼊到⽂本框中
text.inrt('end', str(list(File_url_list).index(i)+1) + ": " + i + "\n")
File_url_list = list(File_url_list)
print(f'{File_url_list=}')
def choo_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'choo_dir提⽰:多张图⽚导⼊错误>>> {e}')
try:
if text.winfo_exists():
text.destroy()
except NameError as e:
print(f'choo_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型转为字符串。        el:
pass
File_url_list = t(File_url_list)
for i in list(File_url_list):  # 把⽂件写⼊到⽂本框中
text.inrt('end', str(list(File_url_list).index(i) + 1) + ": " + i + "\n")
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():
12星座颜值排名text.destroy()
except NameError as e:
print(f'clear_file_list提⽰:清空错误错误>>> {e}')
def get_entry1():      # 设置换⾏间距变量值
global Row_space
num = ()
if num.isdigit():
if int(num) > 0:
Row_space = int(num)
el:
entry1.delete(0, "end")
蜂鸟的特点entry1.inrt(0, 15)
Row_space = 15
def t_split_symbol():
global Split_symbol
Split_symbol = ()
print(f'{Split_symbol=}')
def do_change():
if File_url_list:
v.t("⽂字提取中,请稍后……")
fig(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.inrt('end', r[1] + Split_symbol)
read_text.inrt('end', "\n")
v.t("⽂字提取结束。")
fig(state='normal')    # 恢复按钮可⽤。
# ========================================
t = Thread(target=main, daemon=True)
t.start()
el:
v.t("请先选择图⽚!")
def join_file():
v.t("⽂件开始合并。")
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 = ad()
join_f.write(f.name+'\n'+read_con + '\n\n')
time.sleep(1)
v.t("⽂件合并完毕。")
# 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
f_top = tk.Frame(root, height=65, width=1100, bd=1, relief="flat")  # "sunken" "raid","groove" 或 "ridge"
f_top.pack_propagate(Fal)  # 如果不加这个参数,当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(Fal)
f_left.pack(side='left', padx=20)
f_right = tk.Frame(root, height=660, width=560, bd=1, relief="groove")
f_right.pack_propagate(Fal)
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_choo_file = tk.Button(f_top, text='选择图⽚', command=choo_file)
button_choo_file.pack(side='left', padx=10, ipadx=5)
button_choo_file = tk.Button(f_top, text='选择⽂件夹', command=choo_dir)
button_choo_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" "raid","groove" 或 "ridge"
f_row_content.pack_propagate(Fal)
f_row_content.pack(side='left', padx=15)
button_t_row_height = tk.Button(f_row_content, text='设置⾏间距', command=get_entry1)
button_t_row_height.pack(side='left', ipadx=3, padx=3)
entry1 = tk.Entry(f_row_content, font=('', 18), width=3)
entry1.inrt(0, 15)
entry1.pack(padx=5, side='left')
tk.Label(f_row_content, justify='left', text='填⼊像素值,设置换⾏间距。\n默认15个像素。').pack(side='left')
# 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》f_split = tk.Frame(f_top, height=50, width=215, bg="#D1D4D0", relief="flat")  # "sunken" "raid","groove" 或 "ridge"
f_split.pack_propagate(Fal)
f_split.pack(side='left', padx=4)
button_split = tk.Button(f_split, text='设置分隔符', command=t_split_symbol)
button_split.pack(side='left', ipadx=3, padx=3)
entry2 = tk.Entry(f_split, font=('', 18), width=3)作风上
entry2.inrt(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.t('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=Fal)
# 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库实例⽤法介绍的⽂章就介绍到这了,更多相关python EasyOCR库是什么内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

本文发布于:2023-05-18 20:23:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/914081.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:识别   菜单   间距   提取   部件   默认
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图