├── main.py └── persons ├── Salar.jpg ├── bill.jpg ├── elon.jpg └── steve.jpg /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import face_recognition 4 | import os 5 | 6 | path = 'persons' 7 | images = [] 8 | classNames = [] 9 | personsList = os.listdir(path) 10 | 11 | for cl in personsList: 12 | curPersonn = cv2.imread(f'{path}/{cl}') 13 | images.append(curPersonn) 14 | classNames.append(os.path.splitext(cl)[0]) 15 | print(classNames) 16 | 17 | def findEncodeings(image): 18 | encodeList = [] 19 | for img in images: 20 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 21 | encode = face_recognition.face_encodings(img)[0] 22 | encodeList.append(encode) 23 | return encodeList 24 | 25 | encodeListKnown = findEncodeings(images) 26 | print('Encoding Complete.') 27 | 28 | cap = cv2.VideoCapture(1) 29 | 30 | while True: 31 | _, img = cap.read() 32 | 33 | imgS = cv2.resize(img, (0,0), None, 0.25, 0.25) 34 | imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB) 35 | 36 | faceCurentFrame = face_recognition.face_locations(imgS) 37 | encodeCurentFrame = face_recognition.face_encodings(imgS, faceCurentFrame) 38 | 39 | for encodeface, faceLoc in zip(encodeCurentFrame, faceCurentFrame): 40 | matches = face_recognition.compare_faces(encodeListKnown, encodeface) 41 | faceDis = face_recognition.face_distance(encodeListKnown, encodeface) 42 | matchIndex = np.argmin(faceDis) 43 | 44 | if matches[matchIndex]: 45 | name = classNames[matchIndex].upper() 46 | print(name) 47 | y1, x2, y2, x1 = faceLoc 48 | y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4 49 | cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 2) 50 | cv2.rectangle(img, (x1,y2-35), (x2,y2), (0,0,255), cv2.FILLED) 51 | cv2.putText(img, name, (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 2) 52 | 53 | cv2.imshow('Face Recogntion', img) 54 | cv2.waitKey(1) -------------------------------------------------------------------------------- /persons/Salar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salar-dev/Face-recognition-python-project/945c19b8e997b5440507e825dc5af9bfa274a1c6/persons/Salar.jpg -------------------------------------------------------------------------------- /persons/bill.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salar-dev/Face-recognition-python-project/945c19b8e997b5440507e825dc5af9bfa274a1c6/persons/bill.jpg -------------------------------------------------------------------------------- /persons/elon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salar-dev/Face-recognition-python-project/945c19b8e997b5440507e825dc5af9bfa274a1c6/persons/elon.jpg -------------------------------------------------------------------------------- /persons/steve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salar-dev/Face-recognition-python-project/945c19b8e997b5440507e825dc5af9bfa274a1c6/persons/steve.jpg --------------------------------------------------------------------------------