首先在人工智能OCR识别平台翔云购买100次的人脸识别
改写上一篇内容的代码,为demo2.c:
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.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[1024] = {'\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 = "你的key";
char *secret = "你的secret";
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,typeId,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);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
编译:
gcc demo2.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl
结果返回值是1,那么哪里出问题了呢?
因为编译出来的libcurl没有支持openSSL,也就是不支持https
那么接下来就要编译openssl支持libcurl的https访问
首先删除我们编译的_install文件夹 :rm _install/ -rf
下载好openssl,我发布的资源里有openssl
进入解压好的openssl文件夹,接着执行以下命令
./config
make
sudo make install
进入curl目录重新对libcurl进行编译
./configure --prefix=$PWD/_install --with-ssl
make
make install
此时再次使用命令./a.tou
可以看到有返回值了,但是我们刚才没有传入图片,所以显示必传参数为空
首先下载两张照片,用于人脸识别
拷贝demo2.c为demo3.c
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.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[1024] = {'\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 = "你的key";
char *secret = "你的secret";
int typeId = 21;
char *format = "xml";
system("base64 LQ1.jpg > tempFile");
int fd = open("./tempFile",O_RDWR);
int fileLen = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char *bufPic1 = (char *)malloc(fileLen + 2);
memset(bufPic1, '\0',fileLen+2);
read(fd,bufPic1,fileLen);
close(fd);
system("base64 LQ2.jpg > tempFile");
fd = open("./tempFile",O_RDWR);
fileLen = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char *bufPic2 = (char *)malloc(fileLen + 2);
memset(bufPic2, '\0',fileLen+2);
read(fd,bufPic2,fileLen);
close(fd);
int len = strlen(key) + strlen(secret) + strlen(bufPic1) + strlen(bufPic2) + 128;
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,typeId,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);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
编译运行,结果:
可以看到判定结果为:是
对demo3.c进行优化,拷贝为demo4.c
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.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);
}
char* getPicBase64FromFile(char* filePath)
{
char* bufPic;
char cmd[128] = {'\0'};
sprintf(cmd,"base64 %s > tempFile",filePath);
system(cmd);
int fd = open("./tempFile",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 img1[12];
char img2[12];
char *key = "你的key";
char *secret = "你的secret";
int typeId = 21;
char *format = "xml";
char* bufPic1 = getPicBase64FromFile("./LQ1.jpg");
char* bufPic2 = getPicBase64FromFile("./LQ2.jpg");
int len = strlen(key) + strlen(secret) + strlen(bufPic1) + strlen(bufPic2) + 128;
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,typeId,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("Same person!\n");
}else{
printf("different person\n");
}
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
可以看到结果精简了很多:
学会了人脸识别,其实还可以做很多的项目,比如说发票识别,车牌识别等等,响应的API接口都可以在提供API产品的官网查看,这里我用的是翔云的API
上一篇:智能家居项目(十三)
下一篇:智能家居项目(十五)