基于飞浆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格式示例数据:

三、文本格式示例数据

相关推荐
Sinosecu-OCR21 小时前
文通OCR技术深度解析:自研算法如何搞定复杂场景识别?
算法·ocr·ocr识别系统
AI人工智能+1 天前
一种高效、精准的不动产权证书智能识别技术,打通了物理世界与数字世界的信息壁垒
深度学习·计算机视觉·自然语言处理·ocr·不动产权证书识别
oradh2 天前
Oracle RAC OCR维护操作总结
数据库·oracle·ocr·rac ocr维护·ocr维护·vote disk
诸葛大钢铁2 天前
PDF无法复制文字怎么办?3种方法解除PDF复制限制并提取文字
pdf·ocr·pdf教程·pdf解除限制·pdf无法复制·pdf文字识别
AI人工智能+2 天前
学位证书识别技术通过计算机视觉、自然语言处理和大模型推理相结合,实现了高精度、高效率的证书数字化处理
深度学习·计算机视觉·自然语言处理·ocr·学位证书识别
AI人工智能+2 天前
一种基于深度学习技术的高精度医疗机构执业许可证识别系统,构建了一套基于深度神经网络的端到端智能识别系统,为医疗行业提
深度学习·ocr·医疗机构执业许可证识别
'pi%'3 天前
多 Agent 协同方案实践:基于 LangGraph 搭建能源领域智能调度工作流
人工智能·爬虫·microsoft·langchain·ocr·能源
求真学习3 天前
腾讯优图Youtu-Parsing开源,无损并行解码让表格解析提速26倍,OmniDocBench 93.22分
ocr·pdf解析·腾讯·文档解析·youtu-parsing
翔云 OCR API4 天前
文档 OCR 识别API,智能提取图文信息赋能企业数字化转型
ocr
'pi%'4 天前
LangGraph 多智能体实战:搭建电力政策跟踪 Agent,实现网页爬虫、OCR 与 PDF 文档自动化解析
爬虫·pdf·ocr