opencv hand openpose

使用opencv c++ 来调用caffemodel

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

定义手指关节点

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;

代码清单

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;
}
相关推荐
龙的爹233310 分钟前
论文翻译 | The Capacity for Moral Self-Correction in Large Language Models
人工智能·深度学习·算法·机器学习·语言模型·自然语言处理·prompt
python_知世39 分钟前
2024年中国金融大模型产业发展洞察报告(附完整PDF下载)
人工智能·自然语言处理·金融·llm·计算机技术·大模型微调·大模型研究报告
Fanstay9851 小时前
人工智能技术的应用前景及其对生活和工作方式的影响
人工智能·生活
lunch( ̄︶ ̄)1 小时前
《AI 使生活更美好》
人工智能·生活
Hoper.J1 小时前
用两行命令快速搭建深度学习环境(Docker/torch2.5.1+cu118/命令行美化+插件),包含完整的 Docker 安装步骤
人工智能·深度学习·docker
Shaidou_Data1 小时前
信息技术引领未来:大数据治理的实践与挑战
大数据·人工智能·数据清洗·信息技术·数据治理技术
Elastic 中国社区官方博客1 小时前
开始使用 Elastic AI Assistant 进行可观察性和 Microsoft Azure OpenAI
大数据·人工智能·elasticsearch·microsoft·搜索引擎·全文检索·azure
qq_273900232 小时前
pytorch detach方法介绍
人工智能·pytorch·python
AI狂热爱好者2 小时前
A3超级计算机虚拟机,为大型语言模型LLM和AIGC提供强大算力支持
服务器·人工智能·ai·gpu算力
边缘计算社区2 小时前
推理计算:GPT-o1 和 AI 治理
人工智能·gpt