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

今天基于卷积神经网络给一个简单的嵌入式人脸识别系统
思路如下:登记时,我给了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 path
        pattern: record your info, or detect who you are
        label: record label
        pre_dict: {label:name}
        operation: your own system would like to be control
        retrain: 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'):
                break
        image=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'):
                break
        image=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:
                continue
            else:
                label=int(eval(label))
        else:
            label=None
        sure=bool(eval(input('are you sure? 0 or 1         ')))
        if sure:
            break
    dict_ = {1: 'my friend'}
    simeple_detron(pattern=pattern,label=label,pre_dict=dict_)



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值