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 3deb13d

Browse files
Recognition
1 parent 81e7238 commit 3deb13d

File tree

10 files changed

+24723
-26255
lines changed

10 files changed

+24723
-26255
lines changed

‎Face_Recognition/README.md‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@
22

33
## About This Project
44

5-
Aim: This script recognizes the face in an image using OpenCV's built-in recognizer.
5+
Aim: This script recognizes the face in from video stream.
6+
7+
## Implementation
8+
9+
Captures images from your webcam video stream
10+
Extracts all Faces from the image frame (using haarcascades)
11+
Stores the Face information into numpy arrays
12+
13+
1. Read and show video stream, capture images
14+
2. Detect Faces and show bounding box (haarcascade)
15+
3. Flatten the largest face image(gray scale) and save in a numpy array
16+
4. Repeat the above for multiple people to generate training data
17+
18+
Recognise faces using some classification algo - logistic, KNN, SVM etc.
19+
20+
1. Read a video stream using opencv
21+
2. extract faces out of it
22+
3. load the training data(numpy array of all the persons)
23+
a. x-values are stored in numpy arrays
24+
b. y-values we need to assign for each person
25+
4. use knn to find the prediction of face(int)
26+
5. map the predicted id to name of the user
27+
6. display the predictions on the screen - bounding box and name
628

729
## Application
830

9-
Using this script we can recognize the faces in the running videos (frame by frame).
31+
Using this script we can recognize the faces in the running videos (frame by frame). Face recognition is also useful in human computer interaction, virtual reality, database recovery, multimedia, computer entertainment, information security.
1032

1133
## How To Run
1234

@@ -17,17 +39,8 @@ To run the script use following commands
1739
pip install -r requirements.txt
1840
```
1941

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
42+
2. Get multiple images of multiple person from face_data_collect.py and save them in numpy array.
2543

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-
```
44+
3. Run the face_recognition.py
3245

3346

‎Face_Recognition/Recognition.py‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎Face_Recognition/cv_video_read.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Read a video Stream from Camera ( Frame by frame )
2+
3+
import cv2
4+
cap = cv2.VideoCapture(0)
5+
while True:
6+
ret, frame = cap.read()
7+
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
8+
if ret==False:
9+
continue
10+
11+
cv2.imshow("Video Frame", frame)
12+
cv2.imshow("gray frame", gray_frame)
13+
14+
# wait for user inqut -q, then u will stop the loop
15+
16+
key_pressed = cv2.waitKey(1) & 0xFF
17+
if key_pressed == ord('q'):
18+
break
19+
20+
cap.release()
21+
cv2.destroyAllWindows()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Write a Python Script that captures images from your webcam video stream
2+
# Extracts all Faces from the image frame (using haarcascades)
3+
# Stores the Face information into numpy arrays
4+
5+
# 1. Read and show video stream, capture images
6+
# 2. Detect Faces and show bounding box (haarcascade)
7+
# 3. Flatten the largest face image(gray scale) and save in a numpy array
8+
# 4. Repeat the above for multiple people to generate training data
9+
10+
11+
import cv2
12+
import numpy as np
13+
14+
#Init Camera
15+
cap = cv2.VideoCapture(0)
16+
17+
# Face Detection
18+
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
19+
20+
skip = 0
21+
face_data = []
22+
dataset_path = './data/'
23+
file_name = input("Enter the name of the person : ")
24+
while True:
25+
ret,frame = cap.read()
26+
27+
if ret==False:
28+
continue
29+
30+
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
31+
32+
33+
faces = face_cascade.detectMultiScale(frame,1.3,5)
34+
if len(faces)==0:
35+
continue
36+
37+
faces = sorted(faces,key=lambda f:f[2]*f[3])
38+
39+
# Pick the last face (because it is the largest face acc to area(f[2]*f[3]))
40+
for face in faces[-1:]:
41+
x,y,w,h = face
42+
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
43+
44+
#Extract (Crop out the required face) : Region of Interest
45+
offset = 10
46+
face_section = frame[y-offset:y+h+offset,x-offset:x+w+offset]
47+
face_section = cv2.resize(face_section,(100,100))
48+
49+
skip += 1
50+
if skip%10==0:
51+
face_data.append(face_section)
52+
print(len(face_data))
53+
54+
55+
cv2.imshow("Frame",frame)
56+
cv2.imshow("Face Section",face_section)
57+
58+
key_pressed = cv2.waitKey(1) & 0xFF
59+
if key_pressed == ord('q'):
60+
break
61+
62+
# Convert our face list array into a numpy array
63+
face_data = np.asarray(face_data)
64+
face_data = face_data.reshape((face_data.shape[0],-1))
65+
print(face_data.shape)
66+
67+
# Save this data into file system
68+
np.save(dataset_path+file_name+'.npy',face_data)
69+
print("Data Successfully save at "+dataset_path+file_name+'.npy')
70+
71+
cap.release()
72+
cv2.destroyAllWindows()

‎Face_Recognition/face_detection.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Read a video Stream from Camera ( Frame by frame )
2+
3+
import cv2
4+
cap = cv2.VideoCapture(0)
5+
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") # classifier
6+
while True:
7+
ret, frame = cap.read() # we read 1 img
8+
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
9+
if ret==False:
10+
continue
11+
# now we read the img
12+
# (gray_frame, scaling factor, no of neighbors)
13+
faces = face_cascade.detectMultiscale(gray_frame,1.3,5) # 1.3 means img will shrink by 30%
14+
cv2.imshow("Video Frame",frame)
15+
for (x,y,w,h) in faces:
16+
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
17+
# (frame , coordinates , opposite coordinate of rect , color , boundries)
18+
key_pressed = cv2.waitKey(1) & 0xFF # wait for user inqut -q, then u will stop the loop
19+
if key_pressed == ord('q'):
20+
break
21+
22+
cap.release()
23+
cv2.destroyAllWindows()
24+
25+
26+
27+
28+
""" Scaling Factor -- parameter specifying how much the image size is reduced at each image scale. Basically the scale factor is used to create your scale pyaramid.
29+
1.05 is a good possible value for this, which means you use a small step for resizing i.e. reduce size
30+
31+
Min Neighh -- Parameter specifying how many neigh each candidate rectangle should have to retain it.
32+
this parameter will affect the quality of the detected faces. Higher value results in less detection but with higher quality. 3~6 is a good value for it. """
33+
34+
''' detectMultiscale() - if the imag has 2 faces then this method going to returns the coordinates, the starting of the face (x1,y1), width, height
35+
then there are multiple faces present then it will return tuple of [(x,y,w,h)] '''
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Read a video Stream from Camera ( Frame by frame )
2+
3+
import cv2
4+
cap = cv2.VideoCapture(0)
5+
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") # classifier
6+
7+
while True:
8+
ret, frame = cap.read()
9+
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
10+
11+
if ret == False:
12+
continue
13+
14+
faces = face_cascade.detectMultiScale(gray_frame,1.3,5)
15+
16+
for (x,y,w,h) in faces:
17+
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
18+
19+
cv2.imshow("Video Frame",frame)
20+
21+
key_pressed = cv2.waitKey(1) & 0xFF
22+
if key_pressed == ord('q'):
23+
break
24+
25+
cap.release()
26+
cv2.destroyAllWindows()

0 commit comments

Comments
(0)

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