├── .DS_Store ├── README.MD ├── create_database.py ├── detector.py ├── haarcascade_frontalface_default.xml ├── record_face.py ├── requirements.txt └── trainer.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aryal007/opencv_face_recognition/d97477944cdcdf6abc6419cf34ae105d09a8d7bb/.DS_Store -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # Face recognition on OpenCV 3.4.0 2 | 3 | The full tutorial can be found on the link: http://www.python36.com/face-recognition-using-opencv-part-1/ 4 | 5 | ## Requirements can be found in the file requirements.txt 6 | 7 | ## Files 8 | 9 | 1> create_database.py (used to create sqlite database) 10 | 11 | 2> record_face.py (Records the Name associated with the face) 12 | 13 | 3> trainer.py (used to train LBPHFaceRecognizer for face recognition) 14 | 15 | 4> detector.py (detects the face from previously trained data and fetches corresponding info from database) 16 | 17 | -------------------------------------------------------------------------------- /create_database.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('database.db') 4 | 5 | c = conn.cursor() 6 | 7 | sql = """ 8 | DROP TABLE IF EXISTS users; 9 | CREATE TABLE users ( 10 | id integer unique primary key autoincrement, 11 | name text 12 | ); 13 | """ 14 | c.executescript(sql) 15 | 16 | conn.commit() 17 | 18 | conn.close() -------------------------------------------------------------------------------- /detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import sqlite3 4 | import os 5 | 6 | conn = sqlite3.connect('database.db') 7 | c = conn.cursor() 8 | 9 | fname = "recognizer/trainingData.yml" 10 | if not os.path.isfile(fname): 11 | print("Please train the data first") 12 | exit(0) 13 | 14 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 15 | cap = cv2.VideoCapture(0) 16 | recognizer = cv2.face.LBPHFaceRecognizer_create() 17 | recognizer.read(fname) 18 | 19 | while True: 20 | ret, img = cap.read() 21 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 22 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 23 | for (x,y,w,h) in faces: 24 | cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3) 25 | ids,conf = recognizer.predict(gray[y:y+h,x:x+w]) 26 | c.execute("select name from users where id = (?);", (ids,)) 27 | result = c.fetchall() 28 | name = result[0][0] 29 | if conf < 50: 30 | cv2.putText(img, name, (x+2,y+h-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (150,255,0),2) 31 | else: 32 | cv2.putText(img, 'No Match', (x+2,y+h-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2) 33 | cv2.imshow('Face Recognizer',img) 34 | k = cv2.waitKey(30) & 0xff 35 | if k == 27: 36 | break 37 | 38 | cap.release() 39 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /record_face.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import sqlite3 4 | import os 5 | 6 | conn = sqlite3.connect('database.db') 7 | if not os.path.exists('./dataset'): 8 | os.makedirs('./dataset') 9 | 10 | c = conn.cursor() 11 | 12 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 13 | 14 | cap = cv2.VideoCapture(0) 15 | 16 | uname = input("Enter your name: ") 17 | 18 | c.execute('INSERT INTO users (name) VALUES (?)', (uname,)) 19 | 20 | uid = c.lastrowid 21 | 22 | sampleNum = 0 23 | 24 | while True: 25 | ret, img = cap.read() 26 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 27 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 28 | for (x,y,w,h) in faces: 29 | sampleNum = sampleNum+1 30 | cv2.imwrite("dataset/User."+str(uid)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w]) 31 | cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2) 32 | cv2.waitKey(100) 33 | cv2.imshow('img',img) 34 | cv2.waitKey(1); 35 | if sampleNum > 20: 36 | break 37 | cap.release() 38 | 39 | conn.commit() 40 | 41 | conn.close() 42 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.14.0 2 | opencv-contrib-python==3.4.0.12 3 | opencv-python==3.4.0.12 4 | Pillow==5.0.0 5 | -------------------------------------------------------------------------------- /trainer.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import numpy as np 4 | from PIL import Image 5 | 6 | recognizer = cv2.face.LBPHFaceRecognizer_create() 7 | path = 'dataset' 8 | if not os.path.exists('./recognizer'): 9 | os.makedirs('./recognizer') 10 | 11 | def getImagesWithID(path): 12 | imagePaths = [os.path.join(path,f) for f in os.listdir(path)] 13 | faces = [] 14 | IDs = [] 15 | for imagePath in imagePaths: 16 | faceImg = Image.open(imagePath).convert('L') 17 | faceNp = np.array(faceImg,'uint8') 18 | ID = int(os.path.split(imagePath)[-1].split('.')[1]) 19 | faces.append(faceNp) 20 | IDs.append(ID) 21 | cv2.imshow("training",faceNp) 22 | cv2.waitKey(10) 23 | return np.array(IDs), faces 24 | 25 | Ids, faces = getImagesWithID(path) 26 | recognizer.train(faces,Ids) 27 | recognizer.save('recognizer/trainingData.yml') 28 | cv2.destroyAllWindows() --------------------------------------------------------------------------------