Skip to content

Commit 809d296

Browse files
committed
Added Face Detection Algorithm using Haar Cascade
1 parent d952044 commit 809d296

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
import numpy as np
3+
import cv2
4+
import matplotlib.pyplot as plt
5+
import matplotlib.patches as mpatches
6+
7+
# cascades: https://github.com/opencv/opencv/tree/master/data/haarcascades
8+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
9+
10+
def read_file(image_path):
11+
if os.path.isfile(image_path):
12+
img = cv2.imread(image_path)
13+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
14+
return True, gray
15+
16+
else:
17+
return False, False
18+
19+
while(1):
20+
input_path = str(input("Enter the path of the file: "))
21+
val, img = read_file(input_path)
22+
if val != False:
23+
break
24+
25+
rects = face_cascade.detectMultiScale(img, scaleFactor=1.05,
26+
minNeighbors=10, minSize=(30, 30),flags=cv2.CASCADE_SCALE_IMAGE)
27+
28+
# loop over the bounding boxes
29+
for (x, y, w, h) in rects:
30+
# draw the face bounding box on the image
31+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
32+
33+
# show the output image
34+
cv2.imshow("Image", img)
35+
cv2.waitKey(0)

0 commit comments

Comments
 (0)