Piarasingh85 commited on
Commit
c3e36c9
·
verified ·
1 Parent(s): d5ab27d

Upload 2 files

Browse files
Files changed (2) hide show
  1. face_image_storer.py +24 -0
  2. face_trainer.py +30 -0
face_image_storer.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 #pip install opencv-contrib-python
2
+ cam = cv2.VideoCapture(0)
3
+ detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
4
+
5
+
6
+
7
+ Id=input('enter your id')
8
+ sampleNum=0
9
+ while(True):
10
+ ret, img = cam.read()
11
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
12
+ faces = detector.detectMultiScale(gray, 1.3, 5)
13
+ for (x,y,w,h) in faces:
14
+ cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
15
+
16
+ #saving the captured face in the dataset folder
17
+ cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
18
+ sampleNum=sampleNum+1
19
+ cv2.imshow('frame',img)
20
+ # break if the sample number is morethan 20
21
+ if sampleNum>20:
22
+ break
23
+ cam.release()
24
+ cv2.destroyAllWindows()
face_trainer.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ recognizer=cv2.face.LBPHFaceRecognizer_create()
7
+ path='dataSet'
8
+ def getImagesWithID(path):
9
+ imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
10
+ faces=[]
11
+ IDs=[]
12
+
13
+ for imagepath in imagePaths:
14
+ faceImg=Image.open(imagepath).convert('L')
15
+ faceNp=np.array(faceImg,'uint8')
16
+ print(imagepath)
17
+ ID=int(os.path.split(imagepath)[-1].split(".")[1])
18
+ #dataset/User.1.3
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('trainingData.yml')
28
+ cv2.destroyAllWindows()
29
+
30
+