**
一、 K210进行人脸识别
**
进入官网的开源模型平台(首次登陆需要注册),获取人脸识别源码
maixhub
选择这个人脸识别模型
下载得到三个 .smodel 模型文件
并将三个文件拷贝到 TF 卡
以下为k210的main.py文件
import sensor
import image
import lcd
import KPU as kpu
import time
from Maix import FPIOA, GPIO
import gc
from fpioa_manager import fm
from board import board_info
import utime
from machine import UART,Timer
from fpioa_manager import fm
#映射串口引脚
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
#初始化串口
uart = UART(UART.UART1, 115200, read_buf_len=4096)
uart.write('Hello')
# task_fd = kpu.load(0x300000)
# task_ld = kpu.load(0x400000)
# task_fe = kpu.load(0x500000)
task_fd = kpu.load("/sd/FaceDetection.smodel")
task_ld = kpu.load("/sd/FaceLandmarkDetection.smodel")
task_fe = kpu.load("/sd/FeatureExtraction.smodel")
clock = time.clock()
fm.register(board_info.BOOT_KEY, fm.fpioa.GPIOHS0)
key_gpio = GPIO(GPIO.GPIOHS0, GPIO.IN)
start_processing = False
BOUNCE_PROTECTION = 50
def set_key_state(*_):
global start_processing
start_processing = True
utime.sleep_ms(BOUNCE_PROTECTION)
key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT)
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(1)
sensor.set_vflip(1)
sensor.run(1)
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437,
6.92275, 6.718375, 9.01025) # anchor for face detect
dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),
(81, 105)] # standard face key point position
a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
img_lcd = image.Image()
img_face = image.Image(size=(128, 128))
a = img_face.pix_to_ai()
record_ftr = []
record_ftrs = []
names = ['Mr.1', 'Mr.2', 'Mr.3', 'Mr.4', 'Mr.5',
'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9', 'Mr.10']
ACCURACY = 85
while (1):
img = sensor.snapshot()
clock.tick()
code = kpu.run_yolo2(task_fd, img)
if code:
for i in code:
# Cut face and resize to 128x128
a = img.draw_rectangle(i.rect())
face_cut = img.cut(i.x(), i.y(), i.w(), i.h())
face_cut_128 = face_cut.resize(128, 128)
a = face_cut_128.pix_to_ai()
# a = img.draw_image(face_cut_128, (0,0))
# Landmark for face 5 points
fmap = kpu.forward(task_ld, face_cut_128)
plist = fmap[:]
le = (i.x() + int(plist[0] * i.w() - 10), i.y() + int(plist[1] * i.h()))
re = (i.x() + int(plist[2] * i.w()), i.y() + int(plist[3] * i.h()))
nose = (i.x() + int(plist[4] * i.w()), i.y() + int(plist[5] * i.h()))
lm = (i.x() + int(plist[6] * i.w()), i.y() + int(plist[7] * i.h()))
rm = (i.x() + int(plist[8] * i.w()), i.y() + int(plist[9] * i.h()))
a = img.draw_circle(le[0], le[1], 4)
a = img.draw_circle(re[0], re[1], 4)
a = img.draw_circle(nose[0], nose[1], 4)
a = img.draw_circle(lm[0], lm[1], 4)
a = img.draw_circle(rm[0], rm[1], 4)
# align face to standard position
src_point = [le, re, nose, lm, rm]
T = image.get_affine_transform(src_point, dst_point)
a = image.warp_affine_ai(img, img_face, T)
a = img_face.ai_to_pix()
# a = img.draw_image(img_face, (128,0))
del (face_cut_128)
# calculate face feature vector
fmap = kpu.forward(task_fe, img_face)
feature = kpu.face_encode(fmap[:])
reg_flag = False
scores = []
for j in range(len(record_ftrs)):
score = kpu.face_compare(record_ftrs[j], feature)
scores.append(score)
max_score = 0
index = 0
for k in range(len(scores)):
if max_score < scores[k]:
max_score = scores[k]
index = k
if max_score > ACCURACY:
a = img.draw_string(i.x(), i.y(), ("%s :%2.1f" % (
names[index], max_score)), color=(0, 255, 0), scale=2)
uart.write(names[index])######串口
uart.write("\n")
else:
a = img.draw_string(i.x(), i.y(), ("X :%2.1f" % (
max_score)), color=(255, 0, 0), scale=2)
if start_processing:
record_ftr = feature
record_ftrs.append(record_ftr)
start_processing = False
break
fps = clock.fps()
print("%2.1f fps" % fps)
a = lcd.display(img)
gc.collect()
# kpu.memtest()
# a = kpu.deinit(task_fe)
# a = kpu.deinit(task_ld)
# a = kpu.deinit(task_fd)
此处我在原有的代码上加了串口,在人脸检测成功后通过串口发送到ESP8266处
二、esp8266连接wife通过MQTT上传信息和订阅信息
此处我们选择用Arduino来开发它
配置如下
下边是参考代码
#include <ESP8266WiFi.h>//默认,加载WIFI头文件
#include "PubSubClient.h"//默认,加载MQTT库文件
const char* ssid = "blf";//修改,你的路由去WIFI名字
const char* password = "12345678";//你的WIFI密码
const char* mqtt_server = "bemfa.com";//默认,MQTT服务器
const int mqtt_server_port = 9501;//默认,MQTT服务器
#define ID_MQTT "c39f00e2be6248f1a807173c7ec5bf9b" //修改,你的Client ID
const char* topic = "face"; //主题名字,可在巴法云控制台自行创建,名称随意
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//灯光函数及引脚定义
void turnOnLed();
void turnOffLed();
const int B_led = D4;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) { //接收信息
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String Mqtt_Buff = "";
for (int i = 0; i < length; i++) {
Mqtt_Buff += (char)payload[i];
}
Serial.print(Mqtt_Buff);
Serial.println();
// Switch on the LED if an 1 was received as first character
while(Mqtt_Buff == "OPEN") {//如果接收字符on,亮灯
turnOnLed();//开灯函数
}
Mqtt_Buff = "";
}
void sendMessage(String message) {
if (client.connected()) {
client.publish(topic, message.c_str());
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(ID_MQTT)) {
Serial.println("connected");
client.subscribe(topic);//修改,修改为你的主题
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
当K210识别人脸成功后,将姓名通过串口发送到ESP8266,当esp8266接收到信息后,通过MQTT发布信息,
ESP8266也订阅了MQTT,当MQTT发布"OPEN"时,ESP8266点亮小灯,模拟开门
可以自行接一个继电器
下面是一个免费的服务器,可以在上面自行创建MQTT云
巴法云
在上面可以自己创建主题
三、后台管理系统+数据库
将MQTT发布的信息与数据库进行匹配,
若教室审核通过,就在MQTT主题发布"OPEN"
未通过就不发布
以下为部分代码
import pymysql
# djangoz3gwO"
import paho.mqtt.client as mqtt
from multiprocessing import Process
# import camera_person_num
MQTTHOST = "bemfa.com" # 免费测试服务器
MQTTPORT = 9501
mqttClient = mqtt.Client(client_id="c39f00e2be6248f1a807173c7ec5bf9b")
TOPIC = 'face' ###########主题名称
# encoding: utf-8
global my_name
my_name = ""
# 连接MQTT服务器
def on_mqtt_connect():
mqttClient.connect(MQTTHOST, MQTTPORT, 60) # keepalive = 60
mqttClient.loop_start()
# 消息处理函数
def on_message_come(client, userdata, msg):
print(msg.topic + ":" + str(msg.payload.decode("utf-8")))
my_name=str(msg.payload.decode("utf-8"))
db = pymysql.connect(host="localhost", user="root", password="123456", database="djangoz3gw0", port=3306,
charset="utf8")
cussor = db.cursor()
sql = 'select * from jiaoshishenqing'
cussor.execute(sql)
rest = cussor.fetchall()
for i in rest :
# print(data[-2])
if i[-2] == '是':
# # print(data[3])
if i[3] == my_name:
on_publish(TOPIC, "OPEN", 1)
db.close()
# 消息处理开启多进程
# subscribe 消息订阅
def on_subscribe():
mqttClient.subscribe(TOPIC, 1) # 主题为"test"
mqttClient.on_message = on_message_come # 消息到来处理函数
# publish 消息发布
def on_publish(topic, msg, qos):
mqttClient.publish(topic, msg, qos);
# 多进程中发布消息需要重新初始化mqttClient
def talk(topic, msg):
# cameraPsersonNum = camera_person_num.CameraPsersonNum(msg)
# t_max, t_mean = cameraPsersonNum.personNum()
t_max, t_mean = 2, 3
mqttClient = mqtt.Client()
mqttClient.connect(MQTTHOST, MQTTPORT, 60)
mqttClient.loop_start()
mqttClient.publish(topic, '{"max":' + str(t_max) + ',"mean:"' + str(t_mean) + '}', 1)
def main():
on_mqtt_connect()
on_subscribe()
while 1:
pass
"""死循环"""
if __name__ == '__main__':
main()
由于后台管理系统代码过多就不一一展示,此处就展示效果