-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecognize.py
215 lines (193 loc) · 6.51 KB
/
Recognize.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#%%
import os
import math
import numpy as np
import json
# %%
import cv2
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# out = cv2.VideoWriter("./video.MP4", fourcc, 30.0, (1080, 1920))
# %%
import face_recognition
from PIL import Image, ImageDraw
# %%
def CalEqualizeHist(image):
output_image = cv2.equalizeHist(image)
return output_image
# %%
def CalLaplacian(image):
gray_lap = cv2.Laplacian(image,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(gray_lap)
return laplacian
# %%
def CalLog(image):
image_cp = np.copy(image)
output_image = 255 * np.log(1 + image_cp.astype(int) / 255)
return output_image
# %%
def JudgeBrightness(image):
sum = 0
count = 0
for i in image:
for j in i:
sum = sum + j
count = count + 1
return sum / count
# %%
def CalGamma(average):
return math.log(0.5, average / 255)
# %%
def GammaTransformation(image, gamma):
image_cp = np.copy(image)
output_image = 255 * np.power(image_cp.astype(int) / 255, gamma)
return output_image
# %%
def CalDistance(pointleft, pointright):
distance = math.pow(math.pow(pointleft[0] - pointright[0], 2) + math.pow(pointleft[1] - pointright[1], 2), 0.5)
return distance
# %%
def CalAround(pointleft, pointright, length):
distance = CalDistance(pointleft, pointright)
return math.acos(distance / length) * 180 / 3.14
# %%
def CalLeftRight(pointleft, pointright):
return math.atan2(pointright[1] - pointleft[1], pointright[0] - pointleft[0]) * 180 / 3.14
# %%
def CalUpDown(pointup, pointdown, length):
distance = abs(pointdown[1] - pointup[1])
return math.asin(distance / length) * 180 / 3.14
# %%
def CalCenter(pointgroup):
x = y = n = 0
for point in pointgroup:
x += point[0]
y += point[1]
n += 1
return (x / n, y / n)
# %%
def CalAroundLength(pointgroup):
length = 0
p = pointgroup[0]
flag = 0
for point in pointgroup:
if flag == 1:
plen = CalDistance(p, point)
length = length + plen
else:
flag = 1
return length
# %%
# imagefirst = face_recognition.load_image_file("./image/IMG_2100.jpg")
def ImageShow(imagefirst):
imagelist = face_recognition.face_landmarks(imagefirst)
# print(imagelist)
if (len(imagelist) == 0):
return
else:
# imgroup = []
for im in imagelist:
# pil_image = Image.fromarray(im)
lengtheyes = (CalAroundLength(im['left_eye']) + CalAroundLength(im['right_eye'])) / 2
rate = 1
# global eyelen
# if eyelen == 0:
# eyelen = lengtheyes
# else:
# rate = lengtheyes / eyelen
line = []
line.append(CalCenter(im['left_eye']))
line.append(CalCenter(im['right_eye']))
line2 = []
line2.append(im['nose_bridge'][0])
line2.append(im['nose_bridge'][len(im['nose_bridge']) - 1])
# imgg = []
# imgg.append(CalDistance(line[0], line[1]))
# imgg.append(CalDistance(line2[0], line2[1]))
# imgg.append(CalLeftRight(line[0], line[1]))
# print('eye_length:{}'.format(CalDistance(line[0], line[1])))
# print('nose_length:{}'.format(CalDistance(line2[0], line2[1])))
# print('around:{}'.format(CalLeftRight(line[0], line[1])))
# imgroup.append({'x':imgg})
# imgroup.append({'y':1})
# img = cv2.cvtColor(np.asarray(pil_image),cv2.COLOR_RGB2BGR)
# out.write(img)
# pil_image.show()
# return imgroup
# %%
def FaceRecognize(know_im, imagefirst):
# know_im = face_recognition.load_image_file("./image/image_true/doggy_true.jpg")
know_encodings = face_recognition.face_encodings(know_im)
first_encodings = face_recognition.face_encodings(imagefirst)
if len(know_encodings) == 0:
return 'Error, no person in know_encoding'
elif len(first_encodings) == 0:
return 'Error, no person in this image'
else:
trueandfalse = []
for first_encode in first_encodings:
flag = 0
for know_encode in know_encodings:
if face_recognition.compare_faces([know_encode], first_encode)[0]:
flag = 1
if flag == 0:
trueandfalse.append("false")
else:
trueandfalse.append("true")
# return face_recognition.compare_faces([know_encodings[0]], first_encodings[0])[0]
return trueandfalse
# %%
filename = './image'
for dirname in os.listdir(filename):
# print(dirname)
# print(os.path.join(filename, dirname + '/movie'))
# print(os.listdir(os.path.join(filename, dirname + '/movie')))
thisdir = os.path.join(filename, dirname)
print(thisdir)
# movdir = os.listdir(os.path.join(filename, dirname + '/movie'))
try:
know_im = face_recognition.load_image_file(thisdir + '/photo.jpeg')
imagefirst = face_recognition.load_image_file(thisdir + '/request_photo.jpeg')
# img_src = cv2.imread("E:\\vs_python\\FaceRecognition\\image\\image_current\\request_photo.jpeg", 0)
except Exception as e:
print(e)
else:
# gray2 = Image.fromarray(img_src)
# gray2.show()
# pil_image = CalLog(img_src)
# print(pil_image)
# pil_image = Image.fromarray(pil_image)
# pil_image.show()
# pil_image = CalEqualizeHist(img_src)
# # print(pil_image)
# pil_image = Image.fromarray(pil_image)
# pil_image.show()
gray = cv2.cvtColor(imagefirst, cv2.COLOR_BGR2GRAY)
# gray2 = Image.fromarray(gray)
# gray2.show()
# imm = Image.fromarray(imagefirst)
# imm.show()
gamma = CalGamma(JudgeBrightness(gray))
img_gamma = GammaTransformation(imagefirst, gamma)
# img_gamma = cv2.cvtColor(img_gamma, cv2.COLOR_BGR2GRAY)
img_gamma = Image.fromarray(np.uint8(img_gamma))
# img_gamma.show()
print(str(FaceRecognize(know_im, np.uint8(img_gamma))))
ImageShow(np.uint8(img_gamma))
# %%
# try:
# frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# except Exception as e:
# print(e)
# %%
# try:
# pil_image = Image.fromarray(frame)
# img = cv2.cvtColor(np.asarray(pil_image),cv2.COLOR_RGB2BGR)
# # pil_image = Image.fromarray(img)
# # p
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# gamma = CalGamma(JudgeBrightness(gray))
# frame = GammaTransformat
# ImageShow(np.uint8(frame))
# print(FaceRecognize(know_im, np.uint8(frame)))
# except Exception as e:
# print(e)