-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfaceExtraction.py
69 lines (50 loc) · 1.83 KB
/
faceExtraction.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
from matplotlib import pyplot as plt
from mtcnn.mtcnn import MTCNN
from matplotlib.patches import Rectangle
image = plt.imread('face-001.jpg')
detector = MTCNN()
faces = detector.detect_faces(image)
for face in faces:
print(face)
def highlight_faces(image_path, faces):
# display image
image = plt.imread(image_path)
plt.imshow(image)
ax = plt.gca()
# for each face, draw a rectangle based on coordinates
for face in faces:
x, y, width, height = face['box']
face_border = Rectangle((x, y), width, height,
fill=False, color='red')
ax.add_patch(face_border)
plt.savefig('face_detected.jpg')
plt.show()
"""## Highlight faces from an Image"""
highlight_faces('face-001.jpg', faces)
"""## Extract Face from image"""
from numpy import asarray
from PIL import Image
def extract_face_from_image(image_path, required_size=(224, 224)):
# load image and detect faces
image = plt.imread(image_path)
detector = MTCNN()
faces = detector.detect_faces(image)
face_images = []
for face in faces:
# extract the bounding box from the requested face
x1, y1, width, height = face['box']
x2, y2 = x1 + width, y1 + height
# extract the face
face_boundary = image[y1:y2, x1:x2]
# resize pixels to the model size
face_image = Image.fromarray(face_boundary)
face_image = face_image.resize(required_size)
face_array = asarray(face_image)
face_images.append(face_array)
return face_images
extracted_face = extract_face_from_image('face-001.jpg')
# Display the first face from the extracted faces]
plt.subplot(121),plt.imshow(extracted_face[0]),plt.title("first face")
plt.subplot(122),plt.imshow(extracted_face[1]),plt.title("second face")
plt.savefig("extracted face.jpg")
plt.show()