python中ocr图片文字识别样例(一)

一、使用easyocr安装依赖

复制代码
pip install easyocr
pip install opencv-python-headless  # 处理图像

二、具体实现,此处有个缺陷,大家可以尝试解决下,识别的文字打印结果没问题,但是图片识别出现乱码:

2.1 具体识别的图片
2.2 代码实现:

我有必要说下这块reader = easyocr.Reader('ch_sim', 'en', model_storage_directory='./models', download_enabled=True, gpu=False) ,这块也可以这么写:

reader = easyocr.Reader('ch_sim', 'en',gpu=False) 这么写就会每次都下载模型,所以我把模型下载到指定地址,并且如果有gpu则对应gpu的参数可调整成true

复制代码
# -*- coding: utf-8 -*-
import easyocr
import cv2
import matplotlib.pyplot as plt


# 初始化 OCR reader (这里选择中文和英文)
reader = easyocr.Reader(['ch_sim', 'en'], model_storage_directory='./models', download_enabled=True, gpu=False) 

# 读取图像
image_path = 'read_image.png'  # 替换成你的图片路径
image = cv2.imread(image_path)

# 识别图像中的文字
result = reader.readtext(image_path)

# 输出识别的结果
for (bbox, text, prob) in result:
    print(f"识别结果: {text}, 置信度: {prob:.4f}")

# # 显示识别框和文字
for (bbox, text, prob) in result:
    # bbox 是一个包含四个坐标点的数组,定义了文本框
    top_left = tuple(map(int, bbox[0]))
    bottom_right = tuple(map(int, bbox[2]))

    # 在图像上画出识别结果
    cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
    cv2.putText(image, text, top_left, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

# 显示图片
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
相关推荐
花酒锄作田8 小时前
Pydantic校验配置文件
python
hboot8 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi19 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi20 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽20 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python