电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

python EasyOCR庫實例用法介紹

瀏覽:143日期:2022-06-14 18:54:49
說明

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 memoryresult = reader.readtext(r'd:Desktop4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png', detail = 0)print(result)

實例擴展:

圖文提取的代碼

from pathlib import Pathimport easyocrfile_url = r’識別圖片.jpg’ # 需識別的圖片split_symbol = ’ ’ # 默認空格為分隔符row_space = 15 # 默認字符高度為15px,當識別出來的字符間距超過這個數值時會換行。def make_reader(): # 將模型加載到內存中。模型文件地址 C:Users用戶.EasyOCRmodel reader = easyocr.Reader([’ch_sim’, ’en’]) return readerdef 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 contentif __name__ == '__main__': change_to_character(file_url, make_reader())

UI 界面的代碼

import tkinter as tkfrom tkinter import filedialogfrom PIL import Image, ImageTkfrom pathlib import Pathfrom character import change_to_character, make_readerfrom threading import Threadimport 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 = 1200HEIGHT = 750x = (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 = 15File_url_list = []Img_type = [’.jpg’, ’.jpeg’, ’.png’, ’.gif’]Split_symbol = ’ ’ # 間隔符。Save_dir = Path.cwd().joinpath(’img_to_word’)if Save_dir.is_dir(): passelse: 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:passFile_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 = 15def 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 + ’nn’) 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庫是什么內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

相關文章:
主站蜘蛛池模板: 电液推杆生产厂家|电动推杆|液压推杆-扬州唯升机械有限公司 | 蔬菜配送公司|蔬菜配送中心|食材配送|饭堂配送|食堂配送-首宏公司 | 贵阳用友软件,贵州财务软件,贵阳ERP软件_贵州优智信息技术有限公司 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 升降机-高空作业车租赁-蜘蛛车-曲臂式伸缩臂剪叉式液压升降平台-脚手架-【普雷斯特公司厂家】 | 联系我们老街华纳娱乐公司官网19989979996(客服) | 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 液压油缸-液压缸厂家价格,液压站系统-山东国立液压制造有限公司 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 气力输送设备_料封泵_仓泵_散装机_气化板_压力释放阀-河南锐驰机械设备有限公司 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 苏州教学设备-化工教学设备-环境工程教学模型|同科教仪 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 分子蒸馏设备(短程分子蒸馏装置)_上海达丰仪器 | 波纹补偿器_不锈钢波纹补偿器_巩义市润达管道设备制造有限公司 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 馋嘴餐饮网_餐饮加盟店火爆好项目_餐饮连锁品牌加盟指南创业平台 | 上海质量认证办理中心| 全钢实验台,实验室工作台厂家-无锡市辰之航装饰材料有限公司 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 福建珂朗雅装饰材料有限公司「官方网站」 | 杭州火蝠电商_京东代运营_拼多多全托管代运营【天猫代运营】 | 步进电机_agv电机_伺服马达-伺服轮毂电机-和利时电机 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 |