使用easyocr识别图片文字(本地图片和网络图片)
from PIL import Image import requests from io import BytesIO import easyocr import numpy as np from pathlib import Path file_url = r'./img/1.jpg' # 需识别的图片 file_url = r"****7b00ea.jpg" split_symbol = ' ' # 默认空格为分隔符 row_space = 15 # 默认字符高度为15px,当识别出来的字符间距超过这个数值时会换行。 def make_reader(): # 将模型加载到内存中。模型文件地址 C:\Users\用户\.EasyOCR\model reader = easyocr.Reader(['ch_sim', 'en']) return reader def url_img_to_bytes(url): img = Image.open(BytesIO(requests.get( "url.jpg").content)) # 剪裁出右下角 #img = img.crop((586, 680, 800, 800)) img_io_stream = BytesIO() img.save(img_io_stream, format='PNG') img_bytes = img_io_stream.getvalue() return img_bytes def change_to_character(file_url, reader, split_symbol=' ', row_space=15, save_dir='.',local_img = True): if local_img: #本地图片 with open(file_url, "rb") as img: img_b = img.read() result = reader.readtext(img_b, detail=0) #result = reader.readtext(img_b, detail=0) # 只显示文字 else: #网络图片 img_bytes = url_img_to_bytes(file_url) result = reader.readtext(img_bytes) result.sort(key=lambda x: x[0][0][1]) # 按竖直方向,进行排序==>进行分行处理。 # 按行进行分组 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__": content = change_to_character(file_url, make_reader(),local_img = False)
注意本地需要先把模型保存到:
将模型加载到内存中。模型文件地址 C:\Users\你的用户\.EasyOCR\model