opencv hand openpose

使用opencv c++ 来调用caffemodel

使用opencv 得dnn 模块调用 caffemodel得程序,图片自己输入就行,不做过多得解释,看代码清单。

定义手指关节点

const int POSE_PAIRS202 =

{

{0,1}, {1,2}, {2,3}, {3,4}, // thumb

{0,5}, {5,6}, {6,7}, {7,8}, // index

{0,9}, {9,10}, {10,11}, {11,12}, // middle

{0,13}, {13,14}, {14,15}, {15,16}, // ring

{0,17}, {17,18}, {18,19}, {19,20} // small

};

string protoFile = "hand/pose_deploy.prototxt";

string weightsFile = "hand/pose_iter_102000.caffemodel";

int nPoints = 22;

代码清单

c 复制代码
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;
using namespace cv::dnn;
#ifdef _DEBUG
#pragma comment(lib,"opencv_world455d.lib")
#else
#pragma comment(lib,"opencv_world455.lib")
#endif

const int POSE_PAIRS[20][2] =
{
	{0,1}, {1,2}, {2,3}, {3,4},         // thumb
	{0,5}, {5,6}, {6,7}, {7,8},         // index
	{0,9}, {9,10}, {10,11}, {11,12},    // middle
	{0,13}, {13,14}, {14,15}, {15,16},  // ring
	{0,17}, {17,18}, {18,19}, {19,20}   // small
};

string protoFile = "hand/pose_deploy.prototxt";
string weightsFile = "hand/pose_iter_102000.caffemodel";

int nPoints = 22;

int main(int argc, char **argv)
{
	string imageFile = "qianbo2.jpg";
	// Take arguments from commmand line
	if (argc == 2)
	{
		imageFile = argv[1];
	}

	float thresh = 0.01;

	Mat frame = imread(imageFile);
	Mat frameCopy = frame.clone();
	int frameWidth = frame.cols;
	int frameHeight = frame.rows;

	float aspect_ratio = frameWidth / (float)frameHeight;
	int inHeight = 368;
	int inWidth = (int(aspect_ratio*inHeight) * 8) / 8;

	cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl;

	double t = (double)cv::getTickCount();
	Net net = readNetFromCaffe(protoFile, weightsFile);

	Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);

	net.setInput(inpBlob);

	Mat output = net.forward();

	int H = output.size[2];
	int W = output.size[3];

	// find the position of the body parts
	vector<Point> points(nPoints);
	for (int n = 0; n < nPoints; n++)
	{
		// Probability map of corresponding body's part.
		Mat probMap(H, W, CV_32F, output.ptr(0, n));
		resize(probMap, probMap, Size(frameWidth, frameHeight));

		Point maxLoc;
		double prob;
		minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
		if (prob > thresh)
		{
			circle(frameCopy, cv::Point((int)maxLoc.x, (int)maxLoc.y), 8, Scalar(0, 255, 255), -1);
			cv::putText(frameCopy, cv::format("%d", n), cv::Point((int)maxLoc.x, (int)maxLoc.y), cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0, 0, 255), 2);

		}
		points[n] = maxLoc;
	}

	int nPairs = sizeof(POSE_PAIRS) / sizeof(POSE_PAIRS[0]);

	for (int n = 0; n < nPairs; n++)
	{
		// lookup 2 connected body/hand parts
		Point2f partA = points[POSE_PAIRS[n][0]];
		Point2f partB = points[POSE_PAIRS[n][1]];

		if (partA.x <= 0 || partA.y <= 0 || partB.x <= 0 || partB.y <= 0)
			continue;

		line(frame, partA, partB, Scalar(0, 255, 255), 8);
		circle(frame, partA, 8, Scalar(0, 0, 255), -1);
		circle(frame, partB, 8, Scalar(0, 0, 255), -1);
	}

	t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
	cout << "Time Taken = " << t << endl;
	imshow("Keypoints", frameCopy);
	imshow("Skeleton", frame);
	imwrite("out.jpg", frame);

	waitKey();

	return 0;
}
相关推荐
进击的横打8 小时前
【人工智能】把经验沉淀成 Skill
人工智能
代码方舟8 小时前
PHP数据工程:利用天远学历信息高级版优化专家资源池合规体验
人工智能
飞哥数智坊8 小时前
ZCode 可以自己看微信小程序了
人工智能·ai编程
江润舟8 小时前
万物|炼器 从零手搓工业级旋转目标检测网络 · 卷3 —— 核心算子锻造(三)
人工智能·深度学习
桃西西呀9 小时前
你的 Agent 每判一次都要为大模型吐的字买单?-Laya模型帮你做判断
人工智能·llm·ai编程
品牌常新9 小时前
安防巡检机器人推荐:园区仓储楼宇怎么选
人工智能
释厄6239 小时前
多黑洞具现论·物理/生物/金融三域显化·虚拟宇宙黑洞·奇点=0 圆圈
开发语言·数据结构·人工智能·程序人生·安全威胁分析
具身AGI9 小时前
大脑之外还要一套脊髓,物理AI 交互具身 的下一关
人工智能
haliu9 小时前
【FHE】(十二):为什么我们把 OpenMP 换成了自研线程池
人工智能·嵌入式·c·fhe·推理引擎·c11·边缘推理·同态加密推理
瞬维AI9 小时前
RPA与AI智能体的跨平台自动化执行架构:从任务编排到异常处理
人工智能·自动化·rpa