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')


Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐