Commit 981c5a06 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Make imports more readable and remove boundingbox to eyes

parent 009e21a8
Pipeline #17623 passed with stage
in 22 minutes and 41 seconds
from import Annotator
from import FaceCrop # import for documentation
import # import for documentation
class Base(Annotator):
class Base(
"""Base class for all face annotators"""
def __init__(self, **kwargs):
......@@ -12,7 +12,8 @@ class Base(Annotator):
"""Annotates an image and returns annotations in a dictionary. All
annotator should return at least the ``topleft`` and ``bottomright``
coordinates. Some currently known annotation points such as ``reye``
and ``leye`` are formalized in :any:`FaceCrop`.
and ``leye`` are formalized in
......@@ -47,7 +47,7 @@ def min_face_size_validator(annotations, min_face_size=(32, 32)):
# These imports should be here to avoid circular dependencies
from .Base import Base
from .bobipfacedetect import BobIpFacedetect, BoundingBoxToEyes
from .bobipfacedetect import BobIpFacedetect
from .bobipflandmark import BobIpFlandmark
from .bobipdlib import BobIpDlib
from .bobipmtcnn import BobIpMTCNN
......@@ -75,7 +75,6 @@ def __appropriate__(*args):
import math
from import HDF5File
from bob.ip.color import rgb_to_gray
from bob.ip.facedetect import (
detect_single_face, Sampler, default_cascade, Cascade,
bounding_box_from_annotation, expected_eye_positions)
import bob.ip.color
import bob.ip.facedetect
from . import Base, bounding_box_to_annotations
class BobIpFacedetect(Base):
"""Annotator using bob.ip.facedetect
Provides topleft and bottomright annoations.
cascade : :any:`bob.ip.facedetect.Cascade`
The file name, where a face detector cascade can be found. If ``None``,
the default cascade for frontal faces
:any:`bob.ip.facedetect.default_cascade` is used.
detection_overlap : float
See :any:`bob.ip.facedetect.detect_single_face`.
distance : int
See the Sampling section in the
:ref:`Users Guide of bob.ip.facedetect <bob.ip.facedetect>`.
scale_base : float
See the Sampling section in the
:ref:`Users Guide of bob.ip.facedetect <bob.ip.facedetect>`.
lowest_scale : float
See the Sampling section in the
:ref:`Users Guide of bob.ip.facedetect <bob.ip.facedetect>`.
eye_estimate : bool
If ``True``, expected eye locations are added to the annotations.
def __init__(self, cascade=None,
detection_overlap=0.2, distance=2,
scale_base=math.pow(2., -1. / 16.), lowest_scale=0.125,
super(BobIpFacedetect, self).__init__(**kwargs)
self.sampler = Sampler(
self.sampler = bob.ip.facedetect.Sampler(
scale_factor=scale_base, lowest_scale=lowest_scale,
if cascade is None:
self.cascade = default_cascade()
self.cascade = bob.ip.facedetect.default_cascade()
self.cascade = Cascade(HDF5File(cascade))
self.cascade = bob.ip.facedetect.Cascade(
self.detection_overlap = detection_overlap
self.eye_estimate = eye_estimate
def annotate(self, image, **kwargs):
"""Return topleft and bottomright and expected eye positions
......@@ -43,47 +69,13 @@ class BobIpFacedetect(Base):
quality, leye, reye.
if image.ndim == 3:
image = rgb_to_gray(image)
bounding_box, quality = detect_single_face(
image = bob.ip.color.rgb_to_gray(image)
bbx, quality = bob.ip.facedetect.detect_single_face(
image, self.cascade, self.sampler, self.detection_overlap)
landmarks = bounding_box_to_annotations(bounding_box)
landmarks['quality'] = quality
return landmarks
class BoundingBoxToEyes(Base):
"""Converts bounding box annotations to eye locations. The bounding box's
annotations is expected to have come from :any:`BobIpFacedetect`.
Example usage:
.. doctest::
>>> from import FailSafe
>>> from import (
... BobIpFacedetect, BoundingBoxToEyes)
>>> annotator = FailSafe(
... [BobIpFacedetect(), BoundingBoxToEyes()],
... required_keys=('reye', 'leye'))
def annotate(self, image, annotations, **kwargs):
"""Converts bounding boxes of bob.ip.facedetect to eye locations.
image : numpy.array
annotations : dict
The annotations that are given by :any:`BobIpFacedetect`.
landmarks = bounding_box_to_annotations(bbx)
landmarks['quality'] = quality
if self.eye_estimate:
The annotations with reye and leye locations added.
bbx = bounding_box_from_annotation(source='direct', **annotations)
return annotations
return landmarks
from import FailSafe
from import BobIpFacedetect, BoundingBoxToEyes
from import BobIpFacedetect
annotator = FailSafe(
[BobIpFacedetect(), BoundingBoxToEyes()],
required_keys=('reye', 'leye'))
annotator = BobIpFacedetect(eye_estimate=True)
......@@ -2,7 +2,7 @@ import
from import (
BobIpFacedetect, BoundingBoxToEyes, BobIpFlandmark,
BobIpFacedetect, BobIpFlandmark,
from import FailSafe
import numpy
......@@ -18,19 +18,12 @@ def _assert_bob_ip_facedetect(annot):
def test_bob_ip_facedetect():
from import BobIpFacedetect
annot = BobIpFacedetect()(face_image)
def test_bob_ip_facedetect_eyes():
annotator = FailSafe(
[BobIpFacedetect(), BoundingBoxToEyes()],
required_keys=('reye', 'leye'),
annot = annotator(face_image)
annot = BobIpFacedetect(eye_estimate=True)(face_image)
assert [int(x) for x in annot['reye']] == [175, 128], annot
assert [int(x) for x in annot['leye']] == [175, 221], annot
......@@ -34,7 +34,6 @@ Face Image Annotators
.. autosummary::
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