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')
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)