欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

树莓派3B调用翔云人脸识别接口(cjson格式)

程序员文章站 2024-03-23 22:58:16
...

一、修改部分

int post_faceMatch(double *faceMatch)

这个函数里面的key和secret
树莓派3B调用翔云人脸识别接口(cjson格式)
修改对应的图片名
树莓派3B调用翔云人脸识别接口(cjson格式)

二、程序部分

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
    // 获取到的body存放在ptr中,先将其转换为string格式
	char *buf = (char *)malloc (size * nmemb + 8 );
	memset(buf,'\0',size * nmemb + 8);
	strncpy(buf, ptr, size * nmemb);
    //printf("buf:%s\n",buf);

	cJSON *json = cJSON_Parse(buf);
	printf("jsondata:\n%s\n",cJSON_Print(json));	
    


    
	cJSON_Delete(json);
	free(buf);

    return size * nmemb;
}

int post_faceMatch(double *faceMatch)
{
    //std::string url = request_url + "?access_token=" + access_token;
	char url[256] = {0};
	char request_url[] = "https://netocr.com/api/faceliu.do";
	sprintf(url,"%s",request_url);

	char *getbase64(char *photoname);
	char *img1= getbase64("l1.jpg");
	char *img2 = getbase64("l2.jpg");
    char *key = "";
    char *secret = "";
    int typeId = 21;
    char *format = "json";

	char *params = (char *)malloc(strlen(img1) + strlen(img2) 
								+ strlen(key) + strlen(secret) 
                                + strlen(format) + 128 );
	sprintf(params,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
                    img1,img2,key,secret,typeId,format);


	cJSON *json = cJSON_Parse(params);
    //printf("%s\n",cJSON_Print(json));
	//printf("param : %s\n",params);
    CURL *curl = NULL;
    CURLcode result_code;
    int is_success;
    curl = curl_easy_init();
	double faceMatch_result;
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
        curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params);
	    //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, 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);
		result_code = curl_easy_perform(curl);

		free(img1);
		free(img2);
		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;
        }
        *faceMatch = faceMatch_result;
        curl_easy_cleanup(curl);
        is_success = 0;
    } else {
        fprintf(stderr, "curl_easy_init() failed.");
        is_success = 1;
    }
    return is_success;
}

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';


	//printf("%d\n",strlen(base64));
	return base64;
}


int main ()
{

	double faceMatch = 0;



	post_faceMatch(&faceMatch);
	//printf("faceMatch : %f \n",faceMatch);

/*
	if(faceMatch > 90){
		printf("1");
	}else{
		printf("0");
	}
*/


	return 0;
}

三、编译

gcc xxx.c -lcurl -lssl -lcrypto -lcjson