How to realize face recognition with Python+Caffe on Windows

To do this, you first need to build a caffe environment on windows, thank you  https://www.cnblogs.com/MY0213/p/9225310.html for  providing ideas

Operating system windows 8.1 compiler VS2015

The first step is to download the windows version of caffe  https://github.com/BVLC/caffe/tree/windows

The second step is to download the windows installation package of cmake  https://blog.csdn.net/Create_IT_Man/article/details/105739974

The third step is to download the dependency package of caffe (under the corresponding version)

WIN_DEPENDENCIES_URLS = {
    ('v120', '2.7'):("https://github.com/willyd/caffe-builder/releases/download/v1.1.0/libraries_v120_x64_py27_1.1.0.tar.bz2",
                  "ba833d86d19b162a04d68b09b06df5e0dad947d4"),
    ('v140', '2.7'):("https://github.com/willyd/caffe-builder/releases/download/v1.1.0/libraries_v140_x64_py27_1.1.0.tar.bz2",
                  "17eecb095bd3b0774a87a38624a77ce35e497cd2"),
    ('v140', '3.5'):("https://github.com/willyd/caffe-builder/releases/download/v1.1.0/libraries_v140_x64_py35_1.1.0.tar.bz2",
                  "f060403fd1a7448d866d27c0e5b7dced39c0a607"),
}

The preparations are ready, then unzip caffe on the D drive, in D:\caffe-windows\scripts\build_win.cmd, modify it as follows, which means that it is only compiled with cpu, and pycaffe is compiled with python3.5. The path of python3.5 is in D :\miniconda\envs\py35

Then run build_win.cmd with cmd to ensure that cmd can use cmake. When build_win.cmd runs halfway, it will download the dependency package, directly interrupt ctrl+c, and put the downloaded dependency library compressed package in C:\Users\(your name)\.caffe\dependencies\download\libraries_v140_x64_py35_1.1.0 .tar.bz2, there is no .caffe folder before build_win.cmd is run. Effect after running:

Then you need to compile caffe, first unzip the dependent library compression package to caffe-windows\build, as shown in the figure

And will

D:\caffe-windows\build\libraries\bin, D:\caffe-windows\build\libraries\lib, D:\caffe-windows\build\libraries\x64\vc14\bin three paths are added to the environment variable, Then use VS2015 to open D:\caffe-windows\scripts\build\Caffe.sln and directly generate the solution. If there is no error, it does not matter if the warning is issued.

Finally, copy D:\caffe-windows\python\caffe to your python path. As shown in the figure:

Test python+caffe (pip install if skimage is missing)

---------------------------------------------------------------------------------------------------------------------------------------

Face recognition, essentially calculating the similarity of two faces

First prepare the face recognition model of VGGFACE  http://www.robots.ox.ac.uk/~vgg/software/vgg_face/

Prepare three pictures

import caffe
import numpy as np
import cv2 as cv
import sklearn.metrics.pairwise as pw

def get_feature(net, img):

    img = cv.resize(img, (224,224))
    img = cv.cvtColor(img, cv.COLOR_BGR2RGB).astype(np.float32)/255.0

    img = img[:,:,::-1]*255.0
    mean = np.array([129.1863,104.7624,93.5940])
    img = img - mean

    img = img.transpose((2,0,1))
    img = img[None,:]

    output = net.forward_all( data = img )
    output_prob = output['fc7']

    return output_prob.reshape(1, 4096)

caffe.set_mode_cpu()
proto_path = 'VGG_FACE_deploy.prototxt'
model_path = 'VGG_FACE.caffemodel'
net = caffe.Net(proto_path, model_path, caffe.TEST)

img1 = cv.imread('a.jpg')
img2 = cv.imread('b1.jpg')
img3 = cv.imread('b2.jpg')

f1 = get_feature(net, img1)
f2 = get_feature(net, img2)
f3 = get_feature(net, img3)

s1 = pw.cosine_similarity(f1,f2)
s2 = pw.cosine_similarity(f2,f3)

print('Similarity: %s' % s1)
print('Similarity: %s' % s2)

Get output

Guess you like

Origin blog.csdn.net/XLcaoyi/article/details/108805211