-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep_training_imgs.py
46 lines (32 loc) · 1.1 KB
/
prep_training_imgs.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
import cv2
import os
#change directory for different expressions
currentDirectory = "D:/Facial Expression Detector/training_images_expressions/surprised/"
classifier = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
images = os.listdir(currentDirectory)
i = 1
for img in images:
image = currentDirectory + img
print(str(i))
img = cv2.imread(image)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = classifier.detectMultiScale(img)
for face in faces:
x, y, w, h = [ v for v in face ]
roi_face = img[y:y+h, x:x+w]
roi_face = cv2.resize(roi_face, (300, 300), interpolation = cv2.INTER_AREA)
file = image.split('/')
file = file[-1]
cv2.imwrite(file, roi_face)
print(file + " Saved")
i += 1
cv2.destroyAllWindows()
images = os.listdir(currentDirectory)
i = 1
for filename in os.listdir(currentDirectory):
dst = str(i) + ".jpg"
src = currentDirectory + filename
dst = currentDirectory + dst
os.rename(src, dst)
i += 1
cv2.destroyAllWindows()