当前位置:   article > 正文

树莓派实现人脸识别

树莓派实现人脸识别

本次使用的人脸识别平台为翔云平台
购买连接为: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
  • 3

1.2、解压、配置、编译并安装

tar -xzf openssl-1.1.1i.tar.gz
cd openssl-1.1.1i/

  • 1
  • 2
  • 3
./config
make -j4
sudo make install
  • 1
  • 2
  • 3

二、安装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
  • 1
  • 2

2.2、解压、配置、编译并安装

安装时要按照支持ssl的

在这里插入图片描述

tar -xjf curl-7.71.1.tar.bz2
cd curl-7.71.1/
  • 1
  • 2
./configure --prefix=$HOME/curl --with-ssl
make -j4
make install
  • 1
  • 2
  • 3

配置环境

export LD_LIBRARY_PATH=./curl-7.71.1/_install/lib/

  • 1
  • 2

export C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/curl/include
export LIBRARY_PATH=$LIBRARY_PATH:$HOME/curl/lib


  • 1
  • 2
  • 3
  • 4

配置环境方法二

执行以下命令生效

sudo vi /etc/profile
  • 1

打开文件后添加

export C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/curl/include
export LIBRARY_PATH=$LIBRARY_PATH:$HOME/curl/lib
  • 1
  • 2

改写完生效一下

source /etc/profile
  • 1

代码样例

在这里插入图片描述
此图为我们上传所需参数

#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");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

编译

 gcc demo7.c  -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/  -lcurl -o demo7
  • 1

连接上刚才解压的curl库


树莓派摄像头调用获取照片

1.启动摄像头, 相机有红灯亮起时说明开启

到mjpg路径内修改start脚本
vi start.sh 
  • 1

在这里插入图片描述
红色那句修改为

./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");   
  • 1

2. 2浏览器进入网页查看视频流


浏览器输入 http://ip:8080
ip为树莓派ip地址
  • 1
  • 2
  • 3

2.2 浏览器获取视频中的一帧图片

system("wget http://192.168.xxx.xxx:8080/?action=snapshot -q -O /home/pi/xxx.jpg")

  • 1
  • 2

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\" &")
  • 1

错误锦集

错误一:从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'

  • 1
  • 2
  • 3
  • 4

多加上这个连接

-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)

  • 1
  • 2

这种情况是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();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/495020
推荐阅读
相关标签
  

闽ICP备14008679号