人脸检测初探
来源:互联网 发布:c语言打印空心菱形 编辑:程序博客网 时间:2024/05/16 04:54
介绍
目前人脸识别比较成熟,OpenCV也已经把目前主流的人脸识别算法(PCA,Haar,LBP)集成到算法库,并且附带例程和文档。文档链接http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html 小明网友对这篇文档详细说明 http://guoming.me/face-recognition-with-opencv
数据准备
创建自己的CSV文件在官方的文档中是python程序实现的,但有点小小的不足就是只是打印出来,自己复制到文件。。。这里小改了下程序,自动保存在文件中。
import sysimport os.path# This is a tiny script to help you creating a CSV file from a face# database with a similar hierarchie:## philipp@mango:~/facerec/data/at$ tree# .# |-- README# |-- s1# | |-- 1.pgm# | |-- ...# | |-- 10.pgm# |-- s2# | |-- 1.pgm# | |-- ...# | |-- 10.pgm# ...# |-- s40# | |-- 1.pgm# | |-- ...# | |-- 10.pgm#if __name__ == "__main__": if len(sys.argv) != 2: print "usage: create_csv <base_path>" sys.exit(1) BASE_PATH=sys.argv[1] SEPARATOR=";" fh = open("at.txt",'w') label = 0 for dirname, dirnames, filenames in os.walk(BASE_PATH): for subdirname in dirnames: subject_path = os.path.join(dirname, subdirname) for filename in os.listdir(subject_path): abs_path = "%s/%s" % (subject_path, filename) print "%s%s%d" % (abs_path, SEPARATOR, label) fh.write(abs_path) fh.write(SEPARATOR) fh.write(str(label)) fh.write("\n") label = label + 1 fh.close()
简短代码
/* * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>. * Released to public domain under terms of the BSD Simplified license. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the organization nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * See <http://www.opensource.org/licenses/bsd-license> */#include "opencv2/core/core.hpp"#include "opencv2/contrib/contrib.hpp"#include "opencv2/highgui/highgui.hpp"#include <iostream>#include <fstream>#include <sstream>using namespace cv;using namespace std;static Mat norm_0_255(InputArray _src) { Mat src = _src.getMat(); // Create and return normalized image: Mat dst; switch(src.channels()) { case 1: cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1); break; case 3: cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3); break; default: src.copyTo(dst); break; } return dst;}static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') { std::ifstream file(filename.c_str(), ifstream::in); if (!file) { string error_message = "No valid input file was given, please check the given filename."; CV_Error(CV_StsBadArg, error_message); } string line, path, classlabel; while (getline(file, line)) { stringstream liness(line); getline(liness, path, separator); getline(liness, classlabel); if(!path.empty() && !classlabel.empty()) { images.push_back(imread(path, 0)); labels.push_back(atoi(classlabel.c_str())); } }}int main(int argc, const char *argv[]) { // Get the path to your CSV. string fn_csv = string("at.txt"); // These vectors hold the images and corresponding labels. vector<Mat> images; vector<int> labels; // Read in the data. This can fail if no valid // input filename is given. try { read_csv(fn_csv, images, labels); } catch (cv::Exception& e) { cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl; // nothing more we can do exit(1); } // Quit if there are not enough images for this demo. if(images.size() <= 1) { string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!"; CV_Error(CV_StsError, error_message); } // Get the height from the first image. We'll need this // later in code to reshape the images to their original // size: int height = images[0].rows; // The following lines simply get the last images from // your dataset and remove it from the vector. This is // done, so that the training data (which we learn the // cv::FaceRecognizer on) and the test data we test // the model with, do not overlap. Mat testSample = images[images.size() - 1]; int testLabel = labels[labels.size() - 1]; images.pop_back(); labels.pop_back(); // The following lines create an Eigenfaces model for // face recognition and train it with the images and // labels read from the given CSV file. // This here is a full PCA, if you just want to keep // 10 principal components (read Eigenfaces), then call // the factory method like this: // // cv::createEigenFaceRecognizer(10); // // If you want to create a FaceRecognizer with a // confidence threshold (e.g. 123.0), call it with: // // cv::createEigenFaceRecognizer(10, 123.0); // // If you want to use _all_ Eigenfaces and have a threshold, // then call the method like this: // // cv::createEigenFaceRecognizer(0, 123.0); // Ptr<FaceRecognizer> model = createEigenFaceRecognizer(); model->train(images, labels); // The following line predicts the label of a given // test image: int predictedLabel = model->predict(testSample); // // To get the confidence of a prediction call the model with: // // int predictedLabel = -1; // double confidence = 0.0; // model->predict(testSample, predictedLabel, confidence); // string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel); cout << result_message << endl; return 0;}
0 0
- 人脸检测初探
- 13.GPUImage初探以及人脸检测
- snort规则检测引擎初探
- 标准Hough直线检测初探
- 单一世界架构初探之碰撞检测
- Memory Leak检测神器--LeakCanary初探
- 人脸检测的harr检测函数
- 人脸检测or五官检测
- OpenCV:人脸检测和行人检测
- 目标检测之人脸检测
- 人脸识别--活体检测(眨眼检测)
- Adaboost 人脸检测
- 人脸检测技术
- 人脸检测
- 人脸检测
- 人脸器官检测
- opencv 检测人脸
- adaboost人脸检测
- 如何去理解动漫游戏设计
- 关联矩阵
- Android InputMethodManager 常量
- 如何在Chrome浏览器安装第三方扩展
- assigning to id from incompatible type
- 人脸检测初探
- 接收邮件信息
- 各种垃圾回收算法(二)标记-清除( Mark-Sweep )算法
- Android的内存泄漏和调试
- Leetcode: Climbing Stairs
- 如何区分系统软件和应用软件
- IOS keyboard type
- sql 2008注册服务器失败!viewinfo无效。
- SAP XI 常用事务代码