forked from sourav-coder/face-recognition-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
234 lines (182 loc) · 7.65 KB
/
project.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# importing the libraries ---
import os
import face_recognition
import cv2
import numpy as np
import datetime
from SSIM_PIL import compare_ssim
from pymongo import MongoClient
import imagehash
from PIL import Image
import shortuuid
import pyrebase
mongoUrl = open('./hide.txt','r')
client = MongoClient(mongoUrl.read())
db = client.get_database("thirdeyeSpyDB")
records = db.takendatas
recordsGiven = db.givendatas
unknown = db.unknowns
def uploadImage():
print("from upload image.......................!!!!!")
config = {
"apiKey": "AIzaSyAgeOzZ8ldHh7rJC_6lTWgvXPU_kS55Fqc",
"authDomain": "thirdeye-spy2.firebaseapp.com",
"databaseURL": "https://thirdeye-spy2.firebaseio.com",
"projectId": "thirdeye-spy2",
"storageBucket": "thirdeye-spy2.appspot.com",
"messagingSenderId": "129369110189",
"appId": "1:129369110189:web:f486394302d8ecc4393824"
}
firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
# directory = "./unknownImages"
directory = "./Unknown/test"
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".PNG") or filename.endswith(
".jpeg"):
my_id = shortuuid.uuid()
path_on_cloud = my_id + "." + filename.split(".")[1]
timeOfUnknownImage = filename.split(".")[0]
path_local = filename
storage.child("Unknown/test/" + path_on_cloud).put("Unknown/test/" + path_local)
# get the url of the image
auth = firebase.auth()
email = "[email protected]"
password = "123456"
user = auth.sign_in_with_email_and_password(email, password)
url = storage.child("Unknown/test/" + path_on_cloud).get_url(user['idToken'])
newUnknown = {
"imageURL": url,
"time": timeOfUnknownImage
}
unknown.insert_one(newUnknown)
else:
pass
print('Upload Completed ...... ')
# loading the files of the directory ---
files = []
for dir, dirname, filename in os.walk('image'):
files = filename
known_faces = []
known_name = []
for name in files:
image = face_recognition.load_image_file(os.path.join('image/', name))
location = face_recognition.face_locations(image)
top, right, bottom, left = location[0]
print(location, "dir")
image = image[top:bottom, left:right]
encoding = face_recognition.face_encodings(image)[0]
known_faces.append(encoding)
known_name.append(name[:-4])
# segregation
# ---------------------- VIDEO --------------
cap = cv2.VideoCapture('samples_video/withMask1.mp4')
# cap = cv2.VideoCapture(1)
# cap1 = cv2.VideoCapture(1)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 3)
# opening ss.txt for writing the exact time of entering
file1 = open("ss.txt", "w")
# opening biden.txt for writing the exact time of entering
file2 = open('biden.txt', 'w')
# face classifier
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_fullbody.xml')
mouth = cv2.CascadeClassifier('haarcascades/mouth.xml')
process = True
# face_locations=[]
# face_encoding=[]
i = 0
j = 0
k = 0
hash1=''
while 1:
ret, frame = cap.read()
# ret1,frame1=cap1.read()
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
if ret:
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
# face_names = []
if process:
face_locations = face_recognition.face_locations(rgb_small_frame)
print("Face location ", face_locations)
if face_locations == []:
faces = face_cascade.detectMultiScale(rgb_small_frame, 1.01, 3)
windowWidth = rgb_small_frame.shape[1]
windowHeight = rgb_small_frame.shape[0]
for (x, y, w, h) in faces:
# x*=4
# y*=4
# w*=4
# h*=4
cv2.rectangle(rgb_small_frame, (x, y), (x + w, y + h), (245,245,245), 2)
j = 'Unknown/test/'+str(datetime.datetime.now().strftime("%Y-%m-%d-%I_%M_%S_%p")) +'.jpg'
print(w, h)
if k % 5 == 0 :# and (h >= windowHeight // 5) and (w >= windowHeight // 5):
print('test')
rgb_small_frame = cv2.cvtColor(rgb_small_frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(rgb_small_frame)
if len(str(hash1))==0:
print('Initial')
hash1=imagehash.average_hash(img)
# print('Write :-')
# cv2.imwrite(j, frame)
else:
hash2=imagehash.average_hash(img)
print("minus ",hash1-hash2)
# if the hash value is > cutoff then it must be some other image (experimented value)
if hash1-hash2 > 10:
print('Write 2:-')
print(j)
cv2.imwrite(j, frame)
hash1=hash2
k += 1
# cv2.imshow('video', frame)
# separate for known and unknown people
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_faces, face_encoding)
name = "Unknown"
print(matches)
face_distances = face_recognition.face_distance(known_faces, face_encoding)
best_match_index = np.argmin(face_distances)
print(known_name[best_match_index])
if matches[best_match_index]:
name = known_name[best_match_index]
face_names.append(name)
if name != 'Unknown':
print(name + ' ' + str(datetime.datetime.now())[:19])
# a.add(name+' '+str(datetime.datetime.now())[:19]+'\n')
if name == 'ss' and j % 5 == 0:
file1.write(str(datetime.datetime.now())[11:19] + '\n')
j += 1
if name == 'biden':
file2.write(str(datetime.datetime.now())[11:19] + '\n')
process = not process
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (119, 155, 0), 3)
cv2.rectangle(frame, (left, bottom - 30), (right, bottom), (130, 0, 75), cv2.FILLED)
font = cv2.FONT_HERSHEY_TRIPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.9, (255, 255, 255), 1)
mouth_reacts = mouth.detectMultiScale(frame[top:top + right, left:left + right], 1.1, 3)
if name == 'Unknown' and i % 5 == 0:
j = 'Unknown/' + str(i) + '_faces' + '.jpg'
print("length of mouth array",mouth_reacts)
if len(mouth_reacts)==0:
cv2.imwrite(j, frame)
i += 1
# small_frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
cv2.imshow('video',frame )
if cv2.waitKey(1) & 0xFF == ord('q'):
uploadImage()
break
cap.release()
# cap1.release()
# file1.writelines(a)
file1.close()
file2.close()
cv2.destroyAllWindows()