基于飞浆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人工智能+2 天前
应用银行卡识别技术,构建更安全、便捷的数字身份认证与支付生态
人工智能·ocr·银行卡识别
deephub2 天前
Dots.ocr:告别复杂多模块架构,1.7B参数单一模型统一处理所有OCR任务22
人工智能·深度学习·神经网络·ocr
ccut 第一混3 天前
c#联合Halcon进行OCR字符识别(含halcon-25.05 百度网盘)
c#·ocr·halcon
R-G-B6 天前
【04】OpenCV C++实战篇——实战:发票精准定位,提取指定单元格数据。(倾角计算、旋转矫正、产品定位、目标定位、OCR文字提取)
c++·opencv·ocr·发票精准定位·提取指定单元格数据·倾角计算·旋转矫正
EkihzniY6 天前
单层 PDF 与双层 PDF:一字之差,功能大不同
pdf·ocr
郭庆汝10 天前
本地服务器端部署基于大模型的通用OCR项目——dots.ocr
ocr
EkihzniY11 天前
OCR 精准识别验讫章:让登记与校验更智能
ocr
CodeCraft Studio11 天前
使用 Aspose.OCR 将图像文本转换为可编辑文本
java·人工智能·python·ocr·.net·aspose·ocr工具
TextIn智能文档云平台12 天前
当文档包含图文混排表格时,如何结合大模型(如DeepSeek-VL)和OCR提取数据
人工智能·ocr·大模型文档应用
DisonTangor13 天前
小红书开源dots.ocr:单一视觉语言模型中的多语言文档布局解析
语言模型·开源·ocr