基于翔云OCR云平台的人脸识别(2)

基于翔云OCR云平台的人脸识别(2)

项目思路
基于翔云OCR云平台的人脸识别(2)_第1张图片

raspistill命令的相关参数说明

-v:调试信息查看
-w:图像宽度
-h:图像高度
-rot:图像旋转角度,只支持 0、90、180、270 度(这里说明一下,测试发现其他角度的输入都会被转换到这四个角度之上)
-o:图像输出地址,例如image.jpg,如果文件名为“-”,将输出发送至标准输出设备
-t:获取图像前等待时间,默认为5000,即5秒

raspistill -o test.png -w 400 -h 400 -t 1000
摄像头拍摄分辨率为400x400的图片,等待时间为1s,保存在当前文件夹的test.png

OCR平台返回的字符串格式

"1.0" encoding="UTF-8"?>
<data>
   
      0</status>
      比对完成</value>
   </message>
   
      type="21">
         "判定值"><![CDATA[0.9075357]]></item>  //对比率为0.9075357
         "判定结果"><![CDATA[是]]></item>	//判断为是同一个人
      </card>
   </cardsinfo>
</data>

例程

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

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

char post_info[500];
size_t ReadHandler( void *ptr, size_t size, size_t nmemb, void *stream)
{
     
	int buf_size=size*nmemb;
	char *buf=malloc(buf_size+1);
	memset(buf,'\0',buf_size+1);
	strncpy(buf,ptr,buf_size);
	//printf("%s\n",buf);
	strcat(post_info,buf);
	free(buf);
	return buf_size;
}
char* getImgBase64(char *img_name)
{
     
	int fd; 	
	int size=0;
	char cmd[30];
	char *img_buf;
	sprintf(cmd,"base64 %s >tmpfile.txt",img_name);//将图片Base64流保存到tmpfile.txt文件
	if(system(cmd)==256)
	{
     
		return NULL;
	}
	fd=open("tmpfile.txt",O_RDWR);
	size=lseek(fd,0,SEEK_END);
	//printf("size:%d\n",size);
	img_buf=malloc(size+2);
	memset(img_buf,'\0',size+2);
	lseek(fd,0,SEEK_SET);
	read(fd,img_buf,size);
    close(fd);
	//printf("%s\n",img_buf);
	system("rm tmpfile.txt");
	return img_buf;
}
char* getCamBase64()
{
     
        int fd;
        int size=0;
        char cmd[30];
        char *img_buf;
		system("raspistill -o cambuf.png -w 400 -h 400 -t 1000");//摄像头拍摄
		sleep(1);
        sprintf(cmd,"base64 %s >tmpfile.txt","cambuf.png");//将图片Base64流保存到tmpfile.txt文件
        if(system(cmd)==256)
        {
     
                return NULL;
        }
        fd=open("tmpfile.txt",O_RDWR);
        size=lseek(fd,0,SEEK_END);
        //printf("size:%d\n",size);
        img_buf=malloc(size+2);
        memset(img_buf,'\0',size+2);
        lseek(fd,0,SEEK_SET);
        read(fd,img_buf,size);
        close(fd);
        //printf("%s\n",img_buf);
        system("rm tmpfile.txt");
		system("rm cambuf.png");
        return img_buf;
}

bool postUrl(char* img1_name)
{
     
    CURL *curl;
    CURLcode res;
    char *PostString;
    char *img1;
    char *img2;
    char *key="yourkey";
    char *secret="yoursecret";
    int  typeId=21;
    char *format="xml";
	char *info_loop=NULL;
    float result_rate=0;
    img1=getImgBase64(img1_name);
    img2=getCamBase64();
    if((img1==NULL))
    {
     
    	printf("img dont exist!\n");
		return false;
    }
    PostString=malloc(strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+sizeof(typeId)+strlen(format));
    sprintf(PostString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,key,secret,typeId,format);
    curl = curl_easy_init();
    if (curl)
    {
     
		curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS,PostString);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ReadHandler);
        res = curl_easy_perform(curl);
		//printf("OK:%d\n",res);
        curl_easy_cleanup(curl);
		if((info_loop=strstr(post_info,"判定值"))!=NULL)
        {
     
				if((info_loop=strstr(info_loop,"CDATA"))!=NULL)
				{
     
					info_loop+=6;
					info_loop = strtok(info_loop, "]");
					result_rate=atof(info_loop);
					printf("the result rate:%f\n",result_rate);
					if(result_rate>0.75)
					{
     
						printf("the result:same person\n");
					}
					else
					{
     
						printf("the result:different person\n");
					}
				}
				else
				{
     
					printf("get result error!\n");
				}
        }
        else
        {
     
                printf("get result error!\n");
        }
    }
    free(img1);
    free(img2);
    return true;
}
int main(int argc,char* argv[])
{
     
    memset(post_info,'\0',500);
    if(argc!=2)
    {
     
    	return -1;
    }
    curl_global_init(CURL_GLOBAL_ALL);
    postUrl(argv[1]);
    curl_global_cleanup();
}

运行程序

./mycurl xxx (xxx为参考图片文件)

pi@raspberrypi:~/Desktop/mycurl/test $ ./mycurl hui.png
the result rate:0.874296
the result:same person

你可能感兴趣的:(c语言)