1
1
# Face Recognition with OpenCV built in classifier
2
- # Aim: This script recognizes the face in an image using OpenCV's built-in recognizer. The vast application of this project is
3
- # recognizing the images in the running videos.
4
- # Feature: Easy and understandable code
2
+ # Aim: This script recognizes the face in an image using OpenCV's built-in recognizer. The vast application of this project is
3
+ # recognizing the images in the running videos.
4
+ # Feature: Easy and understandable code
5
5
6
6
import os
7
7
import cv2 as cv
8
8
import numpy as np
9
9
10
10
people = []
11
- DIR = r'images' # DIR - path of the folder containing images
12
- for i in os .listdir (DIR ): # Will append the names of the person in the people list
11
+ DIR = r'images' # DIR - path of the folder containing images
12
+ for i in os .listdir (DIR ): # Will append the names of the person in the people list
13
13
people .append (i )
14
14
15
15
haar_cascade = cv .CascadeClassifier ('haar_face.xml' )
16
16
17
- # For every face in the list the corresponding labels to the features are designated.
18
- features = []
17
+ # For every face in the list the corresponding labels to the features are designated.
18
+ features = []
19
19
labels = []
20
20
21
+
21
22
def train_face ():
22
23
for person in people :
23
- path = os .path .join (DIR ,person )
24
+ path = os .path .join (DIR ,person )
24
25
label = people .index (person )
25
26
26
27
for img in os .listdir (path ):
27
- img_path = os .path .join (path ,img )
28
+ img_path = os .path .join (path ,img )
28
29
29
30
img_arr = cv .imread (img_path )
30
- gray = cv .cvtColor (img_arr ,cv .COLOR_BGR2GRAY )
31
+ gray = cv .cvtColor (img_arr ,cv .COLOR_BGR2GRAY )
31
32
32
- face_rect = haar_cascade .detectMultiScale (gray , scaleFactor = 1.1 , minNeighbors = 4 )
33
+ face_rect = haar_cascade .detectMultiScale (
34
+ gray , scaleFactor = 1.1 , minNeighbors = 4 )
33
35
34
- for (x ,y , w , h ) in face_rect :
35
- face_roi = gray [y :y + h , x :x + w ]
36
+ for (x ,y , w , h ) in face_rect :
37
+ face_roi = gray [y :y + h , x :x + w ]
36
38
features .append (face_roi )
37
39
labels .append (label )
38
40
41
+
39
42
train_face ()
40
43
41
44
# Feature and labels numpy array
42
- features = np .array (features ,dtype = 'object' )
45
+ features = np .array (features ,dtype = 'object' )
43
46
labels = np .array (labels )
44
47
45
48
# Train our recognizer
46
49
face_recognizer = cv .face .LBPHFaceRecognizer_create ()
47
50
48
51
# Trained on features and the labels list
49
- face_recognizer .train (features ,labels )
52
+ face_recognizer .train (features ,labels )
50
53
51
- # Just like haar cascade yml file, we can save this file in yml format so that
54
+ # Just like haar cascade yml file, we can save this file in yml format so that
52
55
# we can easily access this trained model
53
56
face_recognizer .save ('face_trained.yml' )
54
57
55
- #### save the features and labels into the numpy file
58
+ #save the features and labels into the numpy file
56
59
# simply remove the comments of the below 2 line
57
60
#
58
- # np.save('features.npy', features)
59
- # np.save('labels.npy',labels)
61
+ # np.save('features.npy', features)
62
+ # np.save('labels.npy',labels)
0 commit comments