【chineseocr_lite】OCR结果原位置输出

直接上效果图:

原来效果:

字体下载路径: https://github.com/sonatype/maven-guide-zh/raw/master/content-zh/src/main/resources/fonts/simsun.ttc

model.py中核心代码:

def cv2ImgAddText(img,result, text, textColor=(0, 255, 0), textSize=20):
    if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype(
        "font/simsun.ttc", textSize, encoding="utf-8")
    # 绘制文本
    for one in result:
        draw.text((int(one['cx']), int(one['cy'])), one['text'], textColor, font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)


def text_predict():

    # img = cv2.imread(imgpath)
    img = cv2.imread('./test_imgs/train-demo.jpg')
    preds, boxes_list, rects_re, t = text_handle.predict(img, long_size=pse_long_size)


    img2 = draw_bbox(img, boxes_list, color=(0, 255, 0))
    cv2.imwrite("./draw.png", img2)
    #cv2.show(img2)
    result = crnnRec(np.array(img), rects_re)

    # 调用cv.putText()添加文字
    AddText = img.copy()
    AddText.fill(255)
    AddText=cv2ImgAddText(AddText,result,(0, 0 , 139), 40)
     
    # 将原图片和添加文字后的图片拼接起来
    res = np.hstack([img, AddText])
    cv2.imwrite("./result.png", res)
    # 显示拼接后的图片
    #cv2.imshow('text', res)
    #cv2.waitKey()
    #cv2.destroyAllWindows()
    return result

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值