Spaces:
Running
Running
UmairMirza
commited on
Commit
•
96c28ce
1
Parent(s):
982a765
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import face_recognition
|
4 |
+
import os
|
5 |
+
from datetime import datetime
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
path = 'images'
|
9 |
+
images = []
|
10 |
+
personNames = []
|
11 |
+
myList = os.listdir(path)
|
12 |
+
unkownEncodings=[]
|
13 |
+
print(myList)
|
14 |
+
for cu_img in myList:
|
15 |
+
current_Img = cv2.imread(f'{path}/{cu_img}')
|
16 |
+
images.append(current_Img)
|
17 |
+
personNames.append(os.path.splitext(cu_img)[0])
|
18 |
+
print(personNames)
|
19 |
+
|
20 |
+
|
21 |
+
def faceEncodings(images):
|
22 |
+
encodeList = []
|
23 |
+
for img in images:
|
24 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
25 |
+
encode = face_recognition.face_encodings(img)[0]
|
26 |
+
encodeList.append(encode)
|
27 |
+
return encodeList
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
encodeListKnown = faceEncodings(images)
|
32 |
+
print('All Encodings Complete!!!')
|
33 |
+
|
34 |
+
def Attandance(video):
|
35 |
+
cap = cv2.VideoCapture("messi-ronaldo-fb.jpg")
|
36 |
+
index=1
|
37 |
+
while True:
|
38 |
+
#try:
|
39 |
+
ret, frame = cap.read()
|
40 |
+
faces = cv2.resize(frame, (0, 0), None, 0.25, 0.25)
|
41 |
+
faces = cv2.cvtColor(faces, cv2.COLOR_BGR2RGB)
|
42 |
+
|
43 |
+
facesCurrentFrame = face_recognition.face_locations(faces)
|
44 |
+
encodesCurrentFrame = face_recognition.face_encodings(faces, facesCurrentFrame)
|
45 |
+
|
46 |
+
for encodeFace, faceLoc in zip(encodesCurrentFrame, facesCurrentFrame):
|
47 |
+
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
|
48 |
+
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
|
49 |
+
# print(faceDis)
|
50 |
+
matchIndex = np.argmin(faceDis)
|
51 |
+
|
52 |
+
if matches[matchIndex]:
|
53 |
+
name = personNames[matchIndex].upper()
|
54 |
+
names.append(name)
|
55 |
+
|
56 |
+
if cv2.waitKey(1) == 2:
|
57 |
+
break
|
58 |
+
|
59 |
+
return ''.join(name)
|
60 |
+
|
61 |
+
demo=gr.Interface(fn=Attandance,
|
62 |
+
inputs="video",
|
63 |
+
outputs="text",
|
64 |
+
title="Face Attendance",
|
65 |
+
|
66 |
+
)
|
67 |
+
demo.launch(debug=True)
|
68 |
+
print(len(unkownEncodings))
|
69 |
+
|
70 |
+
cap.release()
|
71 |
+
cv2.destroyAllWindows()
|