-
Notifications
You must be signed in to change notification settings - Fork 0
/
biasRemontada.py
189 lines (130 loc) · 5.41 KB
/
biasRemontada.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
from os import listdir
from openvino.inference_engine import IECore
import cv2
import numpy as np
from scipy.spatial.distance import cosine
import imutils
import dlib
from numpy import savez_compressed
det_model=r"C:\Users\LENOVO\Desktop\FaceReid\detection_model\face-detection-0202.xml"
det_weights=os.path.splitext(det_model)[0] +'.bin'
#reid_model=r"C:\Users\LENOVO\Desktop\Detect&Recognize\face_net_mobile_face\model-0000.xml"
reid_model=r"E:\FINAL-YEAR-PROJECT\models\ds1\ds-0000.xml"
reid_weights=os.path.splitext(reid_model)[0] +'.bin'
def output_handler(frame,result,height,width):
faces=list()
for box in result[0][0]:
if box[2]>0.5:
xmin=int(box[3] *width)
ymin=int(box[4] *height)
xmax=int(box[5] *width)
ymax=int(box[6] *height)
face=frame[ymin:ymax,xmin:xmax]
face=cv2.resize(face,(128,128))
faces.append(face)
return faces
def extract_face(filename):
plugin=IECore()
net=plugin.read_network(model=det_model,weights=det_weights)
exec_net=plugin.load_network(network=net,device_name="CPU")
input_blob=list(net.input_info.keys())[0]
output_blob=next(iter(net.outputs))
b,c,h,w=net.input_info[input_blob].input_data.shape
image=cv2.imread(filename)
height=image.shape[0]
width=image.shape[1]
p_image=cv2.resize(image,(w,h))
p_image=p_image.transpose((2,0,1))
p_image=p_image.reshape(1,3,h,w)
infer_request=exec_net.start_async(request_id=0,inputs={input_blob:p_image})
status=exec_net.requests[0].wait(-1)
if status==0:
result=exec_net.requests[0].outputs[output_blob]
return output_handler(image,result,height,width)[0]
def reidentify(test_subject):
reid_plugin=IECore()
reid_net=reid_plugin.read_network(model=reid_model,weights=reid_weights)
reid_execnet=reid_plugin.load_network(network=reid_net,device_name="CPU")
reid_inputblob=list(reid_net.input_info.keys())[0]
reid_outputblob=next(iter(reid_net.outputs))
b,c,h,w=reid_net.input_info[reid_inputblob].input_data.shape
p_image=cv2.cvtColor(test_subject,cv2.COLOR_BGR2RGB)
p_image=cv2.resize(test_subject,(w,h))
p_image=p_image.transpose((2,0,1))
p_image=p_image.reshape(1,3,h,w)
infer_request=reid_execnet.start_async(request_id=0,inputs={reid_inputblob:p_image})
status=reid_execnet.requests[0].wait(-1)
if status==0:
result=reid_execnet.requests[0].outputs[reid_outputblob]
#This stores embeddings
#print(result[0])
#print('storing embedding')
#savez_compressed('tariq3.npz',result[0])
#return np.array(result).reshape((1,256))[0]
return result[0]
def is_match(known_embedding,candidate_embedding,thresh=0.5):
#calculate the distance between embeddings
score=cosine(known_embedding,candidate_embedding)
#score= np.sqrt(np.sum(np.square(np.subtract(known_embedding, candidate_embedding))))
if score<=thresh:
print('face is a match',('Score: ',score,' Threshold: ',thresh))
else:
print('face is not a match',('Score: ',score,' Threshold: ',thresh))
MOT={}
ANC={
'ben_afflek':"E:/FINAL-YEAR-PROJECT/Bias/archive2/ben_afflek/httpafilesbiographycomimageuploadcfillcssrgbdprgfacehqwMTENDgMDUODczNDcNTcjpg.jpg",
'elton_john':"E:/FINAL-YEAR-PROJECT/Bias/archive2/elton_john/httpmediapopsugarassetscomfilescbffewltonjpg.jpg",
'jerry_seinfeld':"E:/FINAL-YEAR-PROJECT/Bias/archive2/jerry_seinfeld/httpimagescontactmusiccomnewsimagesjerryseinfeldjpg.jpg",
'madonna':"E:/FINAL-YEAR-PROJECT/Bias/archive2/madonna/httpiamediaimdbcomimagesMMVBMTANDQNTAxNDVeQTJeQWpwZBbWUMDIMjQOTYVUXCRALjpg.jpg",
'mindy_kaling':"E:/FINAL-YEAR-PROJECT/Bias/archive2/mindy_kaling/httpcdncdnjustjaredcomwpcontentuploadsheadlinesmindykalingcomedypilotjpg.jpg"
}
directory="E:/FINAL-YEAR-PROJECT/Bias/archive2"
ben_anchor="E:/FINAL-YEAR-PROJECT/Bias/archive2/ben_afflek/httpafilesbiographycomimageuploadcfillcssrgbdprgfacehqwMTENDgMDUODczNDcNTcjpg.jpg"
elton_anchor="E:/FINAL-YEAR-PROJECT/Bias/archive2/elton_john/httpmediapopsugarassetscomfilescbffewltonjpg.jpg"
jerry_anchor=r"E:\FINAL-YEAR-PROJECT\Bias\archive2\jerry_seinfeld\httpimagescontactmusiccomnewsimagesjerryseinfeldjpg.jpg"
madonna_anchor=r"E:\FINAL-YEAR-PROJECT\Bias\archive2\madonna\httpiamediaimdbcomimagesMMVBMTANDQNTAxNDVeQTJeQWpwZBbWUMDIMjQOTYVUXCRALjpg.jpg"
mindy_anchor=r"E:\FINAL-YEAR-PROJECT\Bias\archive2\mindy_kaling\httpcdncdnjustjaredcomwpcontentuploadsheadlinesmindykalingcomedypilotjpg.jpg"
for name in listdir(directory):
print('Now on: ',name)
count=0
MOT[str(name)]=[]
path=directory+'/' +name
for file in listdir(path):
current_location=path+'/' +file
extracted_ancFace=extract_face(ANC[str(name)])
test_img=extract_face(current_location)
emb1=reidentify(extracted_ancFace)
emb2=reidentify(test_img)
score=cosine(emb1,emb2)
MOT[str(name)].append(score)
print('End of ',name)
print(MOT)
print("\n")
print("Bias Percentages")
print("\n")
print("At Threshold 0.5")
for name in MOT.keys():
values=MOT[str(name)]
masked=[]
for val in values:
if val<=0.5:
masked.append(1)
else:
masked.append(0)
percentage=sum(masked)/len(masked)
print(name," accuracy: ",percentage)
print("\n")
print("\n")
print("At Threshold 0.55")
for name in MOT.keys():
values=MOT[str(name)]
masked=[]
for val in values:
if val<=0.55:
masked.append(1)
else:
masked.append(0)
percentage=sum(masked)/len(masked)
print(name," accuracy: ",percentage)
print("\n")