用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/MIUAV/vibe-coding-ros2 --skill dnn-inference命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | dnn-inference |
| description | OpenCV4 DNN 模块技能 - 神经网络推理、模型加载、ONNX、TFLite、Darknet 支持 |
| user-invocable | true |
| argument-hint | dnn OR 深度学习推理 OR onnx OR tflite OR tensorflow OR darknet |
OpenCV DNN 模块深度学习推理完整指南
当需要以下帮助时使用此技能:
| 框架 | OpenCV 支持 | 扩展名 |
|---|---|---|
| ONNX | ✓ | .onnx |
| TensorFlow | ✓ | .pb, .tflite |
| Caffe | ✓ | .caffemodel, .prototxt |
| Darknet | ✓ | .weights, .cfg |
| Torch | ✓ | .t7 |
| OpenVINO | ✓ | .xml, .bin |
import cv2
import numpy as np
# 加载模型
net = cv2.dnn.readNetFromONNX('model.onnx')
# 或 TensorFlow
net = cv2.dnn.readNetFromTensorflow('model.pb')
# 或 Darknet
net = cv2.dnn.readNetFromDarknet('model.weights', 'model.cfg')
# 设置后端和目标(使用 GPU)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# 图像预处理
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416),
swapRB=True, crop=False)
# 推理
net.setInput(blob)
output = net.forward()
# 批量图像预处理
images = [cv2.imread(f) for f in image_files]
blob = cv2.dnn.blobFromImages(images, 1/255.0, (416, 416),
swapRB=True, crop=False)
# 批量推理
net.setInput(blob)
outputs = net.forward()
# 处理每个输出
for i, output in enumerate(outputs):
# output shape: [batch, classes, detections, 5+classes]
process_detection(output, images[i])
def postprocess_yolo(output, img_shape, conf_threshold=0.5, nms_threshold=0.4):
h, w = img_shape[:2]
boxes, confidences, class_ids = [], [], []
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
cx, cy, bw, bh = detection[:4]
x = int((cx - bw/2) * w)
y = int((cy - bh/2) * h)
width = int(bw * w)
height = int(bh * h)
boxes.append([x, y, width, height])
confidences.append(float(confidence))
class_ids.append(class_id)
# NMS
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
return [(boxes[i], confidences[i], class_ids[i]) for i in indices.flatten()]
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>
using namespace cv::dnn;
// 加载模型
Net net = readNetFromONNX("model.onnx");
// Net net = readNetFromTensorflow("model.pb");
// Net net = readNetFromDarknet("model.weights", "model.cfg");
// 设置后端
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
// 预处理
Mat blob = blobFromImage(img, 1/255.0, Size(416, 416),
Scalar(), true, false);
// 推理
net.setInput(blob);
Mat output = net.forward();
// 后处理
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<Rect> boxes;
for (int i = 0; i < output.size[2]; i++) {
float confidence = output.at<float>(0, 0, i, 2);
if (confidence > 0.5) {
int classId = (int)output.at<float>(0, 0, i, 1);
int x = ()(output.<>(, , i, ) * img.cols);
y = ()(output.<>(, , i, ) * img.rows);
w = ()(output.<>(, , i, ) * img.cols);
h = ()(output.<>(, , i, ) * img.rows);
boxes.(x, y, w, h);
confidences.(confidence);
classIds.(classId);
}
}
std::vector<> indices;
(boxes, confidences, , , indices);
# CPU
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
# CUDA GPU
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# OpenVINO
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
模型准备:
预处理优化:
blobFromImages 批量处理swapRB=True 处理 RGB/BGR 转换推理优化:
内存管理:
Mat 对象避免频繁分配release() 释放中间结果