智能家居项目人脸识别功能补充

人脸识别功能添加

  • mjpg-streamer软件
  • libcurl库
    • 使用介绍
    • 依赖库OpenSSL[^5]
    • 示例代码
    • 用函数处理获得的数据
  • 人脸识别判定测试代码

mjpg-streamer软件

树莓派通过mjpg-streamer软件控制摄像头1

mjpg-streamer软件2使用步骤

mjpg-streamer软件树莓派3摄像头安装检查

make all #编译
sudo make install #安装

vi start.sh
打开执行脚本

./mjpg_streamer -i “./input_raspicam.so” -o “./output_http.so -w ./www”
写入此条指令

http://IP地址:8080
查看摄像头

libcurl库

libcurl简介4

libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传, HTTP基本表单上传,代理,cookies,和用户认证。

libcurl的官网 http://curl.haxx.se/

库下载https://github.com/curl/curl/releases/tag/curl-7_71_1

libcurl的使用

调用curl_global_init()初始化libcurl

调用curl_easy_init()函数得到 easy interface型指针

调用curl_easy_setopt()设置传输选项

根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务
调用curl_easy_perform()函数完成传输任务
调用curl_easy_cleanup()释放内存

使用介绍

拿到第三方库时,一般要注意看README文件,有时直接在第一层文件夹,有时在里面的docs文件夹里

然后就可以去看INSTALL,文件,看怎么安装库,产看里面的一些安装介绍指令

开源库curl-7.71.1.tar

使用步骤指令

./configure
make
make test (optional)
make install

./configure --prefix=/path/to/curl/tree
./configure --prefix=$PWD/_install
配置安装路径

./configure --with-ssl
配置加密协议https

–host=arm-linux-gnueabihf-gcc
选择交叉编译工具

实际执行

./configure --prefix=$PWD/_install --with-ssl
配置

make
编译

make install
安装

快捷安装libcurl库指令
sudo apt install libssl-dev libcurl4-openssl-dev
gcc xxx.c -lcurl

依赖库OpenSSL5

在使用post做人脸识别时,需要用到OpenSSL 支持https

OpenSSL_使wget支持https

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar -vxf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a/
./config  //配置指令,后面不跟参数,就安装到默认的目录
make  // 编译配置文件
sudo make install //安装OpenSSL,因为前面选了默认安装路径,所以需要权限加sudo

示例代码

1.基本的http GET/POST操作

#include <stdio.h>
#include <curl/curl.h>
#define true 1
#define false 0
typedef unsigned int bool;

bool getUrl(char *filename)
{
    CURL *curl;
    CURLcode res;
    FILE *fp;
    if ((fp = fopen(filename, "w")) == NULL)  // 返回结果用文件存储
        return false;
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Accept: Agent-007");
    curl = curl_easy_init();    // 初始化
    if (curl)
    {
        //curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
        curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头输出到fp指向的文件
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的html主体数据输出到fp指向的文件
        res = curl_easy_perform(curl);   // 执行
        if (res != 0) {

            curl_slist_free_all(headers);
            curl_easy_cleanup(curl);
        }
        fclose(fp);
        return true;
    }
}
bool postUrl(char *filename)
{
    CURL *curl;
    CURLcode res;
    FILE *fp;
    if ((fp = fopen(filename, "w")) == NULL)
        return false;
    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_PROXY, "10.99.60.201:8080");
        curl_easy_setopt(curl, CURLOPT_URL, " http://mail.sina.com.cn/cgi-bin/login.cgi ");   // 指定url
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    fclose(fp);
    return true;
}
int main(void)
{
    getUrl("/tmp/get.html");
    postUrl("/tmp/post.html");
}

文件编译
gcc demo1.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl

设置临时环境变量:
export LD_LIBRARY_PATH=./curl-7.71.1/_install/lib/

执行程序
./a.out

用函数处理获得的数据

#include <stdio.h>
#include <curl/curl.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("==================read Data========================\n");
        printf("%s\n",buf);
}

bool getUrl(char *filename)
{
        CURL *curl;
        CURLcode res;
        FILE *fp; 
        if ((fp = fopen(filename, "w")) == NULL)  // 返回结果用文件存储
                return false;
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Accept: Agent-007");
        curl = curl_easy_init();    // 初始化
        if (curl)
        {
                //curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
                curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
                curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData); //将返回的http头输出到readData函数指向的操作
                //curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头输出到fp指向的文件
                //curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的html主体数据输出到fp指向的文件
                res = curl_easy_perform(curl);   // 执行
                if (res != 0) {

                        curl_slist_free_all(headers);
                        curl_easy_cleanup(curl);
                }
                fclose(fp);
                return true;
        }
}
int main(void)
{
        getUrl("/tmp/get.html");
        return 0;
}
 

res = curl_easy_perform(curl);
res返回错误码6
curl Unsupported protocol: https 问题解决7

人脸识别判定测试代码

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.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[1024] = {'\0'};

size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
        strncpy(buf,ptr,1024);
        printf("%s\n",buf);
}
char *getPicBase64FormFile(char *fliePath)
{
        int fd,flielen;
        char *img;

        char cmd[128] = {'\0'};
        sprintf(cmd,"base64 %s > tmpFile",fliePath);
        system(cmd);

        fd = open("./tmpFile",O_RDWR);
        flielen = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        img = (char *)malloc(flielen+2);
        memset(img,'\0',flielen+2);
        read(fd,img,flielen);
        close(fd);

        system("rm -f tmpFile");

        return img;
}

bool postUrl()
{
        CURL *curl;
        CURLcode res;
        char *postString;

        char *img1;
        char *img2;
        char *key = "7gv6hpWuu64dAPGXjiPveD";
        char *secret = "91d0491f2ffa4cd2a54c81fc4c8762b6";
        int typeId = 21;
        char *format = "xml";

        img1 = getPicBase64FormFile("./1.jpg");
        img2 = getPicBase64FormFile("./2.jpg");


        int len = strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+124;
        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,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); //将返回的http头输出到readData函数指向的操作
                res = curl_easy_perform(curl);
                printf("ok-res:%d\n",res);
                if(strstr(buf,"是") !=NULL){
                        printf("The two are the same\n");
                }else{
                        printf("These two are different\n");
                }
                curl_easy_cleanup(curl);
        }
        free(img1);
        free(img2);
        return true;
}
int main(void)
{
        postUrl();
        return 0;
}


  1. 树莓派mjpg-streamer实现监控拍照功能 ↩︎

  2. mjpg-streamer软件 ↩︎

  3. mjpg-streamer配置CSI摄像头使用 ↩︎

  4. libcurl库参考文档 ↩︎

  5. OpenSSL_使wget支持https ↩︎

  6. curl_easy_perform 返回值说明 ↩︎

  7. res错误码 1: unsupported protocol ↩︎

你可能感兴趣的:(C语言,linux,人脸识别,https,ssl,http)