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

三、文本格式示例数据

相关推荐
沉到海底去吧Go16 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
撞南墙者1 天前
如何让AI自己检查全文?使用OCR和LLM实现自动“全文校订”(可DIY校订规则)
人工智能·ocr
沉到海底去吧Go2 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
TextIn智能文档云平台3 天前
从OCR到Document Parsing,AI时代的非结构化数据处理发生了什么改变?
人工智能·自然语言处理·ocr·pdf解析·textin·复杂文档解析
mulannanlu3 天前
免费开源Umi-OCR,离线使用,批量精准!
ocr
凌康ACG4 天前
易语言使用OCR
c++·yolo·c#·ocr·易语言
开开心心就好4 天前
小巧实用,Windows文件夹着色软件推荐
java·开发语言·前端·决策树·c#·ocr·动态规划
郑知鱼5 天前
【拥抱鸿蒙】HarmonyOS NEXT实现双路预览并识别文字
华为·ocr·harmonyos·鸿蒙·移动端·鸿蒙next·ohos
rick_grace7 天前
使用 pytesseract 构建一个简单 OCR demo
ocr
开开心心就好7 天前
高效合并 Excel 表格实用工具
开发语言·javascript·python·qt·r语言·ocr·excel