进行英文字母的识别,有一个文件用training文件夹代表训练集英文图片,test为测试集英文图片,test.txt存的是测试集对应的标识,zidian.txt为字典,training表示的是训练集的标识,用paddlepaddle框架和paddleocr的PP-OCRv3模型,写出完整的训练预测模型代码进行文本识别项目,写完代码后再编写一个python文件读取文件的。png文件进行文字识别
时间: 2023-05-31 20:01:44 浏览: 198
5700张人脸关键点识别数据集(含训练集,测试集,对应的关键点位置标签).zip
# 训练模型
import os
import paddle
import paddleocr
import cv2
# 定义数据集路径和字典路径
train_dir = "training"
test_dir = "test"
dict_file = "zidian.txt"
# 加载字典
with open(dict_file, encoding="utf-8") as f:
dict = [c.strip() for c in f]
# 定义数据增强器
train_transforms = paddleocr.transforms.Compose([
paddleocr.transforms.RandomContrast(0.3),
paddleocr.transforms.RandomBrightness(0.3)
])
# 定义训练集和测试集
train_dataset = paddleocr.datasets.OCRDataset(train_dir, dict, transform=train_transforms)
test_dataset = paddleocr.datasets.OCRDataset(test_dir, dict)
# 定义模型
model = paddleocr.OCRRecognizer(
config=paddleocr.ocr_config,
dict=dict
)
# 定义训练器
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
trainer = paddleocr.Trainer(model, optimizer)
# 开始训练
trainer.train(train_dataset, epochs=10, batch_size=32)
# 保存模型
model.save("pp-ocrv3")
# 测试模型
# 加载模型
model = paddleocr.OCRRecognizer(
config=paddleocr.ocr_config,
dict=dict
)
model.set_state_dict(paddle.load("pp-ocrv3"))
# 定义预测函数
def predict(image_path):
img = cv2.imread(image_path)
result = model.ocr(img)
return result
# 测试模型
with open("test.txt", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
image_path, label = line.strip().split(" ")
result = predict(os.path.join(test_dir, image_path))
print("预测结果:", result[0][0], "真实结果:", label)
# 读取文件识别
image_path = "example.png"
result = predict(image_path)
print("预测结果:", result[0][0])
阅读全文