树莓派上实现人脸识别

参考博文:
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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃秃秃秃哇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值