-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.py
98 lines (88 loc) · 3.05 KB
/
test3.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
from keras.models import load_model
# model = load_model('./model/nn4.small2.lrn.h5')
from keras.utils import CustomObjectScope
import tensorflow as tf
with CustomObjectScope({'tf': tf}):
model = load_model('model/keras/facenet_keras.h5')
model.summary()
################################################################################################
import os
import cv2
import glob
import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#################################################################################################
def distance(embeddings1, embeddings2):
diff = np.subtract(embeddings1, embeddings2)
dist = np.sum(np.square(diff),1)
return dist
def most_similar(y, y_true , names):
min_d = distance(y, y_true[0])
min_i = 0
for i in range(1,len(y_true)):
print(i)
d = distance(y,y_true[i])
if(d < min_d):
min_d = d
min_i = i
return names[min_i]
#################################################################################
y_true = []
paths = (glob.glob(r'images\single_people\**\*.jpg'))
# paths ='images/single_people/shre/2.jpg'
print(paths)
names = []
for path in paths:
# faces = None
print(path)
img = cv2.imread(path)
print(img.shape)
img = cv2.resize(img, (640,640))
names.append(path.split('\\')[-2])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# cv2.imshow('I', img)
# cv2.waitKey(0)
# print(faces)
if len(faces) == 0:
continue
for (x,y,w,h) in faces:
cv2.rectangle(img,(x-20,y-20),(x+w+20,y+h+20),(255,0,0),2)
# roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y-20:y+h+20, x-20:x+w+20]
input_ = cv2.resize(roi_color, (160,160))
print('Input_shape', input_.shape)
y = model.predict_on_batch(np.array([input_])/255.0)
# print('Y_true', y)
y_true.append(y)
cv2.imshow('I', img )
cv2.waitKey(0)
print(len(y_true))
cv2.destroyAllWindows()
##########################################################################
i=0
paths = (glob.glob(r'images\two_people\**\*.jpg'))
for path in paths:
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
# cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.rectangle(img, (x-20,y-20), (x+w+20, y+h+20),(0,255,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = rgb[y-20:y+h+20, x-20:x+w+20]
print('Actua shape', roi_color.shape)
input_ = cv2.resize(roi_color, (160,160))
print('Input_shape', input_.shape)
y = model.predict_on_batch(np.array([input_])/255.0)
# print('Y_true', y)
name = most_similar(y,y_true, names)
cv2.putText(img, name,org= (100+100*i,100), fontFace =cv2.FONT_HERSHEY_SIMPLEX ,
fontScale = 1 ,color =(255,0,0), thickness =3 )
i=i+1
cv2.imshow('img',img)
cv2.waitKey(0)
############################################################################
cv2.destroyAllWindows()