python使用paddleocr 识别图片上的文本

from paddleocr import PaddleOCR, draw_ocr

# ocr = PaddleOCR(lang="ch", use_gpu="False") , lang="ch"
# ocr = PaddleOCR(use_angle_cls=True)  # 初始化 OCR
ocr = PaddleOCR()  # 初始化 OCR

image_path = 'F:\学习\测试用图\英文测试.png'  # 图片路径
# result = ocr.ocr(image_path, cls=True)  # 进行文字识别
result = ocr.ocr(image_path)  # 进行文字识别
print(result)

with open('wenzi.txt', 'a', encoding='utf-8') as file:
    #  遍历出文字识别的结果
    for line in result:
        for word in line:
            print(word)
            # 提取出识别数据中的文字元组
            text_line = word[-1]
            # 从文字元组中提取文字内容
            text = text_line[0]
            print('test:', text)
            file.write(text + '\n')

print("识别结果已保存到txt文件中")

from PIL import Image

image = Image.open(image_path).convert('RGB')
boxes = [detection[0] for line in result for detection in line]  # Nested loop added
txts = [detection[1][0] for line in result for detection in line]  # Nested loop added
scores = [detection[1][1] for line in result for detection in line]  # Nested loop added
im_show = draw_ocr(image, boxes, txts, scores)
im_show = Image.fromarray(im_show)
im_show.save('test_ocr.jpg')


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值