本次使用的人脸识别平台为翔云平台
购买连接为:https://www.netocr.com/order/price.html
一、安装openssl库
1.1、下载openssl源码
//https://www.openssl.org/source/
//https://ftp.openssl.org/source/old/1.1.1
wget https://ftp.openssl.org/source/old/1.1.1/openssl-1.1.1i.tar.gz
1.2、解压、配置、编译并安装
tar -xzf openssl-1.1.1i.tar.gz
cd openssl-1.1.1i/
./config
make -j4
sudo make install
二、安装curl库
2.1下载curl源码
//https://github.com/curl/curl/releases/tag/curl-7_71_1
wget https://curl.se/download/curl-7.71.1.tar.bz2
2.2、解压、配置、编译并安装
安装时要按照支持ssl的
tar -xjf curl-7.71.1.tar.bz2
cd curl-7.71.1/
./configure --prefix=$HOME/curl --with-ssl
make -j4
make install
配置环境
export LD_LIBRARY_PATH=./curl-7.71.1/_install/lib/
或
export C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/curl/include
export LIBRARY_PATH=$LIBRARY_PATH:$HOME/curl/lib
配置环境方法二
执行以下命令生效
sudo vi /etc/profile
打开文件后添加
export C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/curl/include
export LIBRARY_PATH=$LIBRARY_PATH:$HOME/curl/lib
改写完生效一下
source /etc/profile
代码样例
此图为我们上传所需参数
#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>
#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,10240);
if(strstr(buf,"是") != 0){
printf("yes");
}else{
printf("no");
}
}
char *getBase64FromPic(char * filePath)
{
char cmd[124] ={'\0'};
sprintf(cmd,"base64 %s >tmpfile",filePath);
system(cmd);
int fd;
int length;
fd = open("./tmpfile",O_RDWR);
length = lseek(fd,0,SEEK_END);
char *img2buf = (char *)malloc(length+8);
memset(img2buf,0,length+8);
lseek(fd,0,SEEK_SET);
read(fd,img2buf,length);
close(fd);
system("rm tmpfile");
return img2buf;
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
char *img1;
char *img2;
char *key = "写上你买来的key";
char *secret = "写上你买来的secret";
int typeId = 21;
char *format = "xml";
img1 = getBase64FromPic("liu1.jpeg");
img2 = getBase64FromPic("liu2.jpeg");
char *postString;
int len = strlen(key)+ strlen(secret)+ strlen(img1)+ strlen(img2)+ 1024;
postString = (char *)malloc(len);
memset(postString,0, len);
sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,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_PROXY, "10.99.60.201:8080");
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);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl("/tmp/post.html");
}
编译
gcc demo7.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl -o demo7
连接上刚才解压的curl库
树莓派摄像头调用获取照片
1.启动摄像头, 相机有红灯亮起时说明开启
到mjpg路径内修改start脚本
vi start.sh
红色那句修改为
./mjpg_streamer -i “./input_raspicam.so” -o “./output_http.so -w ./www”
运行 ./start.h即可运行
或
用绝对路径运行
/usr/local/bin/mjpg_streamer -i “/usr/local/lib/mjpg-streamer/input_uvc.so -n -f 30 -r 1280x720” -o “/usr/local/lib/mjpg-streamer/output_http.so -p 8080 -w /usr/local/share/mjpg-streamer/www”
获取图片方法
1.调用树莓派摄像头拍照,在五秒中后拍摄
system("raspistill -o file2.jpg");
2. 2浏览器进入网页查看视频流
浏览器输入 http://ip:8080
ip为树莓派ip地址
2.2 浏览器获取视频中的一帧图片
system("wget http://192.168.xxx.xxx:8080/?action=snapshot -q -O /home/pi/xxx.jpg")
2.3 C语言打开摄像头
system("/usr/local/bin/mjpg_streamer -i \"/usr/local/lib/mjpg-streamer/input_uvc.so -n -f 30 -r 1280x720\" -o \"/usr/local/lib/mjpg-streamer/output_http.so -p 8080 -w /usr/local/share/mjpg-streamer/www\" &")
错误锦集
错误一:从source insight放到树莓派上报错
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>-8产品类型错误
这是由于source insight 上的编码格式为gb2310 树莓派上的编码为utf-8 造成的
file->reload as encode 里面修改完重新写一下程序即可
错误二:出现了在linux上没有的缺少文件 错误
/curl-7.71.1/_install/lib//libcurl.so: undefined reference to `SSL_CTX_set_keylog_callback@OPENSSL_1_1_1'
./curl-7.71.1/_install/lib//libcurl.so: undefined reference to `SSL_CTX_set_post_handshake_auth@OPENSSL_1_1_1'
./curl-7.71.1/_install/lib//libcurl.so: undefined reference to `SSL_CTX_set_ciphersuites@OPENSSL_1_1_1'
多加上这个连接
-lssl -lcrypto
错误三:运行./a.out 时报错
openssl: /usr/lib/x86_64-linux-gnu/libssl.so.1.1: version OPENSSL_1_1_1’ not found (required by openssl)
这种情况是LD_LIBRARY_PATH这个环境变量没有指定。
解决方法:$ export LD_LIBRARY_PATH=/usr/local/lib
最终成品代码
#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>
#include <stdlib.h>
#include <wiringPi.h>
#define true 1
#define false 0
typedef unsigned int bool;
int flag=2;
size_t readData( void *ptr, size_t size,size_t nmemb, void *stream)
{
char buf[10240] = {'\0'};
strncpy(buf,ptr,10240);
if(strstr(buf,"是") != 0){
flag=1;
}else{
flag=0;
}
printf("%d\n",flag);
}
char *getBase64FromPic(char * filePath)
{
char cmd[124] ={'\0'};
sprintf(cmd,"base64 %s >tmpfile",filePath);
system(cmd);
int fd;
int length;
fd = open("./tmpfile",O_RDWR);
length = lseek(fd,0,SEEK_END);
char *img2buf = (char *)malloc(length+8);
memset(img2buf,0,length+8);
lseek(fd,0,SEEK_SET);
read(fd,img2buf,length);
close(fd);
system("rm tmpfile");
return img2buf;
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
char *img1;
char *img2;
char *key = "";
char *secret = "";
int typeId = 21;
char *format = "xml";
img1 = getBase64FromPic("file1.jpg");
img2 = getBase64FromPic("liu2.jpeg");
char *postString;
int len = strlen(key)+ strlen(secret)+ strlen(img1)+ strlen(img2)+ 1024;
postString = (char *)malloc(len);
memset(postString,0, len);
sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,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_PROXY, "10.99.60.201:8080");
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);
curl_easy_cleanup(curl);
}
return true;
}
int OpenDoor()
{
wiringPiSetup();
pinMode(29,OUTPUT); //初始化引脚为输出口
digitalWrite(29,HIGH);
if(flag==0){
digitalWrite(29,LOW); //把引脚置为高电平
sleep(1);
digitalWrite(29,HIGH);
}
else if(flag ==1 ){
}
printf("%d\n",flag);
return 0;
}
int main(void)
{
system("raspistill -o file1.jpg");
postUrl("/tmp/post.html");
OpenDoor();
}