原文链接

PaddleOCR模型下载

OpenVINO2025支持直接加载paddle的模型。

Image

所以可以直接先从官网直接下载PaddleOCRv5.0的模型:

文本检测模型下载地址

# Download and unzip PP-OCRv5_server_det pre-trained modelhttps://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_det_infer.tar

文本识别模型下载地址

# Download and upzip PP-OCRv5_server_rec pre-trained modelhttps://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_rec_infer.tar

推理流程与SDK支持

OpenVINO的推理流程

Image

SDK支持直接加载模型并实现输入与输出层数据读取,代码如下:

core = Core()
det_model = core.read_model(model="./models/ch_PP-OCRv5_det_infer.pdmodel")
det_compiled_model = core.compile_model(model=det_model, device_name="CPU")
# Get input and output nodes for text detection.
det_input_layer = det_compiled_model.input(0)
det_output_layer = det_compiled_model.output(0)
rec_compiled_model = core.compile_model(model="./models/ch_PP-OCRv5_rec_infer.pdmodel", device_name="AUTO")
# Get input and output nodes.
rec_input_layer = rec_compiled_model.input(0)
rec_output_layer = rec_compiled_model.output(0)

代码演示

OpenVINO2025官方给出的代码依然依赖Paddle框架,没有做大完全部署解耦,所以我这里在官方的代码基础上做了修改,把预处理跟后处理还有decode输出放到一个单独的python文件中,取名为:

paddle_dettext_prepost_process.y

使用时候直接导入即可,这样就完全不需要依赖paddle相关的包,只需要依赖OpenVINO跟OpenCV即可。

import paddle_dettext_prepost_process as processing

推理OCR识别部分的代码如下:

start = time.time()
frame = cv.imread("D:/1725.jpg")
cv.imshow("input", frame)
scale = 1280 / max(frame.shape)
if scale < 1:
	frame = cv.resize(
		src=frame,
		dsize=None,
		fx=scale,
		fy=scale,
		interpolation=cv.INTER_AREA,
	)
# Preprocess the image for text detection.
test_image = image_preprocess(frame, 640)
# Measure processing time for text detection.
det_results = det_compiled_model([test_image])[det_output_layer]
# Postprocessing for Paddle Detection.
dt_boxes = post_processing_detection(frame, det_results)
# Preprocess detection results for recognition.
dt_boxes = processing.sorted_boxes(dt_boxes)
batch_num = 6
img_crop_list, img_num, indices = prep_for_rec(dt_boxes, frame)
# For storing recognition results, include two parts:
# txts are the recognized text results, scores are the recognition confidence level.
rec_res = [["", 0.0]] * img_num
txts = []
scores = []
for beg_img_no in range(0, img_num, batch_num):
# Recognition starts from here.
	norm_img_batch = batch_text_box(img_crop_list, img_num, indices, beg_img_no, batch_num)
# Run inference for text recognition.
	rec_results = rec_compiled_model([norm_img_batch])[rec_output_layer]
# Postprocessing recognition results.
	postprocess_op = processing.build_post_process(processing.postprocess_params)
	rec_result = postprocess_op(rec_results)
	for rno in range(len(rec_result)):
		rec_res[indices[beg_img_no + rno]] = rec_result[rno]
	if rec_res:
		txts = [rec_res[i][0] for i in range(len(rec_res))]
		scores = [rec_res[i][1] for i in range(len(rec_res))]
image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
boxes = dt_boxes
# Draw text recognition results beside the image.
draw_img = processing.draw_ocr_box_txt(image, boxes, txts, scores, drop_score=0.5)
# Visualize the PaddleOCR results.
end = time.time()
inf_end = end - start
fps = 1 / inf_end
fps_label = "FPS: %.2f" % fps
f_height, f_width = draw_img.shape[:2]
cv.putText(
	img=draw_img,
	text=fps_label,
	org=(20, 40),
	fontFace=cv.FONT_HERSHEY_COMPLEX,
	fontScale=f_width / 1000,
	color=(0, 0, 255),
	thickness=1,
	lineType=cv.LINE_AA,
)
draw_img = cv.cvtColor(draw_img, cv.COLOR_RGB2BGR)
cv.imshow("OpenVINO2025 PaddleOCR", draw_img)
cv.waitKey(0)
cv.destroyAllWindows()

运行测试结果如下(左侧是原图,右侧是识别结果):

Image

Logo

为开发者提供丰富的英特尔开发套件资源、创新技术、解决方案与行业活动。欢迎关注!

更多推荐