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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

简单的嵌入式人脸识别系统

發布時間:2023/12/9 windows 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的嵌入式人脸识别系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天基于卷積神經網絡給一個簡單的嵌入式人臉識別系統
思路如下:登記時,我給了10個容量,可以給需要登記的人一個標簽,然后pattern選擇‘train’即可實現人臉登記,按’q‘推出截屏。
使用時,選擇’val‘截屏后判斷你是哪一個人,opeartion可以是你自定義的函數,比如你想實現一個人臉識別開所程序,那么opearion只要是傳入和pre之間有聯系的值即可。

import cv2 from torchvision.models import resnet import torch from torch import nn import os import numpy as np import torchvision def video_capture(pattern='array'):capture = cv2.VideoCapture(0)_, frame = capture.read()if pattern=='array':return np.array(frame)else:return frame def simeple_detron(dir='detectron_system',pattern='train',label=1,pre_dict=None,opeartion=None,retrain=False):''':describe:this function is used to control function in 'operation'.basic principle is:label=CONV(your image)action=operation(label):param:dir: weigh saved pathpattern: record your info, or detect who you arelabel: record labelpre_dict: {label:name}operation: your own system would like to be controlretrain: train from head with out loading the weights before'''assert pattern in ['train','val'],print('only support train or val')model = resnet.resnet34(pretrained=True)if not os.path.exists(dir):os.mkdir(dir)if pattern=='train':del (model.fc)model.fc=nn.Linear(in_features=512,out_features=10)if not retrain:model.load_state_dict(torch.load(dir + '/' + 'person.pth'))while(1):image=video_capture(pattern='image')cv2.imshow('photo_shop',image)if cv2.waitKey(30)==ord('q'):breakimage=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Resize((224,224))])(image)optim=torch.optim.Adam(lr=0.001,params=model.parameters())for i in range(100):pre=model(image.unsqueeze(0).repeat(2,1,1,1))loss=nn.CrossEntropyLoss()(pre,torch.Tensor([label,label]).view(2,1).squeeze(-1).long())optim.zero_grad()loss.backward()optim.step()torch.save(model.state_dict(), dir + '/' + 'person.pth')if pattern=='val':del(model.fc)model.fc = nn.Linear(in_features=512, out_features=10)model.load_state_dict(torch.load(dir + '/' + 'person.pth'))model=model.eval()while (1):image = video_capture(pattern='image')cv2.imshow('photo_shop', image)if cv2.waitKey(30) == ord('q'):breakimage=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Resize((224,224))])(image)pre=model(image.unsqueeze(0))if pre_dict:pre=pre_dict[pre.argmax(dim=-1).cpu().detach().item()]print(f'this person is {pre}')if opeartion:opeartion(pre) if __name__=='__main__':print('*************This is an simple detection system**************')print('*************you can assign a function to this system so that make this system become a real cotrol system')print('*************for example: simeple_detron(opeartion= your_function)')print('lets start!')while(1):pattern=input('train is to map your shape to your name, val is to check who you are: ')if pattern=='train':label = input('0~10: used for registration ')if eval(label)<0 or eval(label)>10:continueelse:label=int(eval(label))else:label=Nonesure=bool(eval(input('are you sure? 0 or 1 ')))if sure:breakdict_ = {1: 'my friend'}simeple_detron(pattern=pattern,label=label,pre_dict=dict_)

總結

以上是生活随笔為你收集整理的简单的嵌入式人脸识别系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。