-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_spoofing.py
51 lines (35 loc) · 1.13 KB
/
face_spoofing.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
import numpy as np
import cv2
import joblib
import dlib
clf = joblib.load('models/face_spoofing.pkl')
sample_number = 1
count = 0
measures = np.zeros(sample_number, dtype=np.float)
def calc_hist(img):
histogram = [0] * 3
for j in range(3):
histr = cv2.calcHist([img], [j], None, [256], [0, 256])
histr *= 255.0 / histr.max()
histogram[j] = histr
return np.array(histogram)
def face_spoof(img, face):
x = face[0]*4
y = face[1]*4
x1 = face[2]*4
y1 = face[3]*4
measures[count%sample_number]=0
roi = img[y:y1, x:x1]
point = (0,0)
img_ycrcb = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
img_luv = cv2.cvtColor(roi, cv2.COLOR_BGR2LUV)
ycrcb_hist = calc_hist(img_ycrcb)
luv_hist = calc_hist(img_luv)
feature_vector = np.append(ycrcb_hist.ravel(), luv_hist.ravel())
feature_vector = feature_vector.reshape(1, len(feature_vector))
prediction = clf.predict_proba(feature_vector)
prob = prediction[0][1]
measures[count % sample_number] = prob
#cv2.rectangle(img, (x, y), (x1, y1), (255, 0, 0), 2)
point = (x, y-5)
return measures