基于飞浆OCR的文本框box及坐标中心点检测JSON格式保存文本

OCR的文本框box及JSON数据保存

需求说明

一、借助飞浆框出OCR识别的文本框

二、以圆圈形式标出每个框的中心点位置

三、以JSON及文本格式保存OCR识别的文本

四、以文本格式保存必要的文本信息

解决方法

一、文本的坐标来自飞浆的COR识别

二、借助paddleocr的draw_ocr画出文本的外框

三、中心点由坐标的左上和右下均值得出

四、借助cv2.circle画出中心点

完整代码

python 复制代码
import os
from PIL import Image
from paddleocr import PaddleOCR, draw_ocr
import cv2
def ocr_parse(img,parsePath):
    ocr = PaddleOCR()
    result = ocr.ocr(img, det=True)
    img_name = img.split('/')[-1].replace(".jpg", '')
    image = Image.open(img).convert('RGB')

    boxes = [line[0] for line in result[0]]
    im_show = draw_ocr(image, boxes, font_path=r'C:\Windows\Fonts\simfang.ttf')
    im_show = Image.fromarray(im_show)
    im_show.save( parsePath + img_name+"_V3.jpg")

    midimg = cv2.imread(parsePath + img_name+"_V3.jpg", 1)

    fields = []
    for field in result:
        for index, value in enumerate(field):
            fields.append({"box": field[index][0], "text": field[index][1]})

    dst_path = os.path.join(parsePath, img_name+'.json')
    with open(dst_path, 'w', encoding='utf8') as fw:
        json.dump(fields, fw)

    with open(dst_path, 'r', encoding='utf8') as fr:
        data_obj = json.load(fr)
        val=''
        for item in data_obj:
            left_top=item['box'][0]
            right_bottom=item['box'][2]
            txt = item['text'][0]
            center_x=(left_top[0]+right_bottom[0])/2
            center_y = (left_top[1] + right_bottom[1]) / 2
            print(left_top,'\t', right_bottom,'\t', txt,'\t',center_x,'\t',center_y)

            cv2.circle(midimg, (int(center_x),int(center_y)), 8, (0, 0, 255), 2)

            with open(os.path.join(parsePath, img_name+'.txt'), 'w', encoding='utf-8') as f:
                val+=str(left_top) + '\t' + str(right_bottom) + '\t' + txt + '\t' + str(center_x) + '\t' + str(center_y)+"\t"+str(int(midimg.shape[1]/2))+"\t"+str(int(midimg.shape[0]/2)) +"\n"
                f.write(val)
                f.close()
    cv2.circle(midimg, (int(midimg.shape[1]/2), int(midimg.shape[0]/2)), 16, (0, 255, 0), 2)
    cv2.imwrite(os.path.join(parsePath, img_name+'_V3.jpg'), midimg)

if __name__ == '__main__':
    ocr_parse(r'F:/292.jpg',r'F:/output/')

执行结果

一、中心点及文本框:

二、JSON格式示例数据:

三、文本格式示例数据

相关推荐
AI人工智能+17 小时前
不动产权证书识别技术:融合了计算机视觉、自然语言处理(NLP)和人工智能的深度技术栈
人工智能·计算机视觉·语言模型·ocr·不动产权证书识别
Maydaycxc20 小时前
跨境电商多账号自动化:RPA对接指纹浏览器与OCR识图实战
自动化·ocr·rpa
Miss roro21 小时前
法律文书信息自动提取:OCR识别与AI技术在案件管理中的应用
人工智能·ocr·法律科技·律所管理系统·案件管理系统
Fleshy数模21 小时前
基于机器视觉的工业产品型号识别与报警系统实现
python·ocr·pyqt
开开心心就好1 天前
带OCR识别的电子发票打印工具
运维·javascript·科技·游戏·青少年编程·ocr·powerpoint
weixin_408099671 天前
【实战】用易语言 + OCR API 打造自动文字识别工具(通用文字识别/截图识别,附完整源码)
ocr·文字识别·api调用·api开发·易语言·截图识别·石榴智能
weixin_307779132 天前
OCR图片文本提取代码
图像处理·python·opencv·自动化·ocr
AI人工智能+2 天前
基于OCR与深度学习的发票识别技术,重构报销系统效率
计算机视觉·自然语言处理·ocr·发票识别
小成Coder2 天前
【Jack实战】如何用 Core Vision Kit 给旅行票据做端侧 OCR 识别
华为·ocr·harmonyos·鸿蒙
AI人工智能+3 天前
机动车登记证识别技术通过计算机视觉与深度学习实现证件信息自动化提取,显著提升车辆管理效率
深度学习·计算机视觉·自然语言处理·ocr·机动车登记证识别