2021-11-05 18:00:13 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "cJSON.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
size_t access_token_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
|
|
|
char *buf = (char *)malloc(size * nmemb + 8);
|
|
|
|
memset(buf, '\0', size * nmemb + 8);
|
|
|
|
strncpy(buf, ptr, size * nmemb);
|
|
|
|
cJSON *json = cJSON_Parse(buf);
|
|
|
|
char *access_token_result = (char *)stream;
|
|
|
|
strncpy(access_token_result, cJSON_GetObjectItem(json, "access_token")->valuestring, 128);
|
|
|
|
cJSON_Delete(json);
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
|
|
|
int post_access_token(char *access_token)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode result_code;
|
|
|
|
int error_code = 0;
|
2021-11-17 11:43:20 +08:00
|
|
|
char url[270] = { 0 };
|
2021-11-05 18:00:13 +08:00
|
|
|
char access_token_url[] = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials";
|
|
|
|
char AK[] = "vEcmEHNhIBfmA5qGs5TtcA8R";
|
|
|
|
char SK[] = "G4fk4L8p9QQGMqui4MkMWZ0fGkwWQoKE";
|
|
|
|
char access_token_result[128] = { 0 };
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if (curl) {
|
2021-11-17 11:43:20 +08:00
|
|
|
// url = access_token_url + "&client_id=" + AK + "&client_secret=" + SK;
|
2021-11-05 18:00:13 +08:00
|
|
|
sprintf(url, "%s&client_id=%s&client_secret=%s", access_token_url, AK, SK);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, access_token_result);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, access_token_callback);
|
|
|
|
result_code = curl_easy_perform(curl);
|
|
|
|
if (result_code != CURLE_OK) {
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result_code));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
strcpy(access_token, access_token_result);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
error_code = 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "curl_easy_init() failed.");
|
|
|
|
error_code = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
|
|
|
char *buf = (char *)malloc(size * nmemb + 8);
|
|
|
|
memset(buf, '\0', size * nmemb + 8);
|
|
|
|
strncpy(buf, ptr, size * nmemb);
|
|
|
|
|
|
|
|
cJSON *json = cJSON_Parse(buf);
|
|
|
|
printf("data:%s\n", cJSON_Print(json));
|
|
|
|
if (strstr(cJSON_Print(json), "SUCCESS") == NULL) {
|
|
|
|
*((double *)stream) = 0;
|
|
|
|
} else {
|
2021-11-17 11:43:20 +08:00
|
|
|
;
|
2021-11-05 18:00:13 +08:00
|
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:43:20 +08:00
|
|
|
int post_faceMatch(char *access_token, char *file)
|
2021-11-05 18:00:13 +08:00
|
|
|
{
|
2021-11-17 11:43:20 +08:00
|
|
|
char *getbase64(char *photoname);
|
|
|
|
int is_success;
|
|
|
|
char url[270] = { 0 };
|
2021-11-05 18:00:13 +08:00
|
|
|
char request_url[] = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
|
|
|
|
sprintf(url, "%s?access_token=%s", request_url, access_token);
|
|
|
|
char image[] = "\"image\": ";
|
|
|
|
char image_type[] = "\"image_type\": \"BASE64\"";
|
2021-11-17 11:43:20 +08:00
|
|
|
char face_field[] = "\"face_field\": \"facetype,gender,age,beauty,emotion\"";
|
|
|
|
char *image_base64 = getbase64(file);
|
|
|
|
char *params = (char *)malloc(strlen(image_base64) + strlen(image) + strlen(image_type) + 1024);
|
|
|
|
sprintf(params, "{%s\"%s\", %s, %s}", image, image_base64, image_type, face_field);
|
2021-11-05 18:00:13 +08:00
|
|
|
|
|
|
|
cJSON *json = cJSON_Parse(params);
|
|
|
|
CURL *curl = NULL;
|
|
|
|
CURLcode result_code;
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if (curl) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
|
|
struct curl_slist *headers = NULL;
|
|
|
|
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSON_Print(json));
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, faceMatch_callback);
|
|
|
|
result_code = curl_easy_perform(curl);
|
|
|
|
|
2021-11-17 11:43:20 +08:00
|
|
|
free(image_base64);
|
2021-11-05 18:00:13 +08:00
|
|
|
free(params);
|
|
|
|
cJSON_Delete(json);
|
|
|
|
|
|
|
|
if (result_code != CURLE_OK) {
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result_code));
|
|
|
|
is_success = 1;
|
|
|
|
return is_success;
|
|
|
|
}
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
is_success = 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "curl_easy_init() failed.");
|
|
|
|
is_success = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_success;
|
2021-11-17 11:43:20 +08:00
|
|
|
|
2021-11-05 18:00:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *getbase64(char *photoname)
|
|
|
|
{
|
|
|
|
char order[128] = { 0 };
|
|
|
|
char file[32] = { 0 };
|
|
|
|
char *base64 = NULL;
|
|
|
|
|
|
|
|
sprintf(order, "base64 %s > %s_base64", photoname, photoname);
|
|
|
|
system(order);
|
|
|
|
sprintf(file, "%s_base64", photoname);
|
|
|
|
int fd = open(file, O_RDWR);
|
|
|
|
int len = lseek(fd, 0, SEEK_END);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
base64 = (char *)malloc(len + 1);
|
|
|
|
read(fd, base64, len);
|
|
|
|
base64[len] = '\0';
|
|
|
|
|
|
|
|
return base64;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[], char **envp)
|
|
|
|
{
|
2021-11-17 11:43:20 +08:00
|
|
|
if (argc == 2 && argv[1] != NULL) {
|
2021-11-05 18:00:13 +08:00
|
|
|
;
|
2021-11-17 11:43:20 +08:00
|
|
|
} else {
|
|
|
|
printf("%s image_file\n", argv[0]);
|
2021-11-05 18:00:13 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:43:20 +08:00
|
|
|
char access_token[270];
|
2021-11-05 18:00:13 +08:00
|
|
|
post_access_token(access_token);
|
2021-11-17 11:43:20 +08:00
|
|
|
post_faceMatch(access_token, argv[1]);
|
2021-11-05 18:00:13 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|