Python人工智能-批量识别图片中的文字

该功能是基于 Python 实现的 ** 批量光学字符识别(OCR)** 能力,借助人工智能相关技术,自动遍历指定文件夹内的所有图片文件,提取图片中的印刷体(或手写体,视模型能力而定)文字内容,并输出规整的识别结果。
核心特性
批量处理能力:无需单张图片手动上传识别,可自动扫描目标文件夹(支持子文件夹)内的各类图片格式(JPG、PNG、BMP、TIFF 等)。
AI OCR 驱动:依托成熟的 OCR 技术 / 模型(如百度飞桨 PaddleOCR、Tesseract OCR、阿里云 OCR API 等),实现高精度文字提取,支持中文、英文等多语言识别。
结果规整输出:将每张图片的识别结果与图片信息关联,可保存为 TXT、Excel 等格式,方便后续查阅和整理。
兼容性强:支持处理常规图片、轻微模糊图片、倾斜图片(部分模型自带矫正功能),无需手动预处理图片。

# encoding=utf8

”’
Python人工智能-批量识别图片中的文字
”’

# 安装所需模块
# pip install protobuf==3.19.0 -i https://mirror.baidu.com/pypi/simple
# pip install paddlepaddle==2.3.1 -i https://mirror.baidu.com/pypi/simple
# pip install paddlehub==2.2.0 -i https://mirror.baidu.com/pypi/simple
# pip install opencv-python==4.6.0.66 -i https://mirror.baidu.com/pypi/simple
# pip install paddlenlp==2.5.2 -i https://mirror.baidu.com/pypi/simple
# pip install opencv-python==4.6.0.66 -i https://mirror.baidu.com/pypi/simple

# 导入模块
import paddlehub as hub
import cv2
import os

# 批量识别目录
imgpath = r’D:\Test\4-2-3′ # 注:图片名称不能中文

# 识别结果保存路径
txtpath = r’D:\Test\word.txt’

# 将数据累加并写入txt文档
def writefile(txtpath,strstr):
with open(txtpath, “a”, encoding= “utf-8”) as f:
f.write(strstr) # 写入文件
f.write(“\n\n”)

for file in os.listdir(imgpath):
# 要识别的图片路径
images = os.path.join(imgpath,file)
print(images)

ocr = hub.Module(name=”chinese_ocr_db_crnn_server”, enable_mkldnn=True)
result = ocr.recognize_text(images=[cv2.imread(images)])
for item in result[0][‘data’]:
print(‘———–‘, item[‘text’], ‘———–‘)
writefile(txtpath, item[‘text’])

原创文章,作者:管理员,如若转载,请注明出处:https://www.devcn.cn/50.html

(0)
管理员管理员
上一篇 2天前
下一篇 2天前

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注