예제 #1
0
def align_2d_3d(points_3d,
                points_image,
                image_shape,
                focal_length=None,
                distortion_coeffs=None):
    try:
        import cv2
    except ImportError:
        raise MenpoMissingDependencyError('opencv3')
    height, width = image_shape
    # Create camera matrix
    focal_length = (max(height, width)
                    if focal_length is None else focal_length)
    c_x = width / 2.0
    c_y = height / 2.0
    camera_matrix = np.array([[focal_length, 0, c_x], [0, focal_length, c_y],
                              [0, 0, 1.0]])

    # If distortion coefficients are None, set them to zero
    if distortion_coeffs is None:
        distortion_coeffs = np.zeros(4)
    # Estimate the camera pose given the 3D sparse pointcloud on the
    # mesh, its 2D projection on the image, the camera matrix and the
    # distortion coefficients
    lm2d = points_image.points[:, ::-1].copy()
    converged, r_vec, t_vec = cv2.solvePnP(points_3d.points, lm2d,
                                           camera_matrix, distortion_coeffs)

    if not converged:
        warnings.warn('cv2.SolvePnP did not converge to a solution')

    # Create rotation and translation transform objects from the vectors
    # acquired at the previous step
    rotation_matrix = cv2.Rodrigues(r_vec)[0]
    r = Rotation(rotation_matrix)
    t = Translation(t_vec.ravel())

    return r, t, focal_length
예제 #2
0
from __future__ import division
from functools import partial
from pathlib import Path

from menpo.base import MenpoMissingDependencyError

try:
    from cyffld2 import (load_model, detect_objects,
                         get_frontal_face_mixture_model)
except ImportError:
    raise MenpoMissingDependencyError('cyffld2')

from menpodetect.detect import detect
from menpodetect.compatibility import STRING_TYPES
from .conversion import pointgraph_from_rect, ensure_channel_axis


class _ffld2_detect(object):
    r"""
    A utility callable that allows the caching of an ffld2 detector.

    This callable is important for presenting the correct parameters to the
    user. It also marshalls the return type of the detector back to
    menpo.shape.PointDirectedGraph.

    Parameters
    ----------
    model : `Path` or `str` or `cyffld2.FFLDMixture`
        Either a path to an `cyffld2.FFLDMixture` or the detector itself.

    Raises
예제 #3
0
from __future__ import division
from functools import partial
from pathlib import Path

from menpo.base import MenpoMissingDependencyError

try:
    import bob.ip.facedetect
    from bob.ip.facedetect.detector.cascade import Cascade
except ImportError:
    raise MenpoMissingDependencyError('bob.ip.facedetect')

from menpodetect.detect import detect
from menpodetect.compatibility import STRING_TYPES
from .conversion import bb_to_pointgraph


class _bob_detect(object):
    r"""
    A utility callable that allows the caching of a bob detector.

    This callable is important for presenting the correct parameters to the
    user. It also marshalls the return type of the detector back to
    `menpo.shape.PointDirectedGraph`.

    Parameters
    ----------
    model : `Path` or `str` or `bob.ip.facedetect.detector.cascade.Cascade`
        Either a path to a `bob.ip.facedetect.detector.cascade.Cascade` or a
        `bob.ip.facedetect.detector.cascade.Cascade` itself.
예제 #4
0
import numpy as np

from menpo.base import MenpoMissingDependencyError, partial_doc
from menpo.image.base import normalize_pixels_range
from ..base import winitfeature

try:
    from cyvlfeat.sift.dsift import dsift as cyvlfeat_dsift
except ImportError as e:
    raise MenpoMissingDependencyError(e)


@winitfeature
def dsift(
    pixels,
    window_step_horizontal=1,
    window_step_vertical=1,
    num_bins_horizontal=2,
    num_bins_vertical=2,
    num_or_bins=9,
    cell_size_horizontal=6,
    cell_size_vertical=6,
    fast=True,
    verbose=False,
):
    r"""
    Computes a 2-dimensional dense SIFT features image with ``C`` number of
    channels, where
    ``C = num_bins_horizontal * num_bins_vertical * num_or_bins``. The dense
    SIFT [2]_ implementation is taken from Vlfeat [1]_.
예제 #5
0
from __future__ import division
from functools import partial
from pathlib import Path

from menpo.base import MenpoMissingDependencyError

try:
    import dlib
except ImportError:
    raise MenpoMissingDependencyError('dlib')

from menpodetect.detect import detect
from menpodetect.compatibility import STRING_TYPES
from .conversion import rect_to_pointgraph


class _dlib_detect(object):
    r"""
    A utility callable that allows the caching of a dlib detector.

    This callable is important for presenting the correct parameters to the
    user. It also marshalls the return type of the detector back to
    `menpo.shape.PointDirectedGraph`.

    Parameters
    ----------
    model : `Path` or `str` or `dlib.simple_object_detector`
        Either a path to a `dlib.simple_object_detector` or a
        `dlib.fhog_object_detector` or the detector itself.

    Raises
from __future__ import division
import numpy as np
from menpo.base import MenpoMissingDependencyError, partial_doc
from menpo.image.base import normalize_pixels_range
from ..base import winitfeature

try:
    from cyvlfeat.sift.dsift import dsift as cyvlfeat_dsift
except ImportError:
    raise MenpoMissingDependencyError('cyvlfeat')


@winitfeature
def dsift(pixels,
          window_step_horizontal=1,
          window_step_vertical=1,
          num_bins_horizontal=2,
          num_bins_vertical=2,
          num_or_bins=9,
          cell_size_horizontal=6,
          cell_size_vertical=6,
          fast=True,
          verbose=False):
    r"""
    Computes a 2-dimensional dense SIFT features image with ``C`` number of
    channels, where
    ``C = num_bins_horizontal * num_bins_vertical * num_or_bins``. The dense
    SIFT [2]_ implementation is taken from Vlfeat [1]_.

    Parameters
    ----------
예제 #7
0
from __future__ import division
from functools import partial
from pathlib import Path

from menpo.base import MenpoMissingDependencyError

try:
    import cv2
except ImportError:
    raise MenpoMissingDependencyError('opencv')

from menpodetect.detect import detect
from menpodetect.compatibility import STRING_TYPES
from .conversion import (pointgraph_from_rect, opencv_frontal_face_path,
                         opencv_profile_face_path, opencv_eye_path)


def _get_default_flags():
    version = cv2.__version__.split('.')[0]
    if version == '2':
        return cv2.cv.CV_HAAR_SCALE_IMAGE
    elif version == '3':
        return cv2.CASCADE_SCALE_IMAGE


class _opencv_detect(object):
    r"""
    A utility callable that allows the caching of an opencv detector.

    This callable is important for presenting the correct parameters to the
    user. It also marshalls the return type of the detector back to
예제 #8
0
from __future__ import division
from functools import partial
from pathlib import Path
import numpy as np

from menpo.base import MenpoMissingDependencyError

try:
    from cypico import detect_objects, detect_frontal_faces
except ImportError:
    raise MenpoMissingDependencyError('cypico')

from menpodetect.detect import detect
from menpodetect.compatibility import STRING_TYPES
from .conversion import pointgraph_from_circle


class _pico_detect(object):
    r"""
    A utility callable that allows the caching of a pico detector.

    This callable is important for presenting the correct parameters to the
    user. It also marshalls the return type of the detector back to
    menpo.shape.PointDirectedGraph.

    Parameters
    ----------
    model : `pico detector`
        At the moment loading new pico detectors is not possible. Unless you
        have managed to load a pico detector as a 1D uint8 array.