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

1.通过翔云平台购买人脸识别接口
个人中心服务列表查看剩余接口次数
在这里插入图片描述
2.C语言程序访问接口
需要安装支持ssl的curl库,交叉编译curl库

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

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#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<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
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;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值