1.接线
2.配置语音模块
3.接上串口烧入安装包并且用电脑串口测试
4.板子测试语音模块
5.接入阿里云人脸识别
人脸识别示例代码的位置
导入并配置示例代码
修改“default” face.py
# -*- coding: utf-8 -*-
# 引入依赖包
# pip install alibabacloud_facebody20191230import os
import io
from urllib.request import urlopen
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import SearchFaceAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptionsconfig = Config(# 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。# 如果您用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。# 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),# 访问的域名endpoint='facebody.cn-shanghai.aliyuncs.com',# 访问的域名对应的regionregion_id='cn-shanghai'
)search_face_request = SearchFaceAdvanceRequest()
#场景一:文件在本地
stream0 = open(r'/home/orangepi/facepicture/yyqx1.jpg', 'rb')
search_face_request.image_url_object = stream0#场景二:使用任意可访问的url
#url = 'https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace1.png'
#img = urlopen(url).read()
#search_face_request.image_url_object = io.BytesIO(img)
search_face_request.db_name = 'default'
search_face_request.limit = 5runtime_option = RuntimeOptions()
try:# 初始化Clientclient = Client(config)response = client.search_face_advance(search_face_request, runtime_option)# 获取整体结果print(response.body)
except Exception as error:# 获取整体报错信息print(error)# 获取单个字段print(error.code)# tips: 可通过error.__dict__查看属性名称#关闭流
#stream0.close()
图片比对路径
修改文件名指令“mv”
获取score最大的值在数据库去开锁
将代码封装成函数并且只输出score的最大值 只保留两位小数:
需要的数据 把dict数据问AI用python写出来 face.py
# -*- coding: utf-8 -*-
# 引入依赖包
# pip install alibabacloud_facebody20191230import os
import io
from urllib.request import urlopen
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import SearchFaceAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptionsconfig = Config(# 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。# 如果您用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。# 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),# 访问的域名endpoint='facebody.cn-shanghai.aliyuncs.com',# 访问的域名对应的regionregion_id='cn-shanghai'
)
def alibaba_face():search_face_request = SearchFaceAdvanceRequest()#场景一:文件在本地stream0 = open(r'/home/orangepi/facepicture/yyqx1.jpg', 'rb')search_face_request.image_url_object = stream0#场景二:使用任意可访问的url#url = 'https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace1.png'#img = urlopen(url).read()#search_face_request.image_url_object = io.BytesIO(img)search_face_request.db_name = 'default'search_face_request.limit = 5runtime_option = RuntimeOptions()try:# 初始化Clientclient = Client(config)response = client.search_face_advance(search_face_request, runtime_option)# 获取整体结果#print(response.body)match_list = response.body.to_map()['Data']['MatchList']scores = [item['Score'] for item in match_list[0]['FaceItems']]highest_score = max(scores)#print(highest_score)value = round(highest_score, 2)print(value)except Exception as error:# 获取整体报错信息print(error)# 获取单个字段print(error.code)# tips: 可通过error.__dict__查看属性名称#关闭流stream0.close()
if __name__ == "__main__":alibaba_face()
运行结果:
实现c语言调用python代码 face.c
#include <Python.h>void face_init(void)
{Py_Initialize();// 将当前路径添加到sys.path中PyObject *sys = PyImport_ImportModule("sys");PyObject *path = PyObject_GetAttrString(sys, "path");PyList_Append(path, PyUnicode_FromString("."));}void face_final(void)
{Py_Finalize();
}double face_category(void)
{// 导入para模块PyObject *pModule = PyImport_ImportModule("face");if (!pModule){PyErr_Print();printf("Error: failed to load face.py\n");goto FAILED_MODULE;}//获取say_funny函数对象PyObject *pFunc = PyObject_GetAttrString(pModule, "alibaba_face");if (!pFunc){PyErr_Print();printf("Error: failed to load alibaba_face\n");goto FAILED_FUNC;}//调用say_funny函数并获取返回值PyObject *pValue = PyObject_CallObject(pFunc, NULL);if (!pValue){PyErr_Print();printf("Error: function call failed\n");goto FAILED_FUNC;}//将返回值转换为C类型double result = 0.0;if (!PyArg_Parse(pValue, "d", &result)){PyErr_Print();printf("Error: parse failed\n");goto FAILED_RESULT;}printf("result = %0.2lf\n", result);FAILED_RESULT:Py_DECREF(pValue);
FAILED_VALUE:Py_DECREF(pFunc);
FAILED_FUNC:Py_DECREF(pModule);
FAILED_MODULE:return result;
}int main(int argc,char *argv[])
{double face_result = 0.0;face_init();face_result = face_category();face_final();}
gcc -o face face.c -I /usr/include/python3.10 -L /usr/lib/python3.10/ -lpython3.10 -lwiringPi
./face
结果: