File size: 4,524 Bytes
e941adc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.


                  License Agreement For libfacedetection
                     (3-clause BSD License)

Copyright (c) 2018-2020, Shiqi Yu, all rights reserved.
shiqi.yu@gmail.com

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 names of the copyright holders nor the names of the contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"

//define the buffer size. Do not change the size!
//0x9000 = 1024 * (16 * 2 + 4), detect 1024 face at most
#define DETECT_BUFFER_SIZE 0x9000
using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    char* input_image_filename;
    char* output_image_filename = NULL;
    if(argc != 2 && argc != 3)
    {
        printf("Usage: %s <image_file_name> [output_file_name]\n", argv[0]);
        return -1;
    }
    input_image_filename = argv[1];
    if(argc == 3) {
	output_image_filename = argv[2];
    }


	//load an image and convert it to gray (single-channel)
	Mat image = imread(input_image_filename); 
	if(image.empty())
	{
		fprintf(stderr, "Can not load the image file %s.\n", input_image_filename);
		return -1;
	}


	int * pResults = NULL; 
    //pBuffer is used in the detection functions.
    //If you call functions in multiple threads, please create one buffer for each thread!
    unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    if(!pBuffer)
    {
        fprintf(stderr, "Can not alloc buffer.\n");
        return -1;
    }
	

	///////////////////////////////////////////
	// CNN face detection 
	// Best detection rate
	//////////////////////////////////////////
	//!!! The input image must be a BGR one (three-channel) instead of RGB
	//!!! DO NOT RELEASE pResults !!!
    TickMeter cvtm;
    cvtm.start();

	pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);
    
    cvtm.stop();    
    printf("[\n");
	Mat result_image = image.clone();
	//print the detection results
	for(int i = 0; i < (pResults ? *pResults : 0); i++)
	{
	        short * p = ((short*)(pResults + 1)) + 16*i;
		int confidence = p[0];  
		int x = p[1];
		int y = p[2];
		int w = p[3];
		int h = p[4];
		printf("  {\n");
		printf("    \"xmin\": %d,\n", x);
		printf("    \"ymin\": %d,\n", y);
		printf("    \"xmax\": %d,\n", x+w);
		printf("    \"ymax\": %d,\n", y+h);
		printf("    \"confidence\": %d\n", confidence);
		if (i+1 < *pResults) {
			printf("  },\n");
		}
		else {
			printf("  }\n");
		}
        
        	//show the score of the face. Its range is [0-100]
        	char sScore[256];
        	snprintf(sScore, 256, "%d", confidence);
        	cv::putText(result_image, sScore, cv::Point(x, y-3), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);
        	//draw face rectangle
		rectangle(result_image, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
	}
    printf("]\n");
    if (output_image_filename != NULL)
        imwrite(output_image_filename, result_image);

    //release the buffer
    free(pBuffer);

    return 0;
}