SphereFace python抽取人脸特征

来源:互联网 发布:科创论坛 知乎 编辑:程序博客网 时间:2024/06/04 19:23
import numpy,cv2import os,caffeimport sklearn.metrics.pairwise as pwimport time,skimageimport matplotlib.pyplot as pltimport numpy as npclass LightCNN():    def __init__(self, end_cnn="eltwise_fc1", model_version="LightenedCNN_B"):        self.net = caffe.Net("sphereface_deploy.prototxt","sphereface_model.caffemodel",  caffe.TEST) #load net        self.end_cnn="fc5"        self.model_version = model_version    def getFeat(self, imgPath):        image = cv2.imread("043.jpg")  #opencv is BRG        rgbImg = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)        #cv2.imshow("rgbImg",rgbImg)        #cv2.waitKey(0)        rgbImg = cv2.resize(rgbImg,(96,112));        rgbImg = (rgbImg - 127.5)/128;        rgbImg = rgbImg.transpose((2,0,1))  ## equal to permute        rgbImg = rgbImg[None,:] # add singleton dimension        out = self.net.forward([self.end_cnn], data=rgbImg)        return out['fc5']        #labels=np.loadtxt('names.txt',str,delimiter='\n') # get the name        #print labels[out['prob'].argmax()]cnn = LightCNN()#for r in [i/10.0 for i in range(10)]:  #print rimgPath = "ak.png"t1 = time.time()feat = cnn.getFeat(imgPath)for f in feat:   for i in f:      print i'''feat2 = cnn.getFeat("E://19.jpg")#print (feat)t2 = time.time()#print len(feat),t2 - t1predicts=pw.cosine_similarity(feat, feat2)#t2 = time.time()print predicts,t2 - t1'''
原创粉丝点击