特征库制作完整代码

import os
from a_Net_module import *
import torch
from PIL import Image
from detect_img import Detector
import cv2
import numpy as np


class FaceDatabase():
    def __init__(self, personName_path, params_path,database_path):

        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        net = FaceNet(500).to(device)
        net.load_state_dict(torch.load(params_path))
        net.eval()

        self.database = {}
        for personName in os.listdir(personName_path):
            person_feats = []
            for personImg_name in os.listdir(os.path.join(personName_path, personName)):
                img = Image.open(os.path.join(personName_path, personName, personImg_name))
                person_feat = net.encode((torch.unsqueeze(transf(img), dim=0)).to(device))
                person_feats.append(person_feat)
            person_feats = torch.cat(person_feats, dim=0)
            self.database[personName] = person_feats

        with open(database_path,'w',encoding='utf8') as f:
            torch.save(self.database,database_path)
        f.close()



if __name__ == '__main__':
    personName_path = r'./face_database'
    params_path = r'./params/params_1.pth'
    database_path=r'./params/face_database.txt'
    database = FaceDatabase(personName_path, params_path,database_path)
    data=torch.load(database_path)
    print(data['何钢'].shape)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值