MCPcopy Create free account
hub / github.com/ShiqiYu/libfacedetection / main

Function main

example/detect-image.cpp:47–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45using namespace cv;
46
47int main(int argc, char* argv[])
48{
49 if(argc != 2)
50 {
51 printf("Usage: %s <image_file_name>\n", argv[0]);
52 return -1;
53 }
54
55 //load an image and convert it to gray (single-channel)
56 Mat image = imread(argv[1]);
57 if(image.empty())
58 {
59 fprintf(stderr, "Can not load the image file %s.\n", argv[1]);
60 return -1;
61 }
62
63 int * pResults = NULL;
64 //pBuffer is used in the detection functions.
65 //If you call functions in multiple threads, please create one buffer for each thread!
66 unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
67 if(!pBuffer)
68 {
69 fprintf(stderr, "Can not alloc buffer.\n");
70 return -1;
71 }
72
73
74 ///////////////////////////////////////////
75 // CNN face detection
76 // Best detection rate
77 //////////////////////////////////////////
78 //!!! The input image must be a BGR one (three-channel) instead of RGB
79 //!!! DO NOT RELEASE pResults !!!
80 TickMeter cvtm;
81 cvtm.start();
82
83 pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);
84
85 cvtm.stop();
86 printf("time = %gms\n", cvtm.getTimeMilli());
87
88 printf("%d faces detected.\n", (pResults ? *pResults : 0));
89 Mat result_image = image.clone();
90 //print the detection results
91 for(int i = 0; i < (pResults ? *pResults : 0); i++)
92 {
93 short * p = ((short*)(pResults+1))+142*i;
94 int confidence = p[0];
95 int x = p[1];
96 int y = p[2];
97 int w = p[3];
98 int h = p[4];
99
100 //show the score of the face. Its range is [0-100]
101 char sScore[256];
102 snprintf(sScore, 256, "%d", confidence);
103 cv::putText(result_image, sScore, cv::Point(x, y-3), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);
104 //draw face rectangle

Callers

nothing calls this directly

Calls 2

ptrMethod · 0.80
facedetect_cnnFunction · 0.50

Tested by

no test coverage detected