File size: 1,956 Bytes
96c28ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime
import gradio as gr

path = 'images'
images = []
personNames = []
myList = os.listdir(path)
unkownEncodings=[]
print(myList)
for cu_img in myList:
    current_Img = cv2.imread(f'{path}/{cu_img}')
    images.append(current_Img)
    personNames.append(os.path.splitext(cu_img)[0])
print(personNames)


def faceEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encodeList.append(encode)
    return encodeList



encodeListKnown = faceEncodings(images)
print('All Encodings Complete!!!')

def Attandance(video):
  cap = cv2.VideoCapture("messi-ronaldo-fb.jpg")
  index=1
  while True:
      #try:
          ret, frame = cap.read()
          faces = cv2.resize(frame, (0, 0), None, 0.25, 0.25)
          faces = cv2.cvtColor(faces, cv2.COLOR_BGR2RGB)
  
          facesCurrentFrame = face_recognition.face_locations(faces)
          encodesCurrentFrame = face_recognition.face_encodings(faces, facesCurrentFrame)
  
          for encodeFace, faceLoc in zip(encodesCurrentFrame, facesCurrentFrame):
              matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
              faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
              # print(faceDis)
              matchIndex = np.argmin(faceDis)
  
              if matches[matchIndex]:
                  name = personNames[matchIndex].upper()             
                  names.append(name)
      
          if cv2.waitKey(1) == 2:
              break

  return ''.join(name)

demo=gr.Interface(fn=Attandance,
                  inputs="video",
                  outputs="text",
                  title="Face Attendance",
                  
)
demo.launch(debug=True)
print(len(unkownEncodings))
  
  cap.release()
  cv2.destroyAllWindows()