diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ca6fe06 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "git.ignoreLimitWarning": true +} \ No newline at end of file diff --git a/AttendanceRecord.csv b/AttendanceRecord.csv new file mode 100644 index 0000000..70aeea0 --- /dev/null +++ b/AttendanceRecord.csv @@ -0,0 +1,4 @@ +NAME, TIME + + +BERTOLDAS,00:54:15 \ No newline at end of file diff --git a/Photos/Bertoldas.jpg b/Photos/Bertoldas.jpg new file mode 100644 index 0000000..b7923be Binary files /dev/null and b/Photos/Bertoldas.jpg differ diff --git a/face.py b/face.py new file mode 100644 index 0000000..2a5909c --- /dev/null +++ b/face.py @@ -0,0 +1,59 @@ +import numpy as np +import face_recognition as fr +import cv2 + +video_capture = cv2.VideoCapture(0) + +bertas_image = fr.load_image_file("Images/bertas.jpg") +bertas_face_encoding = fr.face_encodings(bertas_image)[0] + +known_face_encodings = [bertas_face_encoding] +known_face_names = ["bertas"] + +while True: + ret, frame = video_capture.read() + + rgb_frame = frame[:, :, ::-1] + + face_locations = fr.face_locations(rgb_frame) + face_encodings = fr.face_encodings(rgb_frame, face_locations) + + for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): + + matches = fr.compare_faces(known_face_encodings, face_encoding) + + name = "unknown" + face_distances = fr.face_distance(known_face_encodings, face_encoding) + best_match_index = np.argmin(face_distances) + if matches[best_match_index]: + name = known_face_names[best_match_index] + + cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) + + cv2.rectangle(frame, (left, bottom - 35), + (right, bottom), (0, 0, 255), cv2.FILLED) + font = cv2.FONT_HERSHEY_SIMPLEX + cv2.putText(frame, name, (left + 6, bottom - 6), + font, 1.0, (255, 255, 255), 1) + + cv2.imshow('webcam', frame) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +video_capture.release() +cv2.destroyAllWindows() + + +def findEncodings(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 = findEncodings(images) + + +print('Encoding Complete') diff --git a/test.py b/test.py new file mode 100644 index 0000000..d97ea89 --- /dev/null +++ b/test.py @@ -0,0 +1,72 @@ +import face_recognition as fc +import os +import numpy as np +import cv2 +from datetime import datetime + +path = 'Photos' +photos = [] +NameClass = [] +ImgList = os.listdir(path) + +for cl in ImgList: + curImg = cv2.imread(f'{path}/{cl}') + photos.append(curImg) + NameClass.append(os.path.splitext(cl)[0]) + + +def EncodingsFind(images): + ListEncodes = [] + for img in photos: + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + encode = fc.face_encodings(img)[0] + ListEncodes.append(encode) + return ListEncodes + + +def Attendance(name): + with open('AttendanceRecord.csv', 'r+') as f: + DataList = f.readlines() + nameList = [] + for line in DataList: + gap = line.split(',') + nameList.append(gap[0]) + if name not in nameList: + time = datetime.now() + dtString = time.strftime('%H:%M:%S') + f.writelines(f'\n{name},{dtString}') + + +ListEncodesKnown = EncodingsFind(photos) + +capture = cv2.VideoCapture(0) + +while True: + success, img = capture.read() + imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25) + imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB) + + facesFrame = fc.face_locations(imgS) + encodesCurFrame = fc.face_encodings(imgS, facesFrame) + + for encodeFace, faceLoc in zip(encodesCurFrame, facesFrame): + match = fc.compare_faces(ListEncodesKnown, encodeFace) + Distance = fc.face_distance(ListEncodesKnown, encodeFace) + + matchIndex = np.argmin(Distance) + + if match[matchIndex]: + name = NameClass[matchIndex].upper() + + y1, x2, y2, x1 = faceLoc + y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4 + cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) + cv2.rectangle(img, (x1, y2-35), (x2, y2), (0, 255, 0), cv2.FILLED) + cv2.putText(img, name, (x1+6, y2-6), + cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2) + + Attendance(name) + + cv2.imshow('webcam', img) + if cv2.waitKey(1) & 0xFF == ord('q'): + break