-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaar_cascade.py
53 lines (40 loc) · 1.59 KB
/
haar_cascade.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
# Haar cascade face extraction
import os
from cv2 import cv2, data
from glob import glob
from util import image_extensions, has_img_extension
imgdir = os.getcwd()
save_dir = 'extracted'
ext = image_extensions # Image formats
def find_images_in_dir(imgdir):
all_files = os.listdir(imgdir)
files = [file for file in all_files if has_img_extension(file)]
return files
files = find_images_in_dir(imgdir)
filenames = [os.path.splitext(filepath) for filepath in files]
images = [cv2.imread(file) for file in files]
figure_size = (160, 160)
def haar_cascade_extraction(images):
for index, image in enumerate(images):
grayscaled_img = cv2.cvtColor(
image, cv2.COLOR_BGR2GRAY) # Convert to grayscale
face_cascade = cv2.CascadeClassifier(
data.haarcascades + "haarcascade_frontalface_alt2.xml")
faces = face_cascade.detectMultiScale(grayscaled_img, 1.1, 4)
for count, (x, y, w, h) in enumerate(faces):
# Draw rectangle around face
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 255, 255))
face = image[y:y + h, x:x + w]
resized = cv2.resize(face, figure_size)
# Counts up if there is more than one face in image
print(f'Cropping and Saving: {filename}')
cv2.imshow('', resized)
cv2.imwrite(filename, resized)
cv2.waitKey(1)
cv2.destroyAllWindows()
if __name__ == '__main__':
folder = 'men./'
current_path = os.getcwd()
if not current_path.endswith(folder):
os.chdir(folder)
haar_cascade_extraction(images)