Spaces:
Runtime error
Runtime error
final version?
Browse files- bin/detect-image +0 -0
- src/Makefile +10 -0
- src/detect-image.cpp +135 -0
bin/detect-image
CHANGED
|
Binary files a/bin/detect-image and b/bin/detect-image differ
|
|
|
src/Makefile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
CFLAGS=-O3 -Wall
|
| 2 |
+
|
| 3 |
+
detect-image: detect-image.o
|
| 4 |
+
g++ $(CFLAGS) -o detect-image -L /libfacedetection/build/install/lib/ -I /usr/include/opencv4/ -I /libfacedetection/build/install/include/facedetection detect-image.o -lfacedetection -fopenmp -lopencv_imgcodecs -lopencv_objdetect -lopencv_features2d -lopencv_imgproc -lopencv_highgui -lopencv_core
|
| 5 |
+
|
| 6 |
+
detect-image.o: detect-image.cpp
|
| 7 |
+
g++ $(CFLAGS) -I /usr/include/opencv4 -I /libfacedetection/build/install/include/facedetection -o detect-image.o -c detect-image.cpp -fopenmp
|
| 8 |
+
|
| 9 |
+
clean:
|
| 10 |
+
rm -f detect-image.o detect-image
|
src/detect-image.cpp
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
By downloading, copying, installing or using the software you agree to this license.
|
| 3 |
+
If you do not agree to this license, do not download, install,
|
| 4 |
+
copy or use the software.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
License Agreement For libfacedetection
|
| 8 |
+
(3-clause BSD License)
|
| 9 |
+
|
| 10 |
+
Copyright (c) 2018-2020, Shiqi Yu, all rights reserved.
|
| 11 |
+
shiqi.yu@gmail.com
|
| 12 |
+
|
| 13 |
+
Redistribution and use in source and binary forms, with or without modification,
|
| 14 |
+
are permitted provided that the following conditions are met:
|
| 15 |
+
|
| 16 |
+
* Redistributions of source code must retain the above copyright notice,
|
| 17 |
+
this list of conditions and the following disclaimer.
|
| 18 |
+
|
| 19 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
| 20 |
+
this list of conditions and the following disclaimer in the documentation
|
| 21 |
+
and/or other materials provided with the distribution.
|
| 22 |
+
|
| 23 |
+
* Neither the names of the copyright holders nor the names of the contributors
|
| 24 |
+
may be used to endorse or promote products derived from this software
|
| 25 |
+
without specific prior written permission.
|
| 26 |
+
|
| 27 |
+
This software is provided by the copyright holders and contributors "as is" and
|
| 28 |
+
any express or implied warranties, including, but not limited to, the implied
|
| 29 |
+
warranties of merchantability and fitness for a particular purpose are disclaimed.
|
| 30 |
+
In no event shall copyright holders or contributors be liable for any direct,
|
| 31 |
+
indirect, incidental, special, exemplary, or consequential damages
|
| 32 |
+
(including, but not limited to, procurement of substitute goods or services;
|
| 33 |
+
loss of use, data, or profits; or business interruption) however caused
|
| 34 |
+
and on any theory of liability, whether in contract, strict liability,
|
| 35 |
+
or tort (including negligence or otherwise) arising in any way out of
|
| 36 |
+
the use of this software, even if advised of the possibility of such damage.
|
| 37 |
+
*/
|
| 38 |
+
|
| 39 |
+
#include <stdio.h>
|
| 40 |
+
#include <opencv2/opencv.hpp>
|
| 41 |
+
#include "facedetectcnn.h"
|
| 42 |
+
|
| 43 |
+
//define the buffer size. Do not change the size!
|
| 44 |
+
//0x9000 = 1024 * (16 * 2 + 4), detect 1024 face at most
|
| 45 |
+
#define DETECT_BUFFER_SIZE 0x9000
|
| 46 |
+
using namespace cv;
|
| 47 |
+
using namespace std;
|
| 48 |
+
|
| 49 |
+
int main(int argc, char* argv[])
|
| 50 |
+
{
|
| 51 |
+
char* input_image_filename;
|
| 52 |
+
char* output_image_filename = NULL;
|
| 53 |
+
if(argc != 2 && argc != 3)
|
| 54 |
+
{
|
| 55 |
+
printf("Usage: %s <image_file_name> [output_file_name]\n", argv[0]);
|
| 56 |
+
return -1;
|
| 57 |
+
}
|
| 58 |
+
input_image_filename = argv[1];
|
| 59 |
+
if(argc == 3) {
|
| 60 |
+
output_image_filename = argv[2];
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
//load an image and convert it to gray (single-channel)
|
| 65 |
+
Mat image = imread(input_image_filename);
|
| 66 |
+
if(image.empty())
|
| 67 |
+
{
|
| 68 |
+
fprintf(stderr, "Can not load the image file %s.\n", input_image_filename);
|
| 69 |
+
return -1;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
int * pResults = NULL;
|
| 74 |
+
//pBuffer is used in the detection functions.
|
| 75 |
+
//If you call functions in multiple threads, please create one buffer for each thread!
|
| 76 |
+
unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
|
| 77 |
+
if(!pBuffer)
|
| 78 |
+
{
|
| 79 |
+
fprintf(stderr, "Can not alloc buffer.\n");
|
| 80 |
+
return -1;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
///////////////////////////////////////////
|
| 85 |
+
// CNN face detection
|
| 86 |
+
// Best detection rate
|
| 87 |
+
//////////////////////////////////////////
|
| 88 |
+
//!!! The input image must be a BGR one (three-channel) instead of RGB
|
| 89 |
+
//!!! DO NOT RELEASE pResults !!!
|
| 90 |
+
TickMeter cvtm;
|
| 91 |
+
cvtm.start();
|
| 92 |
+
|
| 93 |
+
pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);
|
| 94 |
+
|
| 95 |
+
cvtm.stop();
|
| 96 |
+
printf("[\n");
|
| 97 |
+
Mat result_image = image.clone();
|
| 98 |
+
//print the detection results
|
| 99 |
+
for(int i = 0; i < (pResults ? *pResults : 0); i++)
|
| 100 |
+
{
|
| 101 |
+
short * p = ((short*)(pResults + 1)) + 16*i;
|
| 102 |
+
int confidence = p[0];
|
| 103 |
+
int x = p[1];
|
| 104 |
+
int y = p[2];
|
| 105 |
+
int w = p[3];
|
| 106 |
+
int h = p[4];
|
| 107 |
+
printf(" {\n");
|
| 108 |
+
printf(" \"xmin\": %d,\n", x);
|
| 109 |
+
printf(" \"ymin\": %d,\n", y);
|
| 110 |
+
printf(" \"xmax\": %d,\n", x+w);
|
| 111 |
+
printf(" \"ymax\": %d,\n", y+h);
|
| 112 |
+
printf(" \"confidence\": %d\n", confidence);
|
| 113 |
+
if (i+1 < *pResults) {
|
| 114 |
+
printf(" },\n");
|
| 115 |
+
}
|
| 116 |
+
else {
|
| 117 |
+
printf(" }\n");
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
//show the score of the face. Its range is [0-100]
|
| 121 |
+
char sScore[256];
|
| 122 |
+
snprintf(sScore, 256, "%d", confidence);
|
| 123 |
+
cv::putText(result_image, sScore, cv::Point(x, y-3), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);
|
| 124 |
+
//draw face rectangle
|
| 125 |
+
rectangle(result_image, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
|
| 126 |
+
}
|
| 127 |
+
printf("]\n");
|
| 128 |
+
if (output_image_filename != NULL)
|
| 129 |
+
imwrite(output_image_filename, result_image);
|
| 130 |
+
|
| 131 |
+
//release the buffer
|
| 132 |
+
free(pBuffer);
|
| 133 |
+
|
| 134 |
+
return 0;
|
| 135 |
+
}
|