人脸识别的环境搭建可以看一下https://blog.csdn.net/weixin_48856218/article/details/124407139?spm=1001.2014.3001.5501 我这里就不再赘述了
翔云方案
翔云官网https://www.netocr.com/ 代码编写 需要修改的地方: 填写自己的翔云账号的 key 和 secret :
char *key = "";
char *secret = "";
修改为自己图片的名称:
char *bufPic1 = getPicBase64FromFile("./img1.jpg");
char *bufPic2 = getPicBase64FromFile("./img2.jpg");
xianyundemo.c
#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>
#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);
}
char* getPicBase64FromFile(char *filePath)
{
char *bufPic;
char cmd[128] = {'\0'};
sprintf(cmd, "base64 %s > tmpFile",filePath);
system(cmd);
int fd = open("./tmpFile", O_RDWR);
int filelen = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
bufPic = (char *)malloc(filelen + 2);
memset(bufPic, '\0', filelen+2);
read(fd, bufPic, filelen);
close(fd);
system("rm -f tmpFile");
return bufPic;
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char *key = "";
char *secret = "";
int typeId = 21;
char *format = "xml";
char *bufPic1 = getPicBase64FromFile("./img1.jpg");
char *bufPic2 = getPicBase64FromFile("./img2.jpg");
int len = strlen(key)+strlen(secret)+strlen(bufPic1)+strlen(bufPic2)+124;
postString = (char *)malloc(len);
memset(postString, '\0', len);
sprintf(postString, "&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
bufPic1,bufPic2,key,secret,21,format);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);
curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);
res = curl_easy_perform(curl);
printf("OK:%d\n",res);
if(strstr(buf,"是") != NULL){
printf("yes\n");
}else{
printf("no\n");
}
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
编译
gcc xianyundemo.c -lcurl -lssl -o xyface
百度方案
官网地址:需要自己注册认证领取免费资源,网上注册教程很多,自行百度 https://login.bce.baidu.com/?redirect=https%3A%2F%2Fconsole.bce.baidu.com%2Fai%2F%3F_%3D1650961199726%26fromai%3D1#/ai/face/report/index~apiId=206 需要修改的地方: 填写自己的百度账号的 key 和 secret :
char AK[] = "";
char SK[] = "";
修改对应图片名:
char *image1_base64 = getbase64("img1.jpg");
char *image2_base64 = getbase64("img2.jpg");
baidudemo.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
size_t access_token_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
char *buf = (char *)malloc (size * nmemb + 8 );
memset(buf,'\0',size * nmemb + 8);
strncpy(buf, ptr, size * nmemb);
cJSON *json= cJSON_Parse(buf);
char *access_token_result = (char *)stream;
strncpy(access_token_result,cJSON_GetObjectItem(json,"access_token")->valuestring,128);
cJSON_Delete(json);
free(buf);
return size * nmemb;
}
int post_access_token(char *access_token)
{
CURL *curl;
CURLcode result_code;
int error_code = 0;
char url[256] = {0};
char access_token_url[] = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials";
char AK[] = "";
char SK[] = "";
char access_token_result[128] = {0};
curl = curl_easy_init();
if (curl) {
sprintf(url,"%s&client_id=%s&client_secret=%s",access_token_url,AK,SK);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, access_token_result);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, access_token_callback);
result_code = curl_easy_perform(curl);
if (result_code != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(result_code));
return 1;
}
strcpy(access_token,access_token_result);
curl_easy_cleanup(curl);
error_code = 0;
} else {
fprintf(stderr, "curl_easy_init() failed.");
error_code = 1;
}
return error_code;
}
size_t faceMatch_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
char *buf = (char *)malloc (size * nmemb + 8 );
memset(buf,'\0',size * nmemb + 8);
strncpy(buf, ptr, size * nmemb);
cJSON *json = cJSON_Parse(buf);
printf("data:%s\n",cJSON_Print(json));
if(strstr(cJSON_Print(json),"SUCCESS") ==NULL){
*((double *)stream) = 0;
}else{
cJSON *result = cJSON_GetObjectItem(json,"result");
double *faceMatch_result = (double *)stream;
*faceMatch_result = cJSON_GetObjectItem(result,"score")->valuedouble;
}
cJSON_Delete(json);
free(buf);
return size * nmemb;
}
int post_faceMatch(double *faceMatch, char *access_token)
{
char url[256] = {0};
char request_url[] = "https://aip.baidubce.com/rest/2.0/face/v3/match";
sprintf(url,"%s?access_token=%s",request_url,access_token);
char *getbase64(char *photoname);
char image[] = "\"image\": ";
char image_type[] = "\"image_type\": \"BASE64\"";
char *image1_base64 = getbase64("img1.jpg");
char *image2_base64 = getbase64("img2.jpg");
char *params = (char *)malloc(strlen(image1_base64) + strlen(image2_base64)
+ 2 * strlen(image) + 2 * strlen(image_type) + 128 );
sprintf(params,"[{%s\"%s\", %s}, {%s\"%s\", %s}]",image,image1_base64,image_type
,image,image2_base64,image_type);
cJSON *json = cJSON_Parse(params);
CURL *curl = NULL;
CURLcode result_code;
int is_success;
curl = curl_easy_init();
double faceMatch_result;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSON_Print(json));
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &faceMatch_result);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, faceMatch_callback);
result_code = curl_easy_perform(curl);
free(image1_base64);
free(image2_base64);
free(params);
cJSON_Delete(json);
if (result_code != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(result_code));
is_success = 1;
return is_success;
}
*faceMatch = faceMatch_result;
curl_easy_cleanup(curl);
is_success = 0;
} else {
fprintf(stderr, "curl_easy_init() failed.");
is_success = 1;
}
return is_success;
}
char *getbase64(char *photoname)
{
char order[128] = {0};
char file[32] = {0};
char *base64 = NULL;
sprintf(order,"base64 %s > %s_base64",photoname,photoname);
system(order);
sprintf(file,"%s_base64",photoname);
int fd = open(file,O_RDWR);
int len = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
base64 = (char *)malloc(len+1);
read(fd,base64,len);
base64[len] = '\0';
return base64;
}
int main ()
{
char access_token[128];
double faceMatch = 0;
post_access_token(access_token);
post_faceMatch(&faceMatch,access_token);
if(faceMatch > 80){
printf("yes\n");
}else{
printf("no\n");
}
return 0;
}
编译
gcc baidudemo.c -lcurl -lcjson -o bdface
主函数编写
用继电器来连接电磁锁,继电器引脚初始化为输出(OUTPUT),电磁锁吸合,继电器引脚初始化为输出(INPUT),电磁锁松开。 main.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<wiringPi.h>
void initWiringPi()
{
int ret = wiringPiSetup();
if(ret == -1){
printf("wiringPi初始化失败\n");
perror("init");
}
}
int main(int argc, char const *argv[])
{
FILE *fp;
int n_read;
char buff[1280];
int num = 0;
initWiringPi();
pinMode(24, INPUT);
while(1){
printf("是否进行人脸识别开锁?1是,0否\n");
scanf("%d",&num);
if(num == 1){
printf("准备开启摄像头拍照\n");
system("raspistill -o img2.jpg -q 5");
memset(buff,'\0',1280);
fp = popen("./bdface","r");
n_read = fread(buff,1,1280,fp);
if(n_read < 0){
printf("读取失败\n");
perror("n_read");
}
if(strstr(buff,"yes") != NULL){
pinMode(24,OUTPUT);
printf("好家伙,是自己人,门已经打开,3s后关闭\n");
delay(3000);
pinMode(24,INPUT);
}else{
printf("好家伙,不认识你\n");
printf("buff = %s\n",buff);
}
}
}
return 0;
}
编译
gcc main.c -lwiringPi -o chack
运行
./chack
|