Skip to content

Commit 14b6ce2

Browse files
Merge pull request #183 from Lashuk1729/main
Added Face Detection Algorithm using Haar Cascade
2 parents 3851362 + c8546ba commit 14b6ce2

File tree

11 files changed

+162
-0
lines changed

11 files changed

+162
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Face Detection
2+
3+
## Aim
4+
5+
The aim of the program given is to detect object of interest(face) in real time and to keep tracking of the same object. This is a simple example of how to detect face in Python.
6+
7+
8+
## Purpose
9+
10+
- The purpose is to Build A Real-Time Face Algorithm used to identify faces in an image or a real time video.
11+
- The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper “Rapid Object Detection using a Boosted Cascade of Simple Features” published in 2001.
12+
13+
14+
## Short description of package/script
15+
16+
- Face Recognition is a technology in computer vision. In Face recognition / detection we locate and visualize the human faces in any digital image.
17+
- The __OpenCV GitHub repository__ has the models stored in XML files, and can be read with the OpenCV methods.
18+
- These include models for face detection, eye detection, upper body and lower body detection, license plate detection etc.
19+
- In this project, I applied face detection to some photos from Harry Potter using OpenCV with Python.
20+
21+
22+
## Workflow of the Project
23+
24+
![Professor Snape](./images/Flow_FaceDetection_OpenCV.jpg)
25+
26+
For the classier, we have used packages contain Haar cascade files. cv2.data.haarcascades
27+
28+
## Detailed explanation of script, if needed
29+
30+
If code is not explainable using comments, use this sections to explain your script
31+
32+
33+
## Setup instructions \& Execution
34+
35+
1. Make sure you have Python 3.7 and a text editor installed.
36+
2. Install the required packages using pip install -r requirements.txt.
37+
3. In the main directory Run `python3 face_detection.py` or `python face_detection.py`.
38+
4. Enter the path of the file.
39+
40+
41+
## Sample Test Cases
42+
43+
Some Results of the program:
44+
45+
![Harry Potter](./images/harry.png)
46+
47+
![Professor Snape](./images/snape.png)
48+
49+
50+
## Author(s)
51+
52+
Kushal Borkar
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import cv2
3+
4+
# cascades: https://github.com/opencv/opencv/tree/master/data/haarcascades
5+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
6+
7+
8+
def read_file(image_path):
9+
if os.path.isfile(image_path):
10+
img = cv2.imread(image_path)
11+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
12+
return True, img, gray
13+
14+
else:
15+
return False, False
16+
17+
18+
while(True):
19+
input_path = str(input("Enter the path of the file: "))
20+
val, img, gray = read_file(input_path)
21+
if val == True:
22+
break
23+
24+
rects = face_cascade.detectMultiScale(gray, scaleFactor=1.05,
25+
minNeighbors=10, minSize=(30, 30),
26+
flags=cv2.CASCADE_SCALE_IMAGE)
27+
28+
29+
# loop over the bounding boxes
30+
for (x, y, w, h) in rects:
31+
# draw the face bounding box on the image
32+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
33+
34+
35+
# show the output image
36+
cv2.imshow("Image", img)
37+
cv2.waitKey(0)
13.6 KB
Loading
945 KB
Loading
198 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv_python==4.5.1.48
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Face Detection with Haar Cascade
2+
3+
---
4+
5+
Face detection is one of the fundamental applications used in face recognition that determines the locations and sizes of human faces in arbitrary (digital) images. It detects only the facial features.
6+
7+
---
8+
9+
### What is Haar Cascade?
10+
11+
It is an Object Detection Algorithm used to identify faces in an image or a real time video. The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper “Rapid Object Detection using a Boosted Cascade of Simple Features” published in 2001.
12+
13+
The __OpenCV GitHub repository__ has the models stored in XML files, and can be read with the OpenCV methods. These include models for face detection, eye detection, upper body and lower body detection, license plate detection etc.
14+
15+
In this project, I applied face detection to some photos from Harry Potter using OpenCV with Python.
16+
17+
---
18+
19+
### Steps
20+
21+
1. Make sure you have Python 3.7 and a text editor installed.
22+
2. Install the required packages using pip install -r requirements.txt.
23+
3. In the main directory Run `python3 face_detection.py` or `python face_detection.py`.
24+
4. Enter the path of the file.
25+
26+
---
27+
28+
### Results
29+
30+
Some Results of the program:
31+
32+
![Harry Potter](./images/harry.png)
33+
34+
![Professor Snape](./images/snape.png)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import cv2
3+
4+
# cascades: https://github.com/opencv/opencv/tree/master/data/haarcascades
5+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
6+
7+
8+
def read_file(image_path):
9+
if os.path.isfile(image_path):
10+
img = cv2.imread(image_path)
11+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
12+
return True, img, gray
13+
14+
else:
15+
return False, False
16+
17+
18+
while(True):
19+
input_path = str(input("Enter the path of the file: "))
20+
val, img, gray = read_file(input_path)
21+
if val == True:
22+
break
23+
24+
rects = face_cascade.detectMultiScale(gray, scaleFactor=1.05,
25+
minNeighbors=10, minSize=(30, 30),
26+
flags=cv2.CASCADE_SCALE_IMAGE)
27+
28+
29+
# loop over the bounding boxes
30+
for (x, y, w, h) in rects:
31+
# draw the face bounding box on the image
32+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
33+
34+
35+
# show the output image
36+
cv2.imshow("Image", img)
37+
cv2.waitKey(0)
945 KB
Loading
198 KB
Loading

0 commit comments

Comments
 (0)