-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_face.py
31 lines (22 loc) · 986 Bytes
/
is_face.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
import os
#to not display shell outpu from caffe
os.environ['GLOG_minloglevel'] = '2'
import caffe
import numpy as np
from PIL import Image
caffe.set_mode_cpu()
def is_face(image, caffemodel):
#we use our CNN. the first argument is the architecture of our CNN, the second one are the weights
net = caffe.Net('/datas/deploy.prototxt', '/datas/{}'.format(caffemodel), caffe.TEST)
#we create a transformer that resize the shape of our datas
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # (36,36,1) -> (1,36,36), seen as (1,1,36,36)
#we load our image, and we preprocess it
#im = caffe.io.load_image(image_path, color=False)
image_transformed = transformer.preprocess('data', image)
#we give it as the data to our CNN
net.blobs['data'].data[...] = image_transformed
#we make the CNN work
out = net.forward()
#we get the class
return out['prob'][0][1]