参考博文:
https://blog.csdn.net/weixin_50438937/article/details/113763964?spm=1001.2014.3001.5502
不带实时监控功能
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <curl/curl.h>
#define true 1
#define false 0
typedef unsigned int bool;
char* faceRes = NULL;
char* getBase64(char* photoPath){// 获取照片(jpg格式)的Base64的值
char cmd[256] = {'\0'};
sprintf(cmd, "base64 %s > photoBase64File", photoPath);
system(cmd);// 通过执行"base64"这个指令即可得到照片的Base64值,在这里将得到的Base64值存放在photoBase64File文件中
int fd,size;
fd = open("./photoBase64File", O_RDWR);
size = lseek(fd, 0, SEEK_END) + 1;
lseek(fd, 0, SEEK_SET);
char* res = (char*)malloc(size);
memset(res, '\0', size);
read(fd, res, size);// 从photoBase64File文件中读取照片的Base64值
close(fd);
system("rm photoBase64File");
return res;
}
size_t handle(void *ptr, size_t size, size_t nmemb, void *stream){
//拷贝返回来的结果字段
int ssize = strlen(ptr) + 1;
faceRes = (char*)malloc(ssize);
memset(faceRes, '\0', ssize);
strncpy(faceRes, ptr, ssize);
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char* message = NULL;
// 调用getBase64()自定的函数获取存放在当前文件夹下的两个进行识别的图片的Base64值
char* img1 = getBase64("./host.jpg");
/*************************添加的代码**************************************/
//其中-w和-h:来设定照片的大小; -o:来设置生成的照片名字为visitor.jpg; ./:放在当前目录下
system("raspistill -w 700 -h 525 -o ./visitor.jpg");
/************************************************************************/
char* img2 = getBase64("./visitor.jpg");
// key值和secret值是在翔云官网->个人中心的OCR Key和OCR secret两个的值
char* key = "用自己的";
char* secret = "用自己的";
int typeId = 21;
char* format = "xml";
int size = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+strlen(format)+3;
message = (char*)malloc(size);
memset(message, '\0', size);
sprintf(message, "&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");// 指定url
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);// 指定post内容
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle);// 当拿到结果后,指定调用handle()该自定的函数进行处理
res = curl_easy_perform(curl);// 进行访问官网、进行人脸识别的操作
curl_easy_cleanup(curl);// 执行完后,对curl_easy_init()进行清理
}
if(res != 23){
printf("post failed!\n");
return false;
}else{
printf("post successful!\n");
}
printf("%s\n",faceRes);// 打印出人脸识别后返回来的结果字段
if(strstr(faceRes, "是") != NULL){// 如果返回来的结果字段中有“是”这个字眼,代表是同一个人
printf("This is the same person!\n");
}else{
printf("Tow different persons!\n");
}
free(faceRes);
free(img1);
free(img2);
}
int main(void)
{
postUrl();
return 0;
}
输出结果:
使用mjpg-streamer来进行拍照
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <curl/curl.h>
#define true 1
#define false 0
typedef unsigned int bool;
char* faceRes = NULL;
char* getBase64(char* photoPath){// 获取照片(jpg格式)的Base64的值
char cmd[256] = {'\0'};
sprintf(cmd, "base64 %s > photoBase64File", photoPath);
system(cmd);// 通过执行"base64"这个指令即可得到照片的Base64值,在这里将得到的Base64值存放在photoBase64File文件中
int fd,size;
fd = open("./photoBase64File", O_RDWR);
size = lseek(fd, 0, SEEK_END) + 1;
lseek(fd, 0, SEEK_SET);
char* res = (char*)malloc(size);
memset(res, '\0', size);
read(fd, res, size);// 从photoBase64File文件中读取照片的Base64值
close(fd);
system("rm photoBase64File");
return res;
}
size_t handle(void *ptr, size_t size, size_t nmemb, void *stream){
//拷贝返回来的结果字段
int ssize = strlen(ptr) + 1;
faceRes = (char*)malloc(ssize);
memset(faceRes, '\0', ssize);
strncpy(faceRes, ptr, ssize);
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char* message = NULL;
// 调用getBase64()自定的函数获取存放在当前文件夹下的两个进行识别的图片的Base64值
char* img1 = getBase64("./host.jpg");
/*************************添加的代码**************************************/
//openCamera为start.sh可执行文件的绝对地址,请根据自己的路径改
const char* openCamera = "/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh";
system(openCamera); //调用start.sh可执行文件
//获取访问者的照片
system("wget http://<树莓派ip地址>:8080/?action=snapshot -O ./visitor.jpg");
/************************************************************************/
char* img2 = getBase64("./visitor.jpg");
// key值和secret值是在翔云官网->个人中心的OCR Key和OCR secret两个的值
char* key = "用自己的";
char* secret = "用自己的";
int typeId = 21;
char* format = "xml";
int size = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+strlen(format)+3;
message = (char*)malloc(size);
memset(message, '\0', size);
sprintf(message, "&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");// 指定url
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);// 指定post内容
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle);// 当拿到结果后,指定调用handle()该自定的函数进行处理
res = curl_easy_perform(curl);// 进行访问官网、进行人脸识别的操作
curl_easy_cleanup(curl);// 执行完后,对curl_easy_init()进行清理
}
if(res != 23){
printf("post failed!\n");
return false;
}else{
printf("post successful!\n");
}
printf("%s\n",faceRes);// 打印出人脸识别后返回来的结果字段
if(strstr(faceRes, "是") != NULL){// 如果返回来的结果字段中有“是”这个字眼,代表是同一个人
printf("This is the same person!\n");
}else{
printf("Tow different persons!\n");
}
free(faceRes);
free(img1);
free(img2);
}
int main(void)
{
postUrl();
return 0;
}