日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > pytorch >内容正文

pytorch

V831 人脸识别

發(fā)布時(shí)間:2024/3/26 pytorch 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 V831 人脸识别 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

V831 人臉識(shí)別

  • 人臉識(shí)別功能
    • 參數(shù)說(shuō)明
    • 實(shí)例代碼

之前有個(gè)find_face,那個(gè)只是人臉檢測(cè)。今天測(cè)試的程序是人臉識(shí)別。首先看效果

選個(gè)人臉,按右鍵記錄一下

記錄成功,他現(xiàn)在就是“標(biāo)本”然后以他為“標(biāo)本”去識(shí)別其他人看看
懂我意思把,也可以左鍵刪除人臉特征,找另一個(gè)人右鍵錄入特征


人臉識(shí)別功能

人臉識(shí)別例程,右鍵錄入人臉特征,左鍵刪除人臉特征,屏幕顯示識(shí)別到的人臉標(biāo)簽和分?jǐn)?shù),關(guān)機(jī)數(shù)據(jù)不保存。

文件地址 :https://github.com/sipeed/MaixPy3_scripts/blob/master/scripts/nn_face_recognition.py
注意自己的V831有沒(méi)有model_int8.param和model_int8.bin模型。

參數(shù)說(shuō)明

該腳本使用人臉識(shí)別模塊 maix.nn.app.face。使用方法請(qǐng)參考:模塊 maix.nn.app.face

實(shí)例代碼

#!/usr/bin/python3 # MaixPy3人臉識(shí)別示例腳本 # 功能說(shuō)明:通過(guò)V831上的按鍵控制學(xué)習(xí)人臉特征,并進(jìn)行特征匹配 # 時(shí)間:2021年9月17日 # 作者:Neutree dianjixz from maix import nn from PIL import Image, ImageFont, ImageDraw from maix import camera, display import time from maix.nn.app.face import FaceRecognize from evdev import InputDevice from select import select import threading from evdev import InputDevice from select import selectclass funation:score_threshold = 70 #識(shí)別分?jǐn)?shù)閾值input_size = (224, 224, 3) #輸入圖片尺寸input_size_fe = (128, 128, 3) #輸入人臉數(shù)據(jù)feature_len = 256 #人臉數(shù)據(jù)寬度steps = [8, 16, 32] #channel_num = 0 #通道數(shù)量users = [] #初始化用戶列表names = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] #人臉標(biāo)簽定義model = { "param": "/home/res/model_int8.param","bin": "/home/res/model_int8.bin"}model_fe = {"param": "/home/res/fe_res18_117.param","bin": "/home/res/fe_res18_117.bin"}fun_status = 0 #程序運(yùn)行階段def __init__(self,device=None):for i in range(len(self.steps)):self.channel_num += self.input_size[1] / self.steps[i] * (self.input_size[0] / self.steps[i]) * 2self.channel_num = int(self.channel_num) #統(tǒng)計(jì)通道數(shù)量self.options = { #準(zhǔn)備人臉輸出參數(shù)"model_type": "awnn","inputs": {"input0": self.input_size},"outputs": {"output0": (1, 4, self.channel_num) ,"431": (1, 2, self.channel_num) ,"output2": (1, 10, self.channel_num) },"mean": [127.5, 127.5, 127.5],"norm": [0.0078125, 0.0078125, 0.0078125],}self.options_fe = { #準(zhǔn)備特征提取參數(shù)"model_type": "awnn","inputs": {"inputs_blob": self.input_size_fe},"outputs": {"FC_blob": (1, 1, self.feature_len)},"mean": [127.5, 127.5, 127.5],"norm": [0.0078125, 0.0078125, 0.0078125],}self.keys = InputDevice('/dev/input/event0')threading.Thread(target=self.load_mode).start() #開(kāi)啟加載模型線程self.fun = [self.wait_run,self.run] #裝載階段程序self.event = self.fun[self.fun_status] #定義統(tǒng)一調(diào)用接口,相當(dāng)于函數(shù)指針,此處將會(huì)裝載self.wait_run函數(shù)self.font = ImageFont.truetype("./res/baars.ttf",32, encoding="unic")def __del__(self):del self.m_fe #刪除特征識(shí)別模型del self.m #刪除人臉檢測(cè)模型del self.face_recognizer #刪除人臉識(shí)別類print("-- del face model success!")def get_key(self): #按鍵檢測(cè)函數(shù)r,w,x = select([self.keys], [], [],0)if r:for event in self.keys.read(): if event.value == 1 and event.code == 0x02: # 右鍵return 1elif event.value == 1 and event.code == 0x03: # 左鍵return 2elif event.value == 2 and event.code == 0x03: # 左鍵連按return 3return 0def load_mode(self): #模型加載函數(shù),由模型加載線程啟動(dòng)threshold = 0.5 #人臉閾值nms = 0.3 max_face_num = 1 #輸出的畫(huà)面中的人臉的最大個(gè)數(shù)print("-- load model:", self.model)self.m = nn.load(self.model, opt=self.options)print("-- load ok")print("-- load model:", self.model_fe)self.m_fe = nn.load(self.model_fe, opt=self.options_fe)print("-- load ok")self.face_recognizer = FaceRecognize(self.m, self.m_fe, self.feature_len, self.input_size, threshold, nms, max_face_num)self.fun_status += 1self.event = self.fun[self.fun_status] #統(tǒng)一調(diào)用接口切換至self.run函數(shù)def map_face(self,box,points): #將224*224空間的位置轉(zhuǎn)換到240*240空間內(nèi)def tran(x):return int(x/224*240)box = list(map(tran, box))def tran_p(p):return list(map(tran, p))points = list(map(tran_p, points))return box,pointsdef darw_info(self,draw, box, points, disp_str, bg_color=(255, 0, 0, 255), font_color=(255, 255, 255, 255)): #畫(huà)框函數(shù)box,points = self.map_face(box,points)font_w, font_h = self.font.getsize(disp_str)for p in points:draw.rectangle((p[0] - 1, p[1] -1 , p[0] + 1, p[1] + 1), fill=bg_color)draw.rectangle((box[0], box[1], box[0] + box[2], box[1] + box[3]), fill=None, outline=bg_color, width=2)draw.rectangle((box[0], box[1] - font_h, box[0] + font_w, box[1]), fill=bg_color)draw.text((box[0], box[1] - font_h), disp_str, fill=font_color, font=self.font)def recognize(self, feature): #進(jìn)行人臉匹配def _compare(user): #定義映射函數(shù)return self.face_recognizer.compare(user, feature) #推測(cè)匹配分?jǐn)?shù) score相關(guān)分?jǐn)?shù)face_score_l = list(map(_compare,self.users)) #映射特征數(shù)據(jù)在記錄中的比對(duì)分?jǐn)?shù)return max(enumerate(face_score_l), key=lambda x: x[-1]) #提取出人臉?lè)謹(jǐn)?shù)最大值和最大值所在的位置def wait_run(self): #等待模型加載階段tmp = camera.read(video_num = 1)display.show()def run(self): #模型加載完畢,準(zhǔn)備運(yùn)行階段img = camera.read(video_num = 1) #獲取224*224*3的圖像數(shù)據(jù)if not img:time.sleep(0.02)returnfaces = self.face_recognizer.get_faces(img,False) #提取人臉特征信息if faces:# for prob, box, landmarks, feature, std_img in faces:for prob, box, landmarks, feature in faces:# [ prob, [x,y,w,h], [[x,y], [x,y], [x,y], [x,y], [x,y]], feature ]key_val = self.get_key()if key_val == 1: # 右鍵添加人臉記錄if len(self.users) < len(self.names):print("add user:", len(self.users))self.users.append(feature)else:print("user full")elif key_val == 2: # 左鍵刪除人臉記錄if len(self.users) > 0:print("remove user:", self.names[len(self.users) - 1])self.users.pop()else:print("user empty")draw = display.get_draw() #得到一張畫(huà)布if len(self.users): #判斷是否記錄人臉maxIndex = self.recognize(feature)if maxIndex[1] > self.score_threshold: #判斷人臉識(shí)別閾值,當(dāng)分?jǐn)?shù)大于閾值時(shí)認(rèn)為是同一張臉,當(dāng)分?jǐn)?shù)小于閾值時(shí)認(rèn)為是相似臉self.darw_info(draw, box, landmarks, "{}:{:.2f}".format(self.names[maxIndex[0]], maxIndex[1]), font_color=(0, 0, 255, 255), bg_color=(0, 255, 0, 255))print("user: {}, score: {:.2f}".format(self.names[maxIndex[0]], maxIndex[1]))else:self.darw_info(draw, box, landmarks, "{}:{:.2f}".format(self.names[maxIndex[0]], maxIndex[1]), font_color=(255, 255, 255, 255), bg_color=(255, 0, 0, 255))print("maybe user: {}, score: {:.2f}".format(self.names[maxIndex[0]], maxIndex[1]))else: #沒(méi)有記錄臉self.darw_info(draw, box, landmarks, "no face", font_color=(255, 255, 255, 255), bg_color=(255, 0, 0, 255))display.show()if __name__ == "__main__":import signaldef handle_signal_z(signum,frame):print("APP OVER")exit(0)signal.signal(signal.SIGINT,handle_signal_z)camera.config(size=(224,224))start = funation()while True:start.event()

總結(jié)

以上是生活随笔為你收集整理的V831 人脸识别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。