本文介绍将PP-HumanSeg-Lite轻量级人像分割模型部署到树莓派的流程。先克隆PaddleSeg仓库、安装相关工具并下载预训练模型,接着导出静态图模型并转为ONNX格式,最后编写预测代码。将相关文件夹打包至树莓派,即可运行实现实时人像分割。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

轻量级人像分割模型PP-HumanSeg模型树莓派部署
人像分割是图像分割领域的高频应用,PaddleSeg推出在大规模人像数据上训练的人像分割系列模型PP-HumanSeg,包括超轻量级模型PP-HumanSeg-Lite,满足在服务端、移动端、Web端多种使用场景的需求。本项目将PP-HumanSeg-Lite模型转为onnx并部署到树莓派,实现人像抠图效果。效果如下图所示(这里只露个半脸):
一、介绍
本项目将PaddleSeg的轻量级人像分割模型转换为onnx,将其部署到树莓派实现实时人像分割。树莓派环境如下:
硬件:
树莓派4B
摄像头一个
软件
Ubuntu Desktop 21.10
onnxruntime
opencv-python
二、具体流程
1、克隆paddleseg仓库
In [ ]
# step 1: git clone %cd ~/!git clone https://gitee.com/PaddlePaddle/PaddleSeg.git
2、安装paddleseg
In [ ]
# step 2: install paddleseg!pip install paddleseg
3、下载预训练模型
In [ ]
# step 3: 下载预训练模型%cd ~/PaddleSeg/contrib/PP-HumanSeg!python pretrained_model/download_pretrained_model.py
4、导出静态图模型
需要加上input_shape,本项目部署的模型是PP-HumanSeg-Lite,该模型的输入图片大小是192×192,如果要部署其他模型,需要更改model_path和config路径,。
智谱AI开放平台
智谱AI大模型开放平台-新一代国产自主通用AI开放平台
85 查看详情
In [ ]
# step 4: 导出静态图模型%cd ~/PaddleSeg/contrib/PP-HumanSeg!python ../../export.py --config configs/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely.yml --model_path pretrained_model/fcn_hrnetw18_small_v1_humanseg_192x192/model.pdparams --save_dir export_model/fcn_hrnetw18_small_v1_humanseg_192x192 --with_softmax --input_shape 1 3 192 192
5、onnx转换
In [ ]
# step 5:转为onnx模型# ① 安装paddle2onnx!pip install paddle2onnx
In [ ]
# ② 转换为onnx%cd ~/PaddleSeg/contrib/PP-HumanSeg! paddle2onnx --model_dir ./export_model/fcn_hrnetw18_small_v1_humanseg_192x192/ --model_filename model.pdmodel --params_filename model.pdiparams --save_file onnx_model/model.onnx --opset_version 12
6、移动模型路径
模型路径是:/home/aistudio/pp_humanseg_deploy,文件夹目录结构如下:
|-onnx_model
|—model.onnx
|-predict.py
In [ ]
# step 6: 转移模型路径%cd ~/!mkdir pp_humanseg_deploy%cd ~/pp_humanseg_deploy/!mkdir onnx_model!cp ~/PaddleSeg/contrib/PP-HumanSeg/onnx_model/model.onnx ~/pp_humanseg_deploy/onnx_model
7、预测代码
创建/home/aistudio/pp_humanseg_deploy/predict.py,将以下内容放进predict.py。
import cv2import numpy as npimport onnxruntime as rtdef normalize(im, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]): im = im.astype(np.float32, copy=False) / 255.0 im -= mean im /= std return imdef resize(im, target_size=608, interp=cv2.INTER_LINEAR): if isinstance(target_size, list) or isinstance(target_size, tuple): w = target_size[0] h = target_size[1] else: w = target_size h = target_size im = cv2.resize(im, (w, h), interpolation=interp) return imdef preprocess(image, target_size=(192, 192)): image = normalize(image) image = resize(image, target_size=target_size) image = np.transpose(image, [2, 0, 1]) image = image[None, :, :, :] return imagedef display_masked_image(mask, image, color_map=[255, 0, 0], weight=0.6): mask = mask > 0 c1 = np.zeros(shape=mask.shape, dtype='uint8') c2 = np.zeros(shape=mask.shape, dtype='uint8') c3 = np.zeros(shape=mask.shape, dtype='uint8') pseudo_img = np.dstack((c1, c2, c3)) for i in range(3): pseudo_img[:, :, i][mask] = color_map[i] vis_result = cv2.addWeighted(image, weight, pseudo_img, 1 - weight, 0) return vis_resultonnx_model_path = 'onnx_model/model.onnx'sess = rt.InferenceSession(onnx_model_path)input_name = sess.get_inputs()[0].namelabel_name = sess.get_outputs()[0].nametarget_size = (192, 192)cap_video = cv2.VideoCapture(0)if not cap_video.isOpened(): raise IOError("Error opening video stream or file.")while cap_video.isOpened(): ret, raw_frame = cap_video.read() pre_shape = raw_frame.shape[0:2][::-1] if ret: frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGRA2RGB) frame = preprocess(frame, target_size) pred = sess.run( [label_name], {input_name: frame.astype(np.float32)} )[0] pred = pred[0] raw_frame = resize(raw_frame, target_size) image = display_masked_image(pred, raw_frame) image = resize(image, target_size=pre_shape) cv2.imshow('HumanSegmentation', image) if cv2.waitKey(1) & 0xFF == ord('q'): break else: breakcap_video.release()
三、树莓派运行实例
将/home/aistudio/pp_humanseg_deploy文件夹打包,放入树莓派环境运行predict.py,效果如下:
以上就是轻量级人像分割模型PP-HumanSeg树莓派部署的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/738994.html
微信扫一扫
支付宝扫一扫