栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

k210人脸识别门禁(k210人脸识别原理)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

k210人脸识别门禁(k210人脸识别原理)

import sensor
import image
import lcd
import os
import ubinascii
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
A_1=A_2=0
B_1=B_2=B_3=B_4=0
#==================图框颜色==================
color_R = (255, 0, 0)
color_G = (0, 255, 0)
color_B = (0, 0, 255)
#==================*定义变量===============#
e_1=[0,0,0,0,0,0,0,0,0,0]
a_1=a_2=0#记录众数滤波的次数
#==================I/O===================#
from Maix import FPIOA
from Maix import GPIO
fpioa = FPIOA()
fpioa.set_function(0,fpioa.GPIOHS0)
led1 = GPIO(GPIO.GPIOHS0,GPIO.OUT)
fpioa.set_function(1,fpioa.GPIOHS1)
led2 = GPIO(GPIO.GPIOHS1,GPIO.OUT)
#=================内存卡===================#
feature_file_exists = 0
for v in os.ilistdir('/sd'):#to check key directorys or files in sd card.sd card should be formated to fat32
    if v[0] == 'features.txt' and v[1] == 0x8000:#0x8000 is file
        feature_file_exists = 1
#================内存卡读写操作================#
record_ftr=[] #空列表 用于存储当前196维特征
record_ftrs=[] #空列表 用于存储按键记录下人脸特征, 可以将特征以txt等文件形式保存到sd卡后,读取到此列表,即可实现人脸断电存储。
names = ['MR.1', 'MR.2', 'MR.3', 'Mr.4', 'Mr.5', 'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9' , 'Mr.10'] # 人名标签,与上面列表特征值一一对应。
reco = ''
record = []
def save_feature(feat):
    with open('/sd/features.txt','a') as f:
        record =ubinascii.b2a_base64(feat)
        f.write(record)

st = ''
if(feature_file_exists):
    print("start")
    with open('/sd/features.txt','rb') as f:
        s = f.readlines()
        print(len(s))
        #print(s)
        for line in s:
            #print(ubinascii.a2b_base64(line))
            record_ftrs.append(bytearray(ubinascii.a2b_base64(line)))
#=====================众数滤波==================#
def lvbo(c_1):
  global e_1
  global a_1
  global a_2
  if a_1<10:
    e_1[a_1]=c_1
    a_1=a_1+1
    print("e_1",e_1)
  else:
    a_2=max(e_1, default='列表为空', key=lambda v: e_1.count(v))
    if a_2==0:
        print("啥也没有")
    if a_2==1:
        print("已佩戴口罩")
        led1.value(0)
    if a_2==2:
        print("未佩戴口罩")
        led1.value(1)
    if a_2==3:
        print("人脸识别成功")
        led2.value(0)
    if a_2==4:
        print("人脸识别失败")
        led2.value(1)
    #===为下次做准备===#
    a_1=1
#==================口罩子程序,显示在img==================
def drawConfidenceText(image, rol, classid, value):
    global A_1
    global A_2
    text = ""
    _confidence = int(value * 100)

    if classid == 1:
        text = 'mask: ' + str(_confidence) + '%'
        lvbo(1)
    else:
        text = 'no_mask: ' + str(_confidence) + '%'
        lvbo(2)

    image.draw_string(rol[0], rol[1], text, color=color_R, scale=2.5)

class_IDs = ['no_mask', 'mask']
#==================加载模型分别是196特征值和mask(口罩和人脸)==================**
task_fe = kpu.load(0x500000)
task = kpu.load(0x300000)

clock = time.clock()
#==================定义boot按键==================**
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(dual_buff=True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(1)
sensor.set_vflip(0)
sensor.run(1)
#==================加载模型分别是196特征值和mask(口罩和人脸)==================**
anchor = (0.1606, 0.3562, 0.4712, 0.9568, 0.9877, 1.9108, 1.8761, 3.5310, 3.4423, 5.6823)  # 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, 0.5, 0.3, 5, anchor)
b = kpu.init_yolo2(task_fe, 0.5, 0.3, 5, anchor)
#==================窗口大小==================**
img_lcd = image.Image()
img_face = image.Image(size=(128, 128))
a = img_face.pix_to_ai()

#==================人脸相似度==================**
ACCURACY = 75

while (1):
    img = sensor.snapshot()
    clock.tick()
    code = kpu.run_yolo2(task, img)
    if code:
        totalRes = len(code)

        for item in code:
            ##==================图片转换==================**
            a = img.draw_rectangle(item.rect())
            face_cut = img.cut(item.x(), item.y(), item.w(), item.h())
            face_cut_128 = face_cut.resize(128, 128)
            a = face_cut_128.pix_to_ai()
            #==================口罩==================**
            confidence = float(item.value())
            itemROL = item.rect()
            classID = int(item.classid())
            if confidence < 0.52:
                _ = img.draw_rectangle(itemROL, color=color_B, tickness=5)
                continue

            if classID == 1 and confidence > 0.65:
                _ = img.draw_rectangle(itemROL, color_G, tickness=5)
                if totalRes == 1:
                    drawConfidenceText(img, (0, 0), 1, confidence)
            else:
                _ = img.draw_rectangle(itemROL, color=color_R, tickness=5)
                if totalRes == 1:
                    drawConfidenceText(img, (0, 0), 0, confidence)
            #==================人脸==================**
            fmap = kpu.forward(task_fe, face_cut_128)
            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
            del (face_cut_128)
            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(item.x(), item.y(), ("%s :%2.1f" % (
                    names[index], max_score)), color=(0, 255, 0), scale=2)
                lvbo(3)
            else:
                a = img.draw_string(item.x(), item.y(), ("X :%2.1f" % (
                max_score)), color=(255, 0, 0), scale=2)
                lvbo(4)
            #=====================**按键注册==================*
            if start_processing:
                record_ftrs = []
                record_ftr = feature
                record_ftrs.append(record_ftr)
                save_feature(record_ftr) #存到SD卡
                print(feature)
                start_processing = False
            break
    lvbo(0)
    fps = clock.fps()
    #print("%2.1f fps" % fps)
    img.draw_string(0, 200, "%2.1f fps" % fps, scale=2, color=(255, 0, 0))
    a = lcd.display(img)
    gc.collect()
    b= img.draw_rectangle(80,22,159,195)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号