IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 简单的嵌入式人脸识别系统 -> 正文阅读

[人工智能]简单的嵌入式人脸识别系统

今天基于卷积神经网络给一个简单的嵌入式人脸识别系统
思路如下:登记时,我给了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_)



  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-07-09 17:32:00  更:2021-07-09 17:32:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年3日历 -2024/3/29 16:09:39-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码