Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 77df39e

Browse files
Recognition
1 parent 15d6465 commit 77df39e

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

‎Face_Recognition/Recognition.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
haar_cascade = cv.CascadeClassifier('haar_face.xml')
55

66
people = []
7-
DIR = r'images' ## DIR - path of the folder containing images
7+
DIR = r'images' # DIR - path of the folder containing images
88
for i in os.listdir(DIR):
99
people.append(i)
1010

@@ -18,16 +18,16 @@
1818
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
1919
cv.imshow('Person', gray)
2020

21-
faces_rect = haar_cascade.detectMultiScale(gray,1.1,4)
21+
faces_rect = haar_cascade.detectMultiScale(gray, 1.1, 4)
22+
23+
for (x, y, w, h) in faces_rect:
24+
faces = gray[y:y+h, x:x+h]
2225

23-
for (x,y,w,h) in faces_rect:
24-
faces = gray[y:y+h,x:x+h]
25-
2626
label, confidence = face_recognizer.predict(faces)
27-
cv.putText(img,str(people[label]), (20,20), cv.FONT_HERSHEY_COMPLEX, 1.0,
28-
(0,255,0), thickness=2)
29-
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)
27+
cv.putText(img,str(people[label]), (20,20), cv.FONT_HERSHEY_COMPLEX, 1.0,
28+
(0,255,0), thickness=2)
29+
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)
3030

3131
cv.imshow('detected face', img)
3232

33-
cv.waitKey(0)
33+
cv.waitKey(0)

‎Face_Recognition/face_train.py‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
11
# 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
55

66
import os
77
import cv2 as cv
88
import numpy as np
99

1010
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
1313
people.append(i)
1414

1515
haar_cascade = cv.CascadeClassifier('haar_face.xml')
1616

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 = []
1919
labels = []
2020

21+
2122
def train_face():
2223
for person in people:
23-
path = os.path.join(DIR,person)
24+
path = os.path.join(DIR,person)
2425
label = people.index(person)
2526

2627
for img in os.listdir(path):
27-
img_path = os.path.join(path,img)
28+
img_path = os.path.join(path,img)
2829

2930
img_arr = cv.imread(img_path)
30-
gray = cv.cvtColor(img_arr,cv.COLOR_BGR2GRAY)
31+
gray = cv.cvtColor(img_arr,cv.COLOR_BGR2GRAY)
3132

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)
3335

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]
3638
features.append(face_roi)
3739
labels.append(label)
3840

41+
3942
train_face()
4043

4144
# Feature and labels numpy array
42-
features = np.array(features,dtype='object')
45+
features = np.array(features,dtype='object')
4346
labels = np.array(labels)
4447

4548
# Train our recognizer
4649
face_recognizer = cv.face.LBPHFaceRecognizer_create()
4750

4851
# Trained on features and the labels list
49-
face_recognizer.train(features,labels)
52+
face_recognizer.train(features,labels)
5053

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
5255
# we can easily access this trained model
5356
face_recognizer.save('face_trained.yml')
5457

55-
#### save the features and labels into the numpy file
58+
#save the features and labels into the numpy file
5659
# simply remove the comments of the below 2 line
5760
#
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /