赞
踩
目录
● 要通过libcurl库实现人脸识别,就需要libcurl库支持https协议。在上一篇文章中介绍了通过libcurl库来访问百度网页,只是基于http协议。
● 要让libcurl库支持https协议实现人脸识别,就需要安装openssl这个库。
● http与https:简言之,https比http多了加密协议,具体体现后面会说明。
● 此篇人脸识别介绍主要目的是判断两张人脸图片的相似程度或者接近程度。
① wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz //下载
② tar xvf openssl-1.1.1a.tar.gz //解压
③ cd openssl-1.1.1a //进入解压皓的openssl-1.1.1a目录下
④ ./config //配置在系统的默认位置,省的我们调库的时候再去找库的位置
⑤ make //编译
⑥ sudo make install //最后一步安装完成
在curl-7.71.1目录下操作
① ./configure --prefix=$PWD/_install --with-ssl
② make
③ make install
用手机号注册翔云人工智能开放平台的账号,然后购买关于人脸识别的产品,刷新页面回到个人中心就能看到两个OCR码。
人脸识别http://netocr.com/face.html
- #include <stdio.h>
- #include <curl/curl.h>
- #include <string.h>
- #include <stdlib.h>
-
- #define true 1
- #define false 0
- typedef unsigned int bool;
-
- size_t readData(void *ptr,size_t size,size_t nmemb,void *stream)
- {
- char buf[10240] = {'\0'};
- strncpy(buf,ptr,1024);
- printf("=================================get Data=============================\n");
- printf("%s\n",buf);
- }
-
- bool postUrl()
- {
- CURL *curl;
- CURLcode res;
- char *postString;
-
- char img1[12];
- char img2[12];
-
- char *key = "EUiBxxxxxxxxxxxxxxxgva"; //填翔云平台个人中心里对应显示的内容
- char *secret = "e405xxxxxxxxxxxxxxxxxxxxxxx49c"; //填翔云平台个人中心里对应显示的内容
- int typeId = 21;
- char *format = "xml";
-
- postString = (char *)malloc(strlen(key)+strlen(secret)+2048);
-
- sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s","","",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); //将返回的http头输出到fp指向的文件
- res = curl_easy_perform(curl);
- printf("OK:%d\n",res);
-
- curl_easy_cleanup(curl);
- }
- return true;
- }
- int main(void)
- {
- // getUrl("/tmp/get.html");
- postUrl();
- }
编译:
gcc demo2.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib -lcurl
● jpg格式的图片是压缩过的bmp格式的图片;
● 上传两张人脸照片到虚拟机
● Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。
● Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读。
● Base64由于以上优点被广泛应用于计算机的各个领域,然而由于输出内容中包括两个以上“符号类”字符(+, /, =),不同的应用场景又分别研制了Base64的各种“变种”。为统一和规范化Base64的输出,Base62x被视为无符号化的改进版本
- #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 0
- typedef unsigned int bool;
- char buf[10240] = {'\0'};
-
- size_t readData(void *ptr,size_t size,size_t nmemb,void *stream)
- {
- strncpy(buf,ptr,1024);
- }
-
- char* getPicBase64FromFile(char *filePath)
- {
- char *bufPic;
- char cmd[128] = {'\0'};
-
- sprintf(cmd,"base64 %s > tmpFile",filePath);
- system(cmd);
-
- int fd = open("./tmpFile",O_RDWR);
- int filelen = lseek(fd,0,SEEK_END);
- lseek(fd,0,SEEK_SET);
- bufPic =(char *)malloc(filelen+2);
- memset(bufPic,0,filelen+2);
- read(fd,bufPic,filelen);
- close(fd);
-
- system("rm -f tmpFile");
- return bufPic;
- }
- bool postUrl()
- {
- CURL *curl;
- CURLcode res;
- char *postString;
-
- char *key = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; //对应翔云平台上的内容填写
- char *secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
- int typeId = 21;
- char *format = "xml";
-
- char *bufPic1 = getPicBase64FromFile("./liudehua.jpg");
- char *bufPic2 = getPicBase64FromFile("./liudehua2.jpg");
-
- int len = strlen(key)+strlen(secret)+strlen(bufPic1)+strlen(bufPic2)+124;
- postString = (char *)malloc(len);
- memset(postString,'\0',len);
-
- sprintf(postString,"img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
- bufPic1,bufPic2,key,secret,21,format);
-
- curl = curl_easy_init();
- if (curl)
- {
- 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); //将返回的http头输出到fp指向的文件
- res = curl_easy_perform(curl);
- printf("OK:%d\n",res);
-
- if(strstr(buf,"shi") != NULL){
- printf("the same Person\n");
- }else{
- printf("diff Person\n");
- }
- curl_easy_cleanup(curl);
- }
- return true;
- }
- int main(void)
- {
- postUrl();
- }
编译:
gcc demo3.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib -lcurl
● 如果两张照片接近就打印:the same Person
● 如过两张照片差距较大就打印:diff Person
● 上传一张车牌图片到虚拟机进行测试
● 翔云平台购买车牌识别相关业务,0.01块识别100次;
● 根据下图API文档编写代码;
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <curl/curl.h>
-
- #define true 1
- #define false 0
-
- 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);
- printf("%s\n",buf);
- }
-
- char *getPicBase64File(char *picFile)
- {
- char cmd[128] = {'\0'};
- sprintf(cmd, "base64 %s > tmpFile", picFile);
- system(cmd);
- int fd = open("./tmpFile",O_RDWR);
- int fileLen = lseek(fd, 0, SEEK_END);
- lseek(fd, 0, SEEK_SET);
- char *bufPic = (char *)malloc(fileLen);
- memset(bufPic, 0, fileLen);
- read(fd, bufPic, fileLen);
- close(fd);
-
- system("rm ./tmpFile");
- return bufPic;
- }
-
- bool postUrl()
- {
- CURL *curl;
- CURLcode res;
-
- char *postString;
-
- char *key = "xxxxxxxxxxxxxxxxxxxxxx"; //对应翔云平台给的信息填写
- char *secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; //对应翔云平台给的信息填写
-
- int typeId = 19;
- char *format = "xml";
-
- char *bufPic = getPicBase64File("./car.jpg");//车牌图片
-
- int len = strlen(key)+strlen(secret)+strlen(bufPic)+128;
- postString = (char *)malloc(len);
- memset(postString, 0, len);
- sprintf(postString, "&img=%s&key=%s&secret=%s&typeId=%d&format=%s",
- bufPic,key,secret,typeId,format);
-
- curl = curl_easy_init();
- if (curl)
- {
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);
- curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/recogliu.do");
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);
- res = curl_easy_perform(curl);
- printf("OK:%d\n",res);
-
- curl_easy_cleanup(curl);
- }
- return true;
- }
-
- int main(void)
- {
- postUrl();
- }
编译:
gcc car.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib -lcurl
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。