树莓派采用翔云实现人脸识别

1.通过翔云平台购买人脸识别接口
个人中心服务列表查看剩余接口次数
树莓派采用翔云实现人脸识别_第1张图片
2.C语言程序访问接口
需要安装支持ssl的curl库,交叉编译curl库

(1)C语言程序访问接口

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define true  1
#define false 2
typedef unsigned int bool;

char buf[1024] = {
     '\0'};

size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
     
        strncpy(buf,ptr,1024);
}

char *ObtainBase64(char *p)
{
     
    char *bufPic;
    char cmp[128]={
     '\0'};

    sprintf(cmp,"base64 %s > tmpfile",p);
    system(cmp);
    int fd = open("./tmpfile",O_RDWR);
    int filelen = lseek(fd,0,SEEK_END);
    lseek(fd,0,SEEK_SET);
    bufPic = (char *)malloc(filelen+4);
    memset(bufPic,'\0',filelen+4);
    read(fd,bufPic,filelen+4);
    close(fd);
    system("rm -f tmpfile");

    return bufPic;

}
bool postUrl()
{
     
    CURL *curl;
    CURLcode res;
    char *postString;

    char *img1;
    char *img2;
    char *key = "自己的翔云平台OCR key";
    char *secret = "自己的相遇平台 COR secret";
    int typeld = 21;
    char *format = "xml";

    img1 = ObtainBase64("./image1.jpg");
    img2 = ObtainBase64("./image2.jpg");

    int len = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+128;
    postString = (char *)malloc(len);
    memset(postString,'\0',len);
    sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,key,secret,21,format);

    curl = curl_easy_init();
    if (curl)
    {
     

            //curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // 指定post内
        curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do ");   // 指定url库
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);

        res = curl_easy_perform(curl);
        printf("ok!%d\n",res);

        if(strstr(buf,"是") != NULL){
     
                printf("Recognition success\n");
        }else{
     
                printf("Recognition failure\n");
        }

        curl_easy_cleanup(curl);
    }
    return true;
}

int main(void)
{
     
    postUrl();
}     

(2)获取图片信息并调用接口进行识别程序

#include
#include
#include
#include
int main(){
     
	char cmd[12];
	char ret[128]={
     '\0'};
	FILE *fp;


	while(1){
     
		printf("请输入open开始人脸识别\n");
	
		scanf("%s",cmd);
		getchar();
		if(strcmp(cmd,"open") == 0){
     
			memset(cmd,'0',sizeof(cmd)/sizeof(char));
			printf("开始进行人脸识别请勿离开······\n");
			system("raspistill -t 2000 -o image2.jpg -q 5");  //运用raspistill进行拍照,注意图片不用太大
			sleep(2);
			printf("获取人脸数据,正在比对········\n");
			
			fp = popen("./cossDemo4","r");  //调用编译好的接口程序,进行识别判断,并且获取输出结果	
			int nread = fread(ret,128,1,fp);
			if(strstr(ret,"success") != NULL){
     
				printf("识别成功,开锁\n");
			}else{
     
			
				printf("识别失败\n");
			}
		}
	}
	
	return 0;
}

你可能感兴趣的:(树莓派,人脸识别)