Commit 60e00fe3 authored by Olegs NIKISINS's avatar Olegs NIKISINS

Fixed possible negative coordinates in the face bbx, updated the documentation

parent 6c50c4e8
Pipeline #13297 failed with stages
in 4 minutes and 28 seconds
......@@ -8,36 +8,59 @@ import pkg_resources
detector = bob.ip.mtcnn.FaceDetector()
import numpy as np
def test_face_detection():
### Testing multiple detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.mtcnn', 'data/multiple-faces.jpg'))
faces, landmarks = detector.detect_all_faces(color_image)
assert len(faces) == 18
assert len(landmarks) == 18
### Testing multiple detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.mtcnn', 'data/multiple-faces.jpg'))
faces, landmarks = detector.detect_all_faces(color_image)
assert len(faces) == 18
assert len(landmarks) == 18
possible_landmarks = ['reye', 'leye', 'nose', 'mouthleft', 'mouthright']
for p in possible_landmarks:
assert p in landmarks[0]
possible_landmarks = ['reye', 'leye', 'nose', 'mouthleft', 'mouthright']
for p in possible_landmarks:
assert p in landmarks[0]
### Testing single detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.facedetect', 'data/testimage.jpg'))
faces, landmarks = detector.detect_single_face(color_image)
assert isinstance(faces, BoundingBox)
### Testing single detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.facedetect', 'data/testimage.jpg'))
faces, landmarks = detector.detect_single_face(color_image)
assert isinstance(faces, BoundingBox)
### Testing no detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.mtcnn', 'data/jeep.jpg'))
faces, landmarks = detector.detect_single_face(color_image)
assert faces is None
assert landmarks is None
### Testing no detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.mtcnn', 'data/jeep.jpg'))
faces, landmarks = detector.detect_single_face(color_image)
assert faces is None
assert landmarks is None
#=========================================================================
# assert negative coordinates and even arrays:
def test_crop():
image = np.zeros((3, 100, 100))
result = detector.detect_single_face(image)
assert result == (None, None)
image = np.ones((3, 100, 100))
result = detector.detect_single_face(image)
assert result == (None, None)
# test on the actual image:
test_file = pkg_resources.resource_filename('bob.ip.mtcnn', 'data/test_image.hdf5')
f = bob.io.base.HDF5File(test_file) #read only
image = f.read('image') #reads integer
del f
### Testing multiple detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.facedetect', 'data/testimage.jpg'))
face = detector.detect_crop_align(color_image, final_image_size=(224, 224))
assert face.shape == (3, 224, 224)
result = detector.detect_single_face(image)
assert result[0].topleft == (0, 77)
assert result[0].bottomright == (225, 246)
def test_crop():
### Testing multiple detections
color_image = bob.io.base.load(pkg_resources.resource_filename('bob.ip.facedetect', 'data/testimage.jpg'))
face = detector.detect_crop_align(color_image, final_image_size=(224, 224))
assert face.shape == (3, 224, 224)
......@@ -53,13 +53,14 @@ def rectangle2bounding_box2(raw_bounding_boxes):
"""
Converts a bob.ip.facedetect.BoundingBox
"""
def convert_one(bb):
assert len(bb)==5
topleft = (bb[1], bb[0])
# topleft = (bb[1], bb[0])
topleft = (numpy.max([0, bb[1]]), numpy.max([0, bb[0]]))
size = (bb[3] - bb[1], bb[2] - bb[0])
return BoundingBox(topleft, size)
# If it is only one
if raw_bounding_boxes.ndim == 1:
return convert_one(raw_bounding_boxes)
......@@ -67,7 +68,7 @@ def rectangle2bounding_box2(raw_bounding_boxes):
bounding_boxes = []
for b in raw_bounding_boxes:
bounding_boxes.append(convert_one(b))
return bounding_boxes
......
......@@ -95,7 +95,8 @@ The detection of landmarks can be done as the following:
>>> import bob.io.base
>>> import bob.io.base.test_utils
>>> color_image = bob.io.base.load(bob.io.base.test_utils.datafile('testimage.jpg', 'bob.ip.facedetect'))
>>> normimage = bob.ip.mtcnn.FaceDetector().detect_crop_align(color_image, final_image_size=(224, 224))
>>> color_image_norm = bob.ip.mtcnn.FaceDetector().detect_crop(color_image, final_image_size=(224, 224))
>>> color_image_norm_align = bob.ip.mtcnn.FaceDetector().detect_crop_align(color_image, final_image_size=(224, 224))
.. plot:: plot/plot_align_faces.py
......
......@@ -9,17 +9,22 @@ import bob.ip.draw
from bob.ip.facedetect import BoundingBox
color_image = bob.io.base.load(bob.io.base.test_utils.datafile('testimage.jpg', 'bob.ip.facedetect'))
face = bob.ip.mtcnn.FaceDetector().detect_crop_align(color_image, final_image_size=(224, 224))
face_crop = bob.ip.mtcnn.FaceDetector().detect_crop(color_image, final_image_size=(224, 224))
face_crop_aligned = bob.ip.mtcnn.FaceDetector().detect_crop_align(color_image, final_image_size=(224, 224))
ax = pyplot.subplot(1, 2, 1)
ax = pyplot.subplot(1, 3, 1)
ax.set_title("Before normalization")
pyplot.imshow(bob.ip.mtcnn.utils.bob_to_dlib_image_convertion(color_image , change_color=False))
pyplot.axis('off')
ax = pyplot.subplot(1, 2, 2)
ax = pyplot.subplot(1, 3, 2)
ax.set_title("Normalized")
pyplot.imshow(bob.ip.mtcnn.utils.bob_to_dlib_image_convertion(face, change_color=False))
pyplot.imshow(bob.ip.mtcnn.utils.bob_to_dlib_image_convertion(face_crop, change_color=False))
pyplot.axis('off')
ax = pyplot.subplot(1, 3, 3)
ax.set_title("Normalized and aligned")
pyplot.imshow(bob.ip.mtcnn.utils.bob_to_dlib_image_convertion(face_crop_aligned, change_color=False))
pyplot.axis('off')
#pyplot.show()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment