-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_detectmanual.py
38 lines (30 loc) · 1.13 KB
/
face_detectmanual.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
import cv2
import sys
import random
import string
def detect(ImagePath):
# get cascade path
cascPath = 'haarcascade_frontalface_default.xml'
#create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
#read image and convert to grayscale
image = cv2.imread(ImagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
#prints the number of faces in the terminal
print("Found {0} faces!".format(len(faces)))
#draw a different coloured rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (random.randint(0,255), random.randint(0,255), random.randint(0,255)), 2)
cv2.imwrite(ImagePath, image)
filename = str(raw_input('Enter name of the file you want to detect faces in without the extension: '))
extension = str(raw_input('Enter extension of the file (example: jpg, png etc.): '))
name = filename + '.' + extension
detect(name)