-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict_test_66_7_percent.py
115 lines (95 loc) · 2.78 KB
/
predict_test_66_7_percent.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import glob
import numpy as np
import os
import pickle
import sys
#sys.path.append('/data4/xgboost/python-package')
from xgboost import XGBClassifier
import xgboost as xgb
import matplotlib.pyplot as plt
facials = ['anger','contentment','disgust','happy','sadness','surprise']
result = {}
pairsImage = pickle.load(open('testPairs.pkl','rb'))
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image_folder", required=True,
help="path to image folder")
ap.add_argument("-s", "--save", required=True,
help="save pair of images to file")
args = vars(ap.parse_args())
def majority(pred):
pred = pred.mean()
if pred > 0.5: return 1
else: return 0
'''
return pred.mean(axis=0).argmax()
count = [0,0]
for i in range(len(pred)):
count[pred[i]] =count[pred[i]] + 1
if count[0] > count[1]:
return 0
else:
return 1
'''
def showFaces(s1,s2,path):
import cv2
img = cv2.imread(path + s1 + '/0001.jpg')
fig = plt.figure()
fig.add_subplot(1,2,1)
plt.imshow(img)
fig.add_subplot(1,2,2)
plt.imshow(cv2.imread(path + s2 + '/0001.jpg'))
plt.show()
for facial in facials:
#model = pickle.load(open(facial + '/model.dat','rb'))
model = xgb.Booster()
model.load_model(facial + '/model2.dat')
subjects = os.listdir(facial + '/' + args["image_folder"])
sub = []
while(len(subjects) > 0):
s1 = subjects[0]
for i in range(1,len(subjects)):
if subjects[i] == pairsImage[s1]:
break
s2 = pairsImage[s1]
subjects.pop(i)
subjects.pop(0)
name1 = '' + s1 + '.mp4'
filenames = os.listdir(facial + '/' + args["image_folder"]+ '/' + s1)
X = []
for filename in filenames:
X.append(np.load(facial + '/' + args["image_folder"]+ '/' + s1 + '/' + filename).flatten().tolist())
X = np.array(X)
pred_f = model.predict(xgb.DMatrix(X))
pred1 = pred_f.mean()
name2 = '' + s2 + '.mp4'
filenames = os.listdir(facial + '/' + args["image_folder"]+ '/' + s2)
X = []
for filename in filenames:
X.append(np.load(facial + '/' + args["image_folder"]+ '/' + s2 + '/' + filename).flatten().tolist())
X = np.array(X)
pred_f = model.predict(xgb.DMatrix(X))
pred2 = pred_f.mean()
if pred2> pred1:
result[name2] = 'fake'
result[name1] = 'true'
else:
result[name2] = 'true'
result[name1] = 'fake'
#showFaces(s1,s2,'/server/dataset/Real_Fake/data/testImage/')
'''
for subject in subjects:
X = []
pred_files = []
name = '' + subject + '.mp4'
filenames = os.listdir(facial + '/dataTest/' + subject)
for filename in filenames:
X.append(np.load(facial + '/dataTest/'+ subject + '/' + filename).flatten().tolist())
X = np.array(X)
if len(X) <=0: continue
pred_f = model.predict(xgb.DMatrix(X))
pred = majority(pred_f)
if pred == 0: result[name] = 'true'
else: result[name] = 'fake'
'''
pickle.dump(result, open(args["save"],"wb"))