ddddocr 汉字位置识别

 一、ddddocr介绍

  • 安装 pip install ddddocr
  • star哲哥免费开源的识别项目GitHub - sml2h3/ddddocr: 带带弟弟 通用验证码识别OCR pypi版
import ddddocr
import requests
from io import BytesIO
from PIL import Image, ImageDraw

ocr1 = ddddocr.DdddOcr(show_ad=False)
ocr2 = ddddocr.DdddOcr(det=True, show_ad=False)
# url = "图片链接"
# response = requests.get(url).content

with open("图片路径", "rb")as fp:
    response = fp.read()

img = Image.open(BytesIO(response))  # 读取
canvas = ImageDraw.ImageDraw(img)  # 创建画布

res = ocr2.detection(response)
result = {}
for box in res:
    x1, y1, x2, y2 = box
    result[ocr1.classification(img.crop(box))] = [x1 + ((y1 - x1) // 2), x2 + ((y2 - x2) // 2)]  # 文字位置
    canvas.rectangle(box, outline='red', width=2)  # 画框
img.save("new_img.png")  # 保存
print(result)  # 识别结果

 

你可能感兴趣的:(日记,python,前端,开发语言)