dlib实现人脸识别(一)生成描述文件和标签文件

#include <iostream>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/dnn.h>
#include <dlib/gui_widgets.h>
#include <dlib/clustering.h>
#include <dlib/string.h>
#include <dlib/image_io.h>
#if 0
#include <opencv2/opencv.hpp>
#endif
#include <vector>
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/image_processing/frontal_face_detector.h>

using namespace std;
using namespace dlib;
using namespace cv;

// ----------------------------------------------------------------------------------------

template <long num_filters, typename SUBNET> using con5d = con<num_filters, 5, 5, 2, 2, SUBNET>;
template <long num_filters, typename SUBNET> using con5 = con<num_filters, 5, 5, 1, 1, SUBNET>;

template <typename SUBNET> using downsampler = relu<affine<con5d<32, relu<affine<con5d<32, relu<affine<con5d<16, SUBNET>>>>>>>>>;
template <typename SUBNET> using rcon5 = relu<affine<con5<45, SUBNET>>>;

using net_type = loss_mmod<con<1, 9, 9, 1, 1, rcon5<rcon5<rcon5<downsampler<input_rgb_image_pyramid<pyramid_down<6>>>>>>>>;


template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = add_prev1<block<N, BN, 1, tag1<SUBNET>>>;

template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = add_prev2<avg_pool<2, 2, 2, 2, skip1<tag2<block<N, BN, 2, tag1<SUBNET>>>>>>;

template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN<con<N, 3, 3, 1, 1, relu<BN<con<N, 3, 3, stride, stride, SUBNET>>>>>;

template <int N, typename SUBNET> using ares = relu<residual<block, N, affine, SUBNET>>;
template <int N, typename SUBNET> using ares_down = relu<residual_down<block, N, affine, SUBNET>>;

template <typename SUBNET> using alevel0 = ares_down<256, SUBNET>;
template <typename SUBNET> using alevel1 = ares<256, ares<256, ares_down<256, SUBNET>>>;
template <typename SUBNET> using alevel2 = ares<128, ares<128, ares_down<128, SUBNET>>>;
template <typename SUBNET> using alevel3 = ares<64, ares<64, ares<64, ares_down<64, SUBNET>>>>;
template <typename SUBNET> using alevel4 = ares<32, ares<32, ares<32, SUBNET>>>;

using anet_type = loss_metric<fc_no_bias<128, avg_pool_everything<
	alevel0<
	alevel1<
	alevel2<
	alevel3<
	alevel4<
	max_pool<3, 3, 2, 2, relu<affine<con<32, 7, 7, 2, 2,
	input_rgb_image_sized<150>
	>>>>>>>>>>>>;

/*
matrix<rgb_pixel> img;
cv::Mat image = cv::imread(path);
array2d< bgr_pixel> arrimg(image.rows, image.cols);
dlib::assign_image(img, cv_image<rgb_pixel>(image));
*/

std::vector<matrix<rgb_pixel>> jitter_image(
	const matrix<rgb_pixel>& img
)
{
	// All this function does is make 100 copies of img, all slightly jittered by being
	// zoomed, rotated, and translated a little bit differently. They are also randomly
	// mirrored left to right.
	thread_local dlib::rand rnd;

	std::vector<matrix<rgb_pixel>> crops;
	for (int i = 0; i < 100; ++i)
		crops.push_back(jitter_image(img, rnd));

	return crops;
}

void listFiles(const char * dir, std::vector<string> &vfile)
{
	using namespace std;
	HANDLE hFind;
	WIN32_FIND_DATA findData;
	LARGE_INTEGER size;
	char dirNew[100];

	// 向目录加通配符,用于搜索第一个文件 
	strcpy(dirNew, dir);
	strcat(dirNew, "\\*.*");

	hFind = FindFirstFile(dirNew, &findData);
	do
	{
		// 是否是文件夹,并且名称不为"."或".." 
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0
			&& strcmp(findData.cFileName, ".") != 0
			&& strcmp(findData.cFileName, "..") != 0
			)
		{
			// 将dirNew设置为搜索到的目录,并进行下一轮搜索 
			strcpy(dirNew, dir);
			strcat(dirNew, "\\");
			strcat(dirNew, findData.cFileName);
			//listFiles(dirNew);
		}
		else
		{
			size.LowPart = findData.nFileSizeLow;
			size.HighPart = findData.nFileSizeHigh;
			//cout << findData.cFileName << "\t" << size.QuadPart << " bytes\n";
			vfile.push_back(findData.cFileName);
		}
	} while (FindNextFile(hFind, &findData));

	FindClose(hFind);
	return;
}

Mat MyResizeImage(Mat pSrc, double dScale)
{
	IplImage *pImgSrc = &IplImage(pSrc);
	CvSize size;
	size.width = pImgSrc->width*dScale;
	size.height = pImgSrc->height*dScale;
	IplImage *pDes = cvCreateImage(size, pImgSrc->depth, pImgSrc->nChannels);
	cvResize(pImgSrc, pDes, CV_INTER_CUBIC);
	Mat matDes = cvarrToMat(pDes, true);
	cvReleaseImage(&pDes);
	return matDes;
}

int main(int argc, char*argv[])
{
	try
	{
		//创建人脸检测对象
		net_type net;
		deserialize("./mmod_human_face_detector.dat") >> net;

		//创建人脸特征点检测对象
		shape_predictor sp;
		deserialize("shape_predictor_68_face_landmarks.dat") >> sp;

		//创建人脸识别对象
		anet_type facerec;
		deserialize("dlib_face_recognition_resnet_model_v1.dat") >> facerec;

		//加载需要识别的图片对象
		std::vector<string> arrFiles;
		listFiles("./images/", arrFiles);

		std::vector<string>::iterator it_begin = arrFiles.begin();
		std::vector<string>::iterator it_end = arrFiles.end();

		//人脸描述队列
		std::vector<matrix<float, 0, 1>> arrSerialize;
		//标签队列
		std::vector<string> arrLabel;
		//显示窗口
		image_window img_win;
		for (; it_begin != it_end; ++it_begin)
		{
			string ext = strrchr((*it_begin).c_str(), '.');
			cout << ext.c_str() << endl;

			if (ext == ".jpg")
			{
				string strTmpJpg = "./images/";
				string strJpgName = (*it_begin).substr(0, (*it_begin).rfind("."));
				strTmpJpg += (*it_begin).c_str();

				//get image
				cv::Mat tempimg = imread(strTmpJpg.c_str());
				cv::Mat image2 = MyResizeImage(tempimg, 0.5);
				cv_image<bgr_pixel> cimg(image2);
				matrix<rgb_pixel> img;
				dlib::assign_image(img, cimg);
				
				//检测画面中的人脸
				auto dets = net(img);
				std::vector<matrix<rgb_pixel>> faces;
				std::vector<full_object_detection> shapes;
				for (auto&& d : dets)
				{
					// get the landmarks for this human's face
					auto shape = sp(img, d.rect);		//获取人脸区域68个特征点
					matrix<rgb_pixel> face_chip;
					extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);
					faces.push_back(move(face_chip));
					shapes.push_back(shape);
				}

				if (faces.size() == 0)
				{
					cout << "No faces found in image!" << endl;
					return 1;
				}

				//获取人脸描述
				std::vector<matrix<float, 0, 1>> face_descriptors = facerec(faces);
				std::vector<sample_pair> edges;
				for (size_t i = 0; i < face_descriptors.size(); ++i)
				{
					for (size_t j = i; j < face_descriptors.size(); ++j)
					{
						if (length(face_descriptors[i] - face_descriptors[j]) < 0.6)
							edges.push_back(sample_pair(i, j));
					}
				}
				std::vector<unsigned long> labels;
				const auto num_clusters = chinese_whispers(edges, labels);
				cout << "number of people found in the image: " << num_clusters << endl;

				std::vector<image_window> win_clusters(num_clusters);
				img_win.set_title("img");
				img_win.clear_overlay();
				img_win.set_image(img);
				img_win.add_overlay(render_face_detections(shapes));
				for (size_t cluster_id = 0; cluster_id < num_clusters; ++cluster_id)
				{
					std::vector<matrix<rgb_pixel>> temp;
					for (size_t j = 0; j < labels.size(); ++j)
					{
						if (cluster_id == labels[j])
							temp.push_back(faces[j]);
					}
					win_clusters[cluster_id].set_title("face cluster " + cast_to_string(cluster_id));
					win_clusters[cluster_id].set_image(tile_images(temp));
				}

				arrSerialize.push_back(face_descriptors[0]);
				arrLabel.push_back(strJpgName);
			}
		}

		serialize("assigner.dat") << arrSerialize;
		serialize("label.dat") << arrLabel;
	}
	catch (exception& e)
	{
		cout << e.what() << endl;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

telllong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值