-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaces.py
More file actions
58 lines (55 loc) · 2.36 KB
/
Copy pathfaces.py
File metadata and controls
58 lines (55 loc) · 2.36 KB
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
import cv2
import os
def find_faces(folder, target, algorithm):
alg = algorithm
haar_cascade = cv2.CascadeClassifier(alg)
for file in os.listdir(folder):
# Check if the file is an image
if file.endswith(('.jpg', '.jpeg', '.png')):
# create the image path
image_path = os.path.join(folder, file)
# reading the image
img = cv2.imread(image_path, 0)
# creating a black and white version of the image
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# detecting the faces
faces = haar_cascade.detectMultiScale(
gray_img, scaleFactor=1.05, minNeighbors=4, minSize=(100, 100)
)
# Now we store the faces
i = 0
for x, y, w, h in faces:
# crop the image to select only the face
cropped_image = img[y : y + h, x : x + w]
# loading the target image path into target variable
target_file_name = (f'{target}/' + str(os.path.splitext(file)[0]) + '_' + str(i) + '.jpg')
cv2.imwrite(
target_file_name,
cropped_image,
)
i += 1
def reference_face(folder, target, algorithm):
alg = algorithm
haar_cascade = cv2.CascadeClassifier(alg)
for file in os.listdir(folder):
# Check if the file is an image
if file.endswith(('.jpg', '.jpeg', '.png')):
# create the image path
image_path = os.path.join(folder, file)
# reading the image
img = cv2.imread(image_path, 0)
# creating a black and white version of the image
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# detecting the faces
faces = haar_cascade.detectMultiScale(
gray_img, scaleFactor=1.05, minNeighbors=0, minSize=(100, 100)
)
for x, y, w, h in faces:
# crop the image to select only the face
cropped_image = img[y : y + h, x : x + w]
# loading the target image path into target_file_name variable
target_file_name = (f'{target}/' + str(os.path.splitext(file)[0]) + '.jpg')
cv2.imwrite(
target_file_name,
cropped_image,
)