基于翔云OCR云平臺的人臉識別
本節通過翔云OCR云平臺來實作人臉識別,呼叫人臉對比API,通過https post方式向云服務器提交兩張需要對比的圖片Base64流以及其他資訊,云服務器處理后回傳判斷結果,
翔云OCR云平臺:https://www.netocr.com/
libcurl庫支持https版本的編譯方法
上一節安裝了libcurl庫,有以下步驟:
1、./configure --prefix=$PWD/_install 配置安裝路徑為當前路徑下_install檔案夾
2、make 編譯
3、make install 編譯后的檔案拷貝到指定檔案夾,此時才會出現_install檔案夾
我們會發現程式使用https協議不能連通,因為https=http+ssl,還需要安裝并使用ssl相關庫,可以查看下curl-7.71.1/docs/INSTALL.md,有以下說明:
The configure script always tries to find a working SSL library unless
explicitly told not to. If you have OpenSSL installed in the default search
path for your compiler/linker, you don’t need to do anything special. If you
have OpenSSL installed in/usr/local/ssl, you can run configure like:./configure --with-ssl
If you have OpenSSL installed somewhere else (for example,
/opt/OpenSSL) and
you have pkg-config installed, set the pkg-config path first, like this:env PKG_CONFIG_PATH=/opt/OpenSSL/lib/pkgconfig ./configure --with-ssl
Without pkg-config installed, use this:
./configure --with-ssl=/opt/OpenSSL
我們需要安裝下OpenSSL庫,./configure --with-ssl默認從/usr/local/路徑找OpenSSL庫并呼叫,
1、先移除_install檔案夾(之前libcurl安裝的路徑)
rm _install -rf
2、從網路上自動下載openssl-1.1.1a.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
3、解壓openssl-1.1.1a.tar.gz
tar -xvf opemssl-1.1.1a.tar.gz
4、瀏覽/openssl-1.1.1a/INSTALL檔案,查看安裝步驟說明:
If you want to just get on with it, do:
on Unix (again, this includes Mac OS/X):
$ ./config
$ make
$ make install
This will build and install OpenSSL in the default location, which is: Unix: normal installation directories under
/usr/local
5、Openssl庫安裝步驟:
(1)./config
(2)make
(3)sudo make install(安裝在usr/local,需要root模式)
6、Openssl庫安裝完成后,進入curl-7.71.1檔案夾,通過./configure配置
./configure --prefix=$PWD/_install --with-ssl
后面的步驟一樣:
make 編譯
make install 編譯后的檔案拷貝到指定檔案夾,此時才會出現_install檔案夾
libcurl庫安裝完成
翔云平臺API

用戶ocrKey和ocrSecret在個人中心頁面可以看到,

注意的是,img1和img2是圖片base64流,base64流簡單來說就是將圖片二進制資料通過Base64編碼成字串型別資料,
利用libcurl函式要設定的引數
1、url設定
curl_easy_setopt(curl, CURLOPT_URL, “https://netocr.com/api/faceliu.do”);
2、提交引數的設定
提交引數格式:&attr1=value1&attr2=value2…
sprintf(PostString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,key,secret,typeId,format);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,PostString);
3、回傳的資料(xml格式)
<?xml version="1.0" encoding="UTF-8"?>
<data>
<message>
<status>0</status>
<value>比對完成</value>
</message>
<cardsinfo>
<card type="21">
<item desc="判定值"><![CDATA[0.9075357]]></item> //對比率為0.9075357
<item desc="判定結果"><![CDATA[是]]></item> //判斷為是同一個人
</card>
</cardsinfo>
</data>
shell指令補充
將圖片資料進行base64編碼,得到圖片base64流
base64 xxx (xxx為圖片檔案,可以為png、jpg等格式)
例程
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#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* getBase64(char *img_name)
{
int fd;
int size=0;
char cmd[30];
char *img_buf;
sprintf(cmd,"base64 %s >tmpfile.txt",img_name);
if(system(cmd)==256)
{
return NULL;
}
fd=open("tmpfile.txt",O_RDWR);
size=lseek(fd,0,SEEK_END);
img_buf=malloc(size+2);
memset(img_buf,'\0',size+2);
lseek(fd,0,SEEK_SET);
read(fd,img_buf,size);
close(fd);
system("rm tmpfile.txt");
return img_buf;
}
bool postUrl(char* img1_name,char* img2_name)
{
CURL *curl;
CURLcode res;
char *PostString;
char *img1;
char *img2;
char *key="yourkey";
char *secret="yoursecret";
int typeId=21;
char *format="xml";
img1=getBase64(img1_name);
img2=getBase64(img2_name);
if((img1==NULL)||(img2==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(strstr(post_info,"是")!=NULL)
{
printf("same person\n");
}
else
{
printf("different person\n");
}
}
return true;
}
int main(int argc,char* argv[])
{
memset(post_info,'\0',500);
if(argc!=3)
{
return -1;
}
curl_global_init(CURL_GLOBAL_ALL);
postUrl(argv[1],argv[2]);
curl_global_cleanup();
}
代碼運行
通過運行如下命令,來進行人臉對比,img1和img2代表兩張要對比的圖片,
./mycurl img1 img2
準備圖片檔案



從左到右檔案名依次是reba1.png、reba2.png、xiang1.png,
運行結果
sh@ubuntu:~/Desktop/mycurl/test$ export LD_LIBRARY_PATH="/home/sh/Desktop/mycurl/curl-7.71.1/_install/lib"
sh@ubuntu:~/Desktop/mycurl/test$ gcc demo3.c -I /home/sh/Desktop/mycurl/curl-7.71.1/_install/include -lcurl -L /home/sh/Desktop/mycurl/curl-7.71.1/_install/lib -o mycurl
sh@ubuntu:~/Desktop/mycurl/test$ ./mycurl reba1.png reba2.png
<?xml version="1.0" encoding="UTF-8"?>
<data>
<message>
<status>0</status>
<value>比對完成</value>
</message>
<cardsinfo>
<card type="21">
<item desc="判定值"><![CDATA[0.9565811]]></item>
<item desc="判定結果"><![CDATA[是]]></item>
</card>
</cardsinfo>
</data>
OK:0
same person
sh@ubuntu:~/Desktop/mycurl/test$ ./mycurl reba1.png xiang1.png
<?xml version="1.0" encoding="UTF-8"?>
<data>
<message>
<status>0</status>
<value>比對完成</value>
</message>
<cardsinfo>
<card type="21">
<item desc="判定值"><![CDATA[0.5085605]]></item>
<item desc="判定結果"><![CDATA[否]]></item>
</card>
</cardsinfo>
</data>
OK:0
different person
總結
本節僅僅是兩張已準備好的圖片進行對比,下節采用攝像頭采集圖片的方式進行人臉識別,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321155.html
標籤:其他
上一篇:如何以角度發送卷曲請求
下一篇:前端部署服務器記錄
