-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect_face.cpp
96 lines (81 loc) · 3.1 KB
/
detect_face.cpp
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
#include "dlib/dlib/image_processing/frontal_face_detector.h"
#include "dlib/dlib/image_processing.h"
#include "dlib/dlib/image_io.h"
#include <stdio.h>
#include "dlib/dlib/gui_widgets.h"
#include <string>
#include <sys/stat.h>
#include <dirent.h>
using namespace dlib;
//获取path目录下的所有图片标签和特征保存在entry中
void getAllImages(const char* path)
{
DIR* pDir;
struct dirent* ptr;
struct stat s;
lstat(path, &s);
pDir = opendir(path);
frontal_face_detector detector = get_frontal_face_detector();
array2d<rgb_pixel> img;
//插入不同人的脸部特征记录
if(pDir)
{
// name = std::string(path).substr(found+1).data();
while((ptr = readdir(pDir)) != 0)
{
if (ptr->d_type == DT_DIR)
{
if (strcmp(ptr -> d_name,".") != 0 && strcmp(ptr -> d_name,"..") != 0)
{
getAllImages((std::string(path) + "/" + ptr -> d_name).data());
}
}
else if(ptr->d_type == DT_REG)
{
std::size_t name_end = std::string(ptr -> d_name).find_last_of("_");
std::size_t idx_end = std::string(ptr -> d_name).find_last_of(".");
std::string name = std::string(ptr -> d_name).substr(0, name_end);
std::string idx = std::string(ptr -> d_name).substr(name_end+1, idx_end - name_end - 1);
printf("path:%s\t", name.data());
// entry.push_back(std::make_pair(std::string(path).substr(found+1), ptr -> d_name));
char pa[256];
strcpy(pa, path);
strcat(pa, "/");
load_image(img, strcat(pa, ptr -> d_name));
image_window win(img);
// pyramid_up(img);
std::vector<rectangle> dets = detector(img);
float index = 0, area = 0;
printf("dets_size:%d\n" , dets.size());
if(!dets.size())
continue;
for (unsigned long j = 0; j < dets.size(); ++j)
{
float width = dets[j].right() - dets[j].left();
float height = dets[j].bottom() - dets[j].top();
if(width * height > area)
{
index = j;
area = width * height;
}
win.add_overlay(dets[j]);
dlib::sleep(1000);
}
FILE *fp = fopen("bbox.xy", "a+");
if(NULL == fp)
{
fprintf(stderr, "file open error");
}
fprintf(fp, "%s %s %ld,%ld,%ld,%ld\n", name.data(), idx.data(), dets[index].right(), dets[index].left(), dets[index].bottom(), dets[index].top());
fclose(fp);
std::cout << "image " << ptr -> d_name << " process finished" << std::endl;
}
}
}
}
int main(int argc, char const *argv[])
{
const char *dir = argv[1];
getAllImages(dir);
return 0;
}