forked from Jinnrry/FaceRecognition-RestApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.py
59 lines (45 loc) · 1.32 KB
/
Tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
import numpy as np
import pickle
import skimage
import redis
caffe_root = '/home/jiangwei/bin/caffe-master/'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
import sklearn.metrics.pairwise as pw
#GPU加速
caffe.set_mode_gpu()
#加载caffe模型
global net
net=caffe.Classifier('vgg/deploy.prototxt','vgg/vgg_face.caffemodel',caffe.TEST)
#提取特征数组
def get_feature(path1):
global net
#加载图片
X=read_image(path1)
test_num=np.shape(X)[0]
#X 作为 模型的输入
out = net.forward_all(blobs=['pool5'],data = X)
# print out.keys()
feature1 = np.float64(out["pool5"])
feature1=np.reshape(feature1,(test_num,25088))
#加载注册图片
return feature1
def read_image(im1):
averageImg = [129.1863,104.7624,93.5940]
X=np.empty((1,3,224,224))
# word=filelist.split('\n')
# filename=word[0]
# im1=skimage.io.imread(filename,as_grey=False)
#归一化
image =skimage.transform.resize(im1,(224, 224))*255
X[0,0,:,:]=image[:,:,0]-averageImg[0]
X[0,1,:,:]=image[:,:,1]-averageImg[1]
X[0,2,:,:]=image[:,:,2]-averageImg[2]
return X
#将数据存入redis数据库
def saveData(name,img):
info = get_feature(img)
r = redis.Redis(host='localhost', port=6379, db=0)
r.lpush(name,pickle.dumps(info))