-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacecode.cpp
More file actions
188 lines (162 loc) · 4.55 KB
/
facecode.cpp
File metadata and controls
188 lines (162 loc) · 4.55 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "facecode.h"
#define INPUT1_IMAGE_WIDTH (480)
#define INPUT1_IMAGE_HEIGHT (640)
#define INPUT1_IMAGE_FORMAT ASVL_PAF_I420
#define INPUT2_IMAGE_FORMAT ASVL_PAF_I420
#define INPUT2_IMAGE_WIDTH (480)
#define INPUT2_IMAGE_HEIGHT (640)
//get filename
int getFileName(char *path,char *filename)
{
if (NULL == path) {
return -1;
}
char *name_start = NULL;
char *name_end = NULL;
/*int flag = 0;
name_start = strrchr(path, '/');
if(name_start == NULL)
{
name_start = path;
}
else
flag = 1;*/
name_end = strrchr(path, '.');
if((name_end == NULL) || (name_end == path))
return -1;
memcpy(filename, path, name_end - path);
//memcpy(filename, name_start+flag, name_end - name_start-flag);
return 0;
}
//jpg to yuv
int jpgToyuv(char *path,int width,int height,char *convertName)
{
if(path == NULL)
return -1;
//if (image format )
char cmdstring[256] = {0};
char imageName[256] = {0};
int ret = getFileName(path,imageName);
if(0 != ret)
{
printf("getFileName errno\n");
return -1;
}
sprintf(convertName,"%s.yuv",imageName);
sprintf(cmdstring,"ffmpeg -i %s -s %dx%d -pix_fmt yuv420p -y %s",path,width,height,convertName);
ret = -1;
ret = system(cmdstring);
if(ret < 0)
{
printf("cmd: %s\t error: %s", cmdstring, strerror(errno));
return -1;
}
return 0;
}
int facecomparison(char *path1,char *path2,float *fSimilScore)
{
AFD_FSDK_IMAGE fd_image1 = {0};
AFD_FSDK_IMAGE fd_image2 = {0};
AFR_FSDK_IMAGE fr_image1 = {0};
AFR_FSDK_IMAGE fr_image2 = {0};
AFR_FSDK_FACEINPUT fr_faceResult1;
AFR_FSDK_FACEINPUT fr_faceResult2;
PMRECT rect = (PMRECT)malloc(sizeof(MRECT));
int ret = -1;
char imageName1[256] = {0};
char imageName2[256] = {0};
//image 1
//1.jpg convert to yuv
ret = jpgToyuv(path1,INPUT1_IMAGE_WIDTH,INPUT1_IMAGE_HEIGHT,imageName1);
if(ret != 0)
{
printf("image formate convert error\n");
free(rect);
return -1;
}
//2.detect face rect
fd_image1.lPath = imageName1;
fd_image1.format = INPUT1_IMAGE_FORMAT;
fd_image1.width = INPUT1_IMAGE_WIDTH;
fd_image1.height = INPUT1_IMAGE_HEIGHT;
ret = -1;
ret = facedetect(fd_image1, rect);
if(ret != 0)
{
printf("face detect error\n");
free(rect);
return -1;
}
fr_faceResult1.lOrient = AFR_FSDK_FOC_0;
fr_faceResult1.rcFace.left = rect->left;
fr_faceResult1.rcFace.top = rect->top;
fr_faceResult1.rcFace.right = rect->right;
fr_faceResult1.rcFace.bottom = rect->bottom;
//image 2
//1.jpg convert to yuv
ret = -1;
ret = jpgToyuv(path2,INPUT2_IMAGE_WIDTH,INPUT2_IMAGE_HEIGHT,imageName2);
if(ret != 0)
{
printf("image formate convert error\n");
free(rect);
return -1;
}
//2.detect face rect
fd_image2.lPath = imageName2;
fd_image2.format = INPUT2_IMAGE_FORMAT;
fd_image2.width = INPUT2_IMAGE_WIDTH;
fd_image2.height = INPUT2_IMAGE_HEIGHT;
ret = -1;
memset(rect, 0, sizeof(MRECT));
ret = facedetect(fd_image2, rect);
if(ret != 0)
{
printf("face detect error\n");
free(rect);
return -1;
}
fr_faceResult2.lOrient = AFR_FSDK_FOC_0;
fr_faceResult2.rcFace.left = rect->left;
fr_faceResult2.rcFace.top = rect->top;
fr_faceResult2.rcFace.right = rect->right;
fr_faceResult2.rcFace.bottom = rect->bottom;
//face comparison
fr_image1.lPath = imageName1;
fr_image1.format = INPUT1_IMAGE_FORMAT;
fr_image1.width = INPUT1_IMAGE_WIDTH;
fr_image1.height = INPUT1_IMAGE_HEIGHT;
fr_image2.lPath = imageName2;
fr_image2.format = INPUT2_IMAGE_FORMAT;
fr_image2.width = INPUT2_IMAGE_WIDTH;
fr_image2.height = INPUT2_IMAGE_HEIGHT;
ret = -1;
ret = facerecognition(fr_image1,fr_faceResult1,fr_image2,fr_faceResult2,fSimilScore);
if(ret != 0)
{
printf("face recognition error\n");
free(rect);
return -1;
}
printf("face recognition successed\n");
free(rect);
return 0;
}
int main(int argc,char*argv[])
{
if(argc != 2)
{
printf("facecomparison imagepath1 imagepath2 \n");
exit(0);
}
float fSimilScore = 0.0;
facecomparison(argv[0],argv[1],&fSimilScore);
printf("fSimilScore === %f",fSimilScore);
return 0;
}