青岛建站方案,龙元建设网站,全国代运营最好的公司,wordpress附件到ossOCR的文本框box及JSON数据保存
需求说明
一、借助飞浆框出OCR识别的文本框
二、以圆圈形式标出每个框的中心点位置
三、以JSON及文本格式保存OCR识别的文本
四、以文本格式保存必要的文本信息
解决方法
一、文本的坐标来自飞浆的COR识别 二、借助paddleocr的draw_ocr画出…OCR的文本框box及JSON数据保存
需求说明
一、借助飞浆框出OCR识别的文本框
二、以圆圈形式标出每个框的中心点位置
三、以JSON及文本格式保存OCR识别的文本
四、以文本格式保存必要的文本信息
解决方法
一、文本的坐标来自飞浆的COR识别 二、借助paddleocr的draw_ocr画出文本的外框 三、中心点由坐标的左上和右下均值得出 四、借助cv2.circle画出中心点
完整代码
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, detTrue)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_pathrC:\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, encodingutf8) as fw:json.dump(fields, fw)with open(dst_path, r, encodingutf8) as fr:data_obj json.load(fr)valfor item in data_obj:left_topitem[box][0]right_bottomitem[box][2]txt item[text][0]center_x(left_top[0]right_bottom[0])/2center_y (left_top[1] right_bottom[1]) / 2print(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, encodingutf-8) as f:valstr(left_top) \t str(right_bottom) \t txt \t str(center_x) \t str(center_y)\tstr(int(midimg.shape[1]/2))\tstr(int(midimg.shape[0]/2)) \nf.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(rF:/292.jpg,rF:/output/)执行结果
一、中心点及文本框 二、JSON格式示例数据 三、文本格式示例数据