|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +import face_recognition |
| 4 | +import os |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +path = 'Images' |
| 8 | +Images = [] |
| 9 | +PersonName = [] |
| 10 | +mylist = os.listdir(path) |
| 11 | +print(mylist) |
| 12 | +# for separating the name from their extensions |
| 13 | +for cu_img in mylist: |
| 14 | + current_Img = cv2.imread(f'{path}/{cu_img}') |
| 15 | + Images.append(current_Img) |
| 16 | + PersonName.append(os.path.splitext(cu_img)[0]) |
| 17 | +print(PersonName) |
| 18 | + |
| 19 | + |
| 20 | +def encodings(images): |
| 21 | + encodelist = [] |
| 22 | + for img in images: |
| 23 | + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 24 | + encode = face_recognition.face_encodings(img)[0] |
| 25 | + encodelist.append(encode) |
| 26 | + return encodelist |
| 27 | + |
| 28 | + |
| 29 | +encode_list_Known = encodings(Images) |
| 30 | +print("ALL ENCODING FOUND!!!") |
| 31 | + |
| 32 | + |
| 33 | +def attendance(name): |
| 34 | + with open('Attendence.csv', 'r+') as f: |
| 35 | + myDataList = f.readlines() |
| 36 | + nameList = [] |
| 37 | + for line in myDataList: |
| 38 | + entry = line.split(',') |
| 39 | + nameList.append(entry[0]) |
| 40 | + if name not in nameList: |
| 41 | + time_now = datetime.now() |
| 42 | + tStr = time_now.strftime('%H:%M:%S') |
| 43 | + dStr = time_now.strftime('%d/%m/%Y') |
| 44 | + f.writelines(f'\n{name},{tStr},{dStr}') |
| 45 | + |
| 46 | + |
| 47 | +cap = cv2.VideoCapture(0) |
| 48 | + |
| 49 | +while True: |
| 50 | + ret, frame = cap.read() |
| 51 | + faces = cv2.resize(frame, (0, 0), None, 0.25, 0.25) |
| 52 | + faces = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 53 | + |
| 54 | + faces_currentframe = face_recognition.face_locations(faces) |
| 55 | + encode_currentframe = face_recognition.face_encodings(faces, faces_currentframe) |
| 56 | + |
| 57 | + for encodeFace, faceLoc in zip(encode_currentframe, faces_currentframe): |
| 58 | + matches = face_recognition.compare_faces(encode_list_Known, encodeFace) |
| 59 | + faceDistance = face_recognition.face_distance(encode_list_Known, encodeFace) |
| 60 | + |
| 61 | + matchIndex = np.argmin(faceDistance) |
| 62 | + |
| 63 | + if matches[matchIndex]: |
| 64 | + name = PersonName[matchIndex].upper() |
| 65 | + y1, x2, y2, x1 = faceLoc |
| 66 | + #y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4 |
| 67 | + cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) |
| 68 | + cv2.rectangle(frame, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED) |
| 69 | + cv2.putText(frame, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2) |
| 70 | + attendance(name) |
| 71 | + |
| 72 | + cv2.imshow("camera", frame) |
| 73 | + if cv2.waitKey(10) == 13: |
| 74 | + break |
| 75 | +cap.release() |
| 76 | +cv2.destroyAllWindows() |
0 commit comments