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 895bc5c

Browse files
Face Recognition
1 parent 1d5fb83 commit 895bc5c

File tree

5 files changed

+59589
-0
lines changed

5 files changed

+59589
-0
lines changed

‎Face_Recognition/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Face Recognition using OpenCV's Builtin Recognizer
2+
3+
## About This Project
4+
5+
Aim: This script recognizes the face in an image using OpenCV's built-in recognizer.
6+
7+
## Application
8+
9+
Using this script we can recognize the faces in the running videos (frame by frame).
10+
11+
## How To Run
12+
13+
To run the script use following commands
14+
15+
1. Get the required modules
16+
```bash
17+
pip install -r requirements.txt
18+
```
19+
20+
2. Get multiple images of multiple person and add the path of that folder in the following line of face_train.py
21+
```python
22+
DIR = r' '
23+
```
24+
3. Run face_train.py so that it could read the faces and label them
25+
26+
4. Files face_trained.yml, features.npy and labels.npy will be created
27+
28+
5. Run the recognition.py and insert any random images of the person you are recognizing
29+
```python
30+
img = cv.imread(r' ')
31+
```
32+
33+

‎Face_Recognition/Recognition.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
import cv2 as cv
3+
import os
4+
haar_cascade = cv.CascadeClassifier('haar_face.xml')
5+
6+
people = []
7+
DIR = r' ' ## DIR - path of the folder containing images
8+
for i in os.listdir(DIR):
9+
people.append(i)
10+
11+
# LBPHFaceRecognizer - instatiate the face recognizer
12+
face_recognizer = cv.face.LBPHFaceRecognizer_create()
13+
# We are using the trained yml file.
14+
face_recognizer.read('face_trained.yml')
15+
16+
img = cv.imread(r' ')
17+
18+
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
19+
cv.imshow('Person', gray)
20+
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]
25+
26+
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)
30+
31+
cv.imshow('detected face', img)
32+
33+
cv.waitKey(0)

‎Face_Recognition/face_train.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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
5+
6+
import os
7+
import cv2 as cv
8+
import numpy as np
9+
10+
people = []
11+
DIR = r' ' # 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+
people.append(i)
14+
15+
haar_cascade = cv.CascadeClassifier('haar_face.xml')
16+
17+
# For every face in the list the corresponding labels to the features are designated.
18+
features = []
19+
labels = []
20+
21+
def train_face():
22+
for person in people:
23+
path = os.path.join(DIR,person)
24+
label = people.index(person)
25+
26+
for img in os.listdir(path):
27+
img_path = os.path.join(path,img)
28+
29+
img_arr = cv.imread(img_path)
30+
gray = cv.cvtColor(img_arr,cv.COLOR_BGR2GRAY)
31+
32+
face_rect = haar_cascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors=4)
33+
34+
for (x,y,w,h) in face_rect:
35+
face_roi = gray[y:y+h , x:x+w]
36+
features.append(face_roi)
37+
labels.append(label)
38+
39+
train_face()
40+
41+
# Feature and labels numpy array
42+
features = np.array(features,dtype='object')
43+
labels = np.array(labels)
44+
45+
# Train our recognizer
46+
face_recognizer = cv.face.LBPHFaceRecognizer_create()
47+
48+
# Trained on features and the labels list
49+
face_recognizer.train(features,labels)
50+
51+
# Just like haar cascade yml file, we can save this file in yml format so that
52+
# we can easily access this trained model
53+
face_recognizer.save('face_trained.yml')
54+
55+
# save the features and labels into the numpy file
56+
np.save('features.npy', features)
57+
np.save('labels.npy',labels)
58+

0 commit comments

Comments
(0)

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