(OpenCV face recognition source code) LINUX opencv simple call to recognize human faces at source on the success of the operation in person along with C ++

Use environmental tools and libraries ubuntu18.04 opencv4.2.0 cmake 3.10.2

Find a place to build the project file: mkdir project
project下 mkdir facedetect
facedetect mkdir build first and then create CMakeLists.txt facedetect.cpp

Here Insert Picture Description

Use the following instructions will require resources, copied to under build, make sure that the current directory is in the build:

Resource requirements: haarcascade_frontalface_default.xml (you can go to look up the location of the resource by the find command, https: //blog.csdn.net/nb_zsy/article/details/104150589, left instructions not to return search of the URL).
Cp - r /usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml.
this directive is based on my own environment to lose, you lose mine own path to the instruction, resources need is haarcascade_frontalface_default.xml

Under the build execute cmake ... (only two points, I do not know why a multi-point display here)
Then execute cmake --build ./build/
If successful you should see the following (not including .jpg file)

Here Insert Picture Description

Then add jpg image they want to identify the next build, facedetect program under execution build, will face marked with a green frame.

./facedetect zsy_sister.jpg (the picture is my own shot)

Here Insert Picture Description
./facedetect harris_and_wife.jpg (the picture is the online search)
Here Insert Picture Description

CMakeLists.txt Code:

#cmake need this
cmake_minimum_required(VERSION 3.1)

#Enable C++ 11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED)

#define project name
project(detectface)

#find OpenCV
find_package(OpenCV REQUIRED)

#If package found,several variable will be set
message(STATUS "OpenCV libray status: ")
message(STATUS"    config: ${OpenCV_DIR}")
message(STATUS"    version: ${OpenCV_VERSION}")
message(STATUS"    libraries: ${OpenCV_LIBS}")
message(STATUS"    include path: ${OpenCV_INCLUDE_DIRS}")

#declare executable target build from sources
add_executable(facedetect facedetect.cpp)

#link to OpenCV lib
target_link_libraries(facedetect ${OpenCV_LIBS})

facedetect.cpp Source:

#include "opencv2/opencv.hpp"

int main(int argc, char const *argv[])
{
	cv::CascadeClassifier cascade;
	std::string imageName, cascadeName;
	cv::Mat image, gray;
	std::vector<cv::Rect> faces;
	cv::Scalar greenColor = cv::Scalar(0,255,0);

	cascadeName = "./haarcascade_frontalface_default.xml";
	imageName = argv[1];
	//load image and class
	if(!cascade.load(cascadeName)){
		std::cerr << "Could not load classifier cascade" << std::endl;
	}else if(imageName.empty()){
		std::cout<<"Could not read"<<imageName<<std::endl;
	}else{
		image = cv::imread(imageName,1);
		if (image.empty())
		{
			std::cout << "Could not imread image" << std::endl;
				/* code */
		}
	}
	//convert to gray
	cv::cvtColor(image,gray,cv::COLOR_BGR2GRAY);

	// run face detector
	cascade.detectMultiScale(image,faces,1.1,2,0,cv::Size(30,30));

	//output rectange
	for(size_t i = 0; i < faces.size(); ++i){
		cv::Rect r = faces[i];
		cv::rectangle(
			image,cv::Point(cvRound(r.x),cvRound(r.y)),
			cv::Point(cvRound(r.x+r.width-1),cvRound(r.y+r.height-1)),
			greenColor,3,8,0);
	}

	cv::imshow("result",image);
	cv::waitKey(0);
	cv::destroyAllWindows();
	return 0;
}

YOUTUBE big thanks to the guidance of God's will over the wall of a friend please see:
https://www.youtube.com/watch?v=GwHRG-JZZew

Published 36 original articles · won praise 3 · Views 4998

Guess you like

Origin blog.csdn.net/nb_zsy/article/details/104247389