使用libcurl库进行人脸识别和车牌识别

使用libcurl库进行人脸识别和车牌识别

基于ocr识别软件

基于bs识别 b浏览器 s服务 HTTP网路通信协议
用C语言代码发起网络请求,在翔云人工开放平台完成识别的功能

curl库的作用是访问http和HTTPS网站 HTTPS加密的网站

通过curl库访问翔云人工开放平台实现人脸识别
由于翔云人工开放平台是HTTPS格式的网站,接口地址https://netocr.com/api/faceliu.do,需要在curl库安装文件的基础上加上ssl
–with-ssl添加支持HTTPS格式协议,默认为http,翔云为HTTPS的
./configure --prefix=$PWD/_install --with-ssl

char img1[20];
char img2[20];
char *key=" 5PLKp9A738G938ruPHXy9c";
char *secret="83d03f0d64fa4caeaff4bbe44fa5290f";
int typeld=21;
char *format="xml";

通过定义和赋值图片给的12个参数实现人脸识别的初始化,在把参数传给curl库的函数来实现访问翔云和返回人脸识别的结果

在这里插入图片描述

人脸识别的图片是base64格式
base64是linux自带的图片格式转换工具
用法:base64 img.jpg 工具+图片名
base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。
Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读。
system(“base64 img.jfif > tmpfile”);//执行base64图片转换命令,把结果保存在tmpfile文件中

人脸识别的方案有两种
1、直接上传两张图做为参数
2、通过摄像头拍照获取照片在作为参数打开图片进行base64 转换
人脸识别的代码

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

       #include <unistd.h>

#define true 1  //C语言中没有bool类型,bool类型是C++的,以下三行是C语言定义的bool类型
#define false 0
typedef unsigned bool;
//读取https的结果
size_t readfunction( void *ptr, size_t size, size_t nmemb, void *stream){//规定的函数原型
	char buff[1024]={'\0'};
	strncpy(buff,ptr,1024);
	printf("===============================getdata===================================\n");
	printf("%s\n",buff);
	
	
	
}
char* getBase( char*path){
	char cmd[128];
	sprintf(cmd,"base64 %s > tmpfile",path);
	printf("cmd:%s\n",cmd);
	system(cmd);//执行base64图片转换命令,把结果保存在tmpfile文件中
	int fd=open("./tmpfile",O_RDWR,0777);
	int len=lseek(fd,0,SEEK_END);//获取base64格式的图片大小
	lseek(fd,0,SEEK_SET);
	char* buf=(char*)malloc(len);
	memset(buf,'\0',len);
	read(fd,buf,len);
	close(fd);
	system("rm -rf tmpfile");
	return buf;
	
	
}
bool postUrl(char* opto, char* poto1)
{
    CURL *curl;
    CURLcode res;
	char *key="5PLKp9A738G938ruPHXy9c";
	char *secret="83d03f0d64fa4caeaff4bbe44fa5290f";
	int typeId=21;
	char *format="xml";
	char *postData;
	char *buf1=getBase(opto);
	char *buf2=getBase(poto1);
	int llen=strlen(buf1)+strlen(buf2)+strlen(key)+224;
	postData=(char*)malloc(llen);
	memset(postData,'\0',llen);
	sprintf(postData,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",buf1,
	buf2,key,secret,typeId,format);//每个参数用&分隔,打印到postData
    curl = curl_easy_init();
    if (curl)
    {
		
        //curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
        //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86");    // 指定post内容
		 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);    // 指定post内容
		//curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头内容输出到fp指向的文件
		// CURLOPT_WRITEFUNCTION选项将http头内容作为参数调用readfunction函数,与CURLOPT_WRITEDATA是文件
		curl_easy_setopt(curl,  CURLOPT_WRITEFUNCTION, readfunction); //是函数  将返回的http头内容传递给指向的函数
        curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // 指定url
        res = curl_easy_perform(curl);
		printf("%d\n",res);
        curl_easy_cleanup(curl);
    }

    return true;
}
int main(int argc,char** argv)//char** argv   char* argv[]  都是指针数组
{
	if(argc!=3){
		printf("执行有误,终端输入:./可执行文件  图片1 图片2\n");
		return -1;
		
	}
    postUrl(argv[1],argv[2]);
    printf("img1:%s img2:%s\n",argv[1],argv[2]);
}

车牌识别

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

       #include <unistd.h>

#define true 1  //C语言中没有bool类型,bool类型是C++的,以下三行是C语言定义的bool类型
#define false 0
typedef unsigned bool;
//读取https的结果
size_t readfunction( void *ptr, size_t size, size_t nmemb, void *stream){//规定的函数原型
	char buff[1024]={'\0'};
	strncpy(buff,ptr,1024);
	printf("===============================getdata===================================\n");
	printf("%s\n",buff);
	
	
	
}
char* getBase( char*path){
	char cmd[128];
	sprintf(cmd,"base64 %s > tmpfile3",path);
	printf("cmd:%s\n",cmd);
	system(cmd);//执行base64图片转换命令,把结果保存在tmpfile文件中
	int fd=open("./tmpfile3",O_RDWR,0777);
	int len=lseek(fd,0,SEEK_END);//获取base64格式的图片大小
	lseek(fd,0,SEEK_SET);
	char* buf=(char*)malloc(len);
	memset(buf,'\0',len);
	read(fd,buf,len);
	close(fd);
	system("rm -rf tmpfile3");
	return buf;
	
	
}
bool postUrl(char* poto)
{
    CURL *curl;
    CURLcode res;
	char *key="5PLKp9A738G938ruPHXy9c";
	char *secret="83d03f0d64fa4caeaff4bbe44fa5290f";
	int typeId=19;
	char *format="xml";
	char *postData;
	char *buf1=getBase(poto);	
	int llen=strlen(buf1)+strlen(key)+324;
	postData=(char*)malloc(llen);
	memset(postData,'\0',llen);
	sprintf(postData,"&img=%s&key=%s&secret=%s&typeId=%d&format=%s",buf1,key,secret,typeId,format);//每个参数用&分隔,打印到postData
          curl = curl_easy_init();
    if (curl)
    {
		
        //curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
        //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86");    // 指定post内容
		 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);    // 指定post内容
		//curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头内容输出到fp指向的文件
		// CURLOPT_WRITEFUNCTION选项将http头内容作为参数调用readfunction函数,与CURLOPT_WRITEDATA是文件
		curl_easy_setopt(curl,  CURLOPT_WRITEFUNCTION, readfunction); //是函数  将返回的http头内容传递给指向的函数
        curl_easy_setopt(curl, CURLOPT_URL,"https://netocr.com/api/recogliu.do");   // 指定url
        res = curl_easy_perform(curl);
		printf("%d\n",res);
        curl_easy_cleanup(curl);
    }

    return true;
}
int main(int argc,char** argv)
{

    postUrl(argv[1]);
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值