RK3588 AI 应用开发 (ResNet50V2-关键点检测)【玩转华为云】
【摘要】 本章介绍了基于 RK3588 的 ResNet50V2 关键点检测应用开发全流程,包括模型训练与转换、Gradio 界面设计、推理代码实现、批量预测处理及 Flask 服务部署,完整实现了从模型到端到端应用的落地。
RK3588 AI 应用开发 (ResNet50V2-关键点检测)
一、模型训练与转换
ResNet50V2 是改进版的深度卷积神经网络,基于 ResNet 架构发展而来。它采用前置激活(将 BN 和 ReLU 移至卷积前)与身份映射,优化了信息传播和模型训练性能。作为 50 层深度的网络,ResNet50V2 广泛应用于图像分类、目标检测等任务,支持迁移学习,适合快速适配新数据集,具有良好的泛化能力和较高准确率。
模型的训练与转换教程已经开放在AI Gallery中,其中包含训练数据、训练代码、模型转换脚本。
在ModelArts的Notebook环境中训练后,再转换成对应平台的模型格式:onnx格式可以用在Windows设备上,RK系列设备上需要转换为rknn格式。
二、应用开发
1. 开发 Gradio 界面
import cv2
import json
import base64
import requests
import numpy as np
import gradio as gr
def test_image(image_path):
try:
image_bgr = cv2.imread(image_path)
image_string = cv2.imencode('.jpg', image_bgr)[1].tobytes()
image_base64 = base64.b64encode(image_string).decode('utf-8')
params = {"image_base64": image_base64}
response = requests.post(f'http://{ip}:{port}{url}', data=json.dumps(params),
headers={"Content-Type": "application/json"})
if response.status_code == 200:
image_base64 = response.json().get("image_base64")
image_binary = base64.b64decode(image_base64)
image_array = np.frombuffer(image_binary, dtype=np.uint8)
image_rgb = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
else:
image_rgb = None
except Exception as e:
return None
else:
return image_rgb
if __name__ == "__main__":
port = 8000
ip = "127.0.0.1"
url = "/v1/ResNet50V2"
demo = gr.Interface(fn=test_image, inputs=gr.Image(type="filepath"), outputs=["image"], title="ResNet50V2 猫脸关键点检测")
demo.launch(share=False, server_port=3000)
/home/orangepi/miniconda3/envs/python-3.10.10/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
* Running on local URL: http://127.0.0.1:3000
* To create a public link, set `share=True` in `launch()`.
2. 编写推理代码
class ResNet50V2:
def __init__(self, model_path):
self.rknn_lite = RKNNLite()
self.rknn_lite.load_rknn(model_path)
self.rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0_1_2)
def preprocess(self, image):
image = image[:, :, ::-1]
image = cv2.resize(image, (224, 224))
return np.expand_dims(image, axis=0)
def rknn_infer(self, data):
outputs = self.rknn_lite.inference(inputs=[data])
return outputs[0]
def post_process(self, pred):
feat = pred.squeeze().reshape(-1, 2)
return feat
def predict(self, image):
# 图像预处理
data = self.preprocess(image)
# 模型推理
pred = self.rknn_infer(data)
# 模型后处理
keypoints = self.post_process(pred)
# 绘制关键点检测结果
h, w, _ = image.shape
for x, y in keypoints:
cv2.circle(image, (int(x * w), int(y * h)), 5, (0, 255, 0), -1)
return image[..., ::-1]
def release(self):
self.rknn_lite.release()
3. 图片批量预测
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from rknnlite.api import RKNNLite
model = ResNet50V2('model/ResNet50V2.rknn')
for image in os.listdir("image"):
image = cv2.imread(os.path.join("image", image))
image = model.predict(image)
plt.imshow(image)
plt.axis('off')
plt.show()
model.release()
4. 创建 Flask 服务
import cv2
import base64
import numpy as np
from rknnlite.api import RKNNLite
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/v1/ResNet50V2', methods=['POST'])
def inference():
data = request.get_json()
image_base64 = data.get("image_base64")
image_binary = base64.b64decode(image_base64)
image_array = np.frombuffer(image_binary, dtype=np.uint8)
image_bgr = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
image_rgb = model.predict(image_bgr)
image_string = cv2.imencode('.jpg', image_rgb)[1].tobytes()
image_base64 = base64.b64encode(image_string).decode('utf-8')
return jsonify({
"image_base64": image_base64
}), 200
if __name__ == '__main__':
model = ResNet50V2('model/ResNet50V2.rknn')
app.run(host='0.0.0.0', port=8000)
model.release()
W rknn-toolkit-lite2 version: 2.3.2
* Serving Flask app '__main__'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://192.168.3.50:8000
Press CTRL+C to quit
127.0.0.1 - - [02/May/2025 02:13:40] "POST /v1/ResNet50V2 HTTP/1.1" 200 -
127.0.0.1 - - [02/May/2025 02:13:46] "POST /v1/ResNet50V2 HTTP/1.1" 200 -
5. 上传图片预测
三、小结
本章介绍了基于 RK3588 的 ResNet50V2 关键点检测应用开发全流程,包括模型训练与转换、Gradio 界面设计、推理代码实现、批量预测处理及 Flask 服务部署,完整实现了从模型到端到端应用的落地。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)