python EasyOCR库实例用法介绍
程序员文章站
2022-06-16 08:32:09
说明1、easyocr是一个用python编写的ocr三方库。可以在python中调用,用来识别图像中的文字,并输出为文本。2、支持80多种语言的识别,识别精度高,甚至要超过paddleocr。安装命...
说明
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:\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:\users\用户\.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 = 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("\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__(self, master=none): # super().__init__(master) # self.master = master # self.pack() # # self.img = tk.photoimage(file=r"c:\users\yanhy\desktop\捕获22.png") # self.create_widgets() # # def create_widgets(self): # self.img = tk.photoimage(file=r"c:\users\yanhy\desktop\捕获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 + "\n") 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 + "\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(): 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', "\n") 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+'\n'+read_con + '\n\n') 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='填入像素值,设置换行间距。\n默认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库实例用法介绍的文章就介绍到这了,更多相关python easyocr库是什么内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!