modified: BaiduAIFaceDetection
modified: BaiduAIFaceDetection.c
This commit is contained in:
parent
8944030830
commit
ed6e97ba27
Binary file not shown.
@ -16,7 +16,6 @@ size_t access_token_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
|||||||
cJSON *json = cJSON_Parse(buf);
|
cJSON *json = cJSON_Parse(buf);
|
||||||
char *access_token_result = (char *)stream;
|
char *access_token_result = (char *)stream;
|
||||||
strncpy(access_token_result, cJSON_GetObjectItem(json, "access_token")->valuestring, 128);
|
strncpy(access_token_result, cJSON_GetObjectItem(json, "access_token")->valuestring, 128);
|
||||||
//printf("stream:%s\n",(char *)stream);
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
@ -28,7 +27,7 @@ int post_access_token(char *access_token)
|
|||||||
CURL *curl;
|
CURL *curl;
|
||||||
CURLcode result_code;
|
CURLcode result_code;
|
||||||
int error_code = 0;
|
int error_code = 0;
|
||||||
char url[256] = { 0 };
|
char url[270] = { 0 };
|
||||||
char access_token_url[] = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials";
|
char access_token_url[] = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials";
|
||||||
char AK[] = "vEcmEHNhIBfmA5qGs5TtcA8R";
|
char AK[] = "vEcmEHNhIBfmA5qGs5TtcA8R";
|
||||||
char SK[] = "G4fk4L8p9QQGMqui4MkMWZ0fGkwWQoKE";
|
char SK[] = "G4fk4L8p9QQGMqui4MkMWZ0fGkwWQoKE";
|
||||||
@ -36,9 +35,8 @@ int post_access_token(char *access_token)
|
|||||||
|
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
if (curl) {
|
if (curl) {
|
||||||
//std::string url = access_token_url + "&client_id=" + AK + "&client_secret=" + SK;
|
// url = access_token_url + "&client_id=" + AK + "&client_secret=" + SK;
|
||||||
sprintf(url, "%s&client_id=%s&client_secret=%s", access_token_url, AK, SK);
|
sprintf(url, "%s&client_id=%s&client_secret=%s", access_token_url, AK, SK);
|
||||||
//printf("url: %s\n",url );
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||||||
@ -49,7 +47,6 @@ int post_access_token(char *access_token)
|
|||||||
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result_code));
|
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result_code));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
//printf("access_token_result:%s\n",access_token_result);
|
|
||||||
strcpy(access_token, access_token_result);
|
strcpy(access_token, access_token_result);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
error_code = 0;
|
error_code = 0;
|
||||||
@ -63,7 +60,6 @@ int post_access_token(char *access_token)
|
|||||||
|
|
||||||
size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
||||||
{
|
{
|
||||||
// 获取到的body存放在ptr中,先将其转换为string格式
|
|
||||||
char *buf = (char *)malloc(size * nmemb + 8);
|
char *buf = (char *)malloc(size * nmemb + 8);
|
||||||
memset(buf, '\0', size * nmemb + 8);
|
memset(buf, '\0', size * nmemb + 8);
|
||||||
strncpy(buf, ptr, size * nmemb);
|
strncpy(buf, ptr, size * nmemb);
|
||||||
@ -73,11 +69,7 @@ size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
|||||||
if (strstr(cJSON_Print(json), "SUCCESS") == NULL) {
|
if (strstr(cJSON_Print(json), "SUCCESS") == NULL) {
|
||||||
*((double *)stream) = 0;
|
*((double *)stream) = 0;
|
||||||
} else {
|
} else {
|
||||||
cJSON *result = cJSON_GetObjectItem(json, "result");
|
;
|
||||||
//printf("data:%s\n",cJSON_Print(result));
|
|
||||||
double *faceMatch_result = (double *)stream;
|
|
||||||
//*faceMatch_result = cJSON_GetObjectItem(result, "score")->valuedouble;
|
|
||||||
//printf("stream:%f\n",*faceMatch_result);
|
|
||||||
}
|
}
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
free(buf);
|
free(buf);
|
||||||
@ -85,44 +77,35 @@ size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
|||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
int post_faceMatch(double *faceMatch, char *access_token, char *file)
|
int post_faceMatch(char *access_token, char *file)
|
||||||
{
|
{
|
||||||
char url[256] = { 0 };
|
char *getbase64(char *photoname);
|
||||||
//char request_url[] = "https://aip.baidubce.com/rest/2.0/face/v3/match";
|
int is_success;
|
||||||
|
char url[270] = { 0 };
|
||||||
char request_url[] = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
|
char request_url[] = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
|
||||||
sprintf(url, "%s?access_token=%s", request_url, access_token);
|
sprintf(url, "%s?access_token=%s", request_url, access_token);
|
||||||
|
|
||||||
char *getbase64(char *photoname);
|
|
||||||
char image[] = "\"image\": ";
|
char image[] = "\"image\": ";
|
||||||
char image_type[] = "\"image_type\": \"BASE64\"";
|
char image_type[] = "\"image_type\": \"BASE64\"";
|
||||||
char face_field[] = "\"face_field\": \"facetype,gender,age,beauty\"";
|
char face_field[] = "\"face_field\": \"facetype,gender,age,beauty,emotion\"";
|
||||||
|
char *image_base64 = getbase64(file);
|
||||||
char *image1_base64 = getbase64(file);
|
char *params = (char *)malloc(strlen(image_base64) + strlen(image) + strlen(image_type) + 1024);
|
||||||
char *params = (char *)malloc(strlen(image1_base64) + strlen(image) + strlen(image_type) + 1024);
|
sprintf(params, "{%s\"%s\", %s, %s}", image, image_base64, image_type, face_field);
|
||||||
|
|
||||||
sprintf(params, "{%s\"%s\", %s, %s}", image, image1_base64, image_type, face_field);
|
|
||||||
|
|
||||||
cJSON *json = cJSON_Parse(params);
|
cJSON *json = cJSON_Parse(params);
|
||||||
//printf("%s\n", cJSON_Print(json));
|
|
||||||
//printf("param : %s\n", params);
|
|
||||||
CURL *curl = NULL;
|
CURL *curl = NULL;
|
||||||
CURLcode result_code;
|
CURLcode result_code;
|
||||||
int is_success;
|
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
double faceMatch_result;
|
|
||||||
if (curl) {
|
if (curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||||
struct curl_slist *headers = NULL;
|
struct curl_slist *headers = NULL;
|
||||||
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
|
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||||
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSON_Print(json));
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSON_Print(json));
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &faceMatch_result);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, faceMatch_callback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, faceMatch_callback);
|
||||||
result_code = curl_easy_perform(curl);
|
result_code = curl_easy_perform(curl);
|
||||||
|
|
||||||
free(image1_base64);
|
free(image_base64);
|
||||||
free(params);
|
free(params);
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
||||||
@ -131,7 +114,6 @@ int post_faceMatch(double *faceMatch, char *access_token, char *file)
|
|||||||
is_success = 1;
|
is_success = 1;
|
||||||
return is_success;
|
return is_success;
|
||||||
}
|
}
|
||||||
*faceMatch = faceMatch_result;
|
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
is_success = 0;
|
is_success = 0;
|
||||||
} else {
|
} else {
|
||||||
@ -140,6 +122,7 @@ int post_faceMatch(double *faceMatch, char *access_token, char *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return is_success;
|
return is_success;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *getbase64(char *photoname)
|
char *getbase64(char *photoname)
|
||||||
@ -163,20 +146,16 @@ char *getbase64(char *photoname)
|
|||||||
|
|
||||||
int main(int argc, char *argv[], char **envp)
|
int main(int argc, char *argv[], char **envp)
|
||||||
{
|
{
|
||||||
/*
|
if (argc == 2 && argv[1] != NULL) {
|
||||||
if (argc == 3 && argv[1] != NULL && argv[2] != NULL) {
|
|
||||||
;
|
;
|
||||||
} else {
|
} else {
|
||||||
printf("%s image_file image_file\n", argv[0]);
|
printf("%s image_file\n", argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
char access_token[270];
|
char access_token[270];
|
||||||
double faceMatch = 0; // 面部匹配
|
|
||||||
|
|
||||||
post_access_token(access_token);
|
post_access_token(access_token);
|
||||||
|
post_faceMatch(access_token, argv[1]);
|
||||||
post_faceMatch(&faceMatch, access_token, argv[1]);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user