-
Notifications
You must be signed in to change notification settings - Fork 0
/
Face detection with OpenCV.py
103 lines (47 loc) · 2.49 KB
/
Face detection with OpenCV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Import the necessary libraries
import numpy as np
import cv2
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
# Loading the image to be tested
test_image = cv2.imread('baby1.jpg')
# Converting to grayscale as opencv expects detector takes in input gray scale images
test_image_gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
# Displaying grayscale image
plt.imshow(test_image_gray, cmap='gray')
# Since we know that OpenCV loads an image in BGR format so we need to convert it into RBG format to be able to display its true colours. Let us write a small function for that.
def convertToRGB(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# # Haar cascade files
# Loading the classifier for frontal face
haar_cascade_face = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_alt2.xml')
# # Face detection
faces_rects = haar_cascade_face.detectMultiScale(test_image_gray, scaleFactor = 1.2, minNeighbors = 5);
# Let us print the no. of faces found
print('Faces found: ', len(faces_rects))
# Our next step is to loop over all the co-ordinates it returned and draw rectangles around them using Open CV.We will be drawing a green rectangle with thicknessof 2
for (x,y,w,h) in faces_rects:
cv2.rectangle(test_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Finally, we shall display the original image in coloured to see if the face has been detected correctly or not.
#convert image to RGB and show image
plt.imshow(convertToRGB(test_image))
# Let us create a generalised function for the entire face detection process.
def detect_faces(cascade, test_image, scaleFactor = 1.1):
# create a copy of the image to prevent any changes to the original one.
image_copy = test_image.copy()
#convert the test image to gray scale as opencv face detector expects gray images
gray_image = cv2.cvtColor(image_copy, cv2.COLOR_BGR2GRAY)
# Applying the haar classifier to detect faces
faces_rect = cascade.detectMultiScale(gray_image, scaleFactor=scaleFactor, minNeighbors = 5)
for (x, y, w, h) in faces_rect:
cv2.rectangle(image_copy, (x, y), (x+w, y+h), (0, 255, 0), 15)
return image_copy
# Testing the function on new image
#loading image
test_image2 = cv2.imread('group.jpg')
#call the function to detect faces
faces = detect_faces(haar_cascade_face, test_image2)
#convert to RGB and display image
plt.imshow(convertToRGB(faces))
# Saving the final image
cv2.imwrite('image1.png',faces)