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

一、修改部分

int post_faceMatch(double *faceMatch)

这个函数里面的key和secret
在这里插入图片描述
修改对应的图片名
在这里插入图片描述

二、程序部分

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值