-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface_cropper.py
71 lines (53 loc) · 2.24 KB
/
face_cropper.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
import dlib
from PIL import Image
from skimage import io
import matplotlib.pyplot as plt
import os
import pandas as pd
import glob
dataset = "C:\Documents (Austin)\P-ai\Kaggle\SET_00_OUTPUT_TRAIN_SAMPLE_VIDEOS"
def clear_folder():
mydir = "C:\Documents (Austin)\P-ai\Kaggle\cropped-faces"
filelist = [ f for f in os.listdir(mydir) if f.endswith(".jpg") ]
for f in filelist:
os.remove(os.path.join(mydir, f))
def detect_faces(image):
face_detector = dlib.get_frontal_face_detector()
detected_faces = face_detector(image, 1)
face_frames = [(x.left(), x.top(),
x.right(), x.bottom()) for x in detected_faces]
return face_frames
df = pd.DataFrame(columns=['filename', 'faces'])
all_filename = []
all_faces = []
individual_imagesets = os.listdir(dataset)
#print(individual_imagesets)
for filenames in individual_imagesets:
for img in os.listdir(dataset + "\\" + filenames):
if img.endswith(".jpg"):
img_path = dataset + "\\" + filenames + "\\" + img
image = io.imread(img_path)
detected_faces = detect_faces(image)
print(detected_faces)
if len(detected_faces) == 1:
face = Image.fromarray(image).crop(detected_faces[0])
# plt.subplot(1, )
plt.axis('off')
new_path = dataset + "\\" + filenames + "\\" + img
plt.imshow(face)
plt.savefig(new_path, dpi=300, bbox_inches='tight')
else:
os.remove(img_path)
# all_filename.append(img_path)
# all_faces.append(len(detected_faces))
# for n, face_rect in enumerate(detected_faces):
# face = Image.fromarray(image).crop(face_rect)
# plt.subplot(1, len(detected_faces), n+1)
# plt.axis('off')
# new_path = "C:\Documents (Austin)\P-ai\Kaggle\cropped" + str(counter) + "-" + filename
# counter += 1;
# plt.imshow(face)
# plt.savefig(new_path, dpi=300, bbox_inches='tight')
# df['filename'] = all_filename
# df['faces'] = all_faces
# df.to_csv('all-data.csv')