utils.py 2.6 KB
Newer Older
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
1 2 3
import numpy
from bob.ip.facedetect import BoundingBox
import dlib
4
import bob.io.image
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
5

6

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
7 8 9 10 11 12
def bob_to_dlib_image_convertion(bob_image, change_color=True):
    """
    Bob stores color images as (C, W, H), where C is channels, W is the width and H is height; AND the order of the
    colors are R,G,B.
    On the other hand, Dlib do (W, H, C) AND the order of the colors are the same as OpenCV (B, G, R)
    """
13 14 15 16

    if len(bob_image.shape) == 2:
        return bob_image

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
17 18 19 20 21 22 23 24
    dlib_image = numpy.zeros(shape=(bob_image.shape[1], bob_image.shape[2], bob_image.shape[0]), dtype="uint8")
    if change_color:
        dlib_image[:, :, 0] = bob_image[2, :, :]  # B
        dlib_image[:, :, 1] = bob_image[1, :, :]  # G
        dlib_image[:, :, 2] = bob_image[0, :, :]  # R
    else:
        dlib_image[:, :, 0] = bob_image[0, :, :]  # B
        dlib_image[:, :, 1] = bob_image[1, :, :]  # G
25
        dlib_image[:, :, 2] = bob_image[2, :, :]  # R
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
26 27 28 29

    return dlib_image


30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
def dlib_to_bob_image_convertion(dlib_image, change_color=True):
    """
    Bob stores color images as (C, W, H), where C is channels, W is the width and H is height; AND the order of the
    colors are R,G,B.
    On the other hand, Dlib do (W, H, C) AND the order of the colors are the same as OpenCV (B, G, R)
    """

    if len(dlib_image.shape) == 2:
        return dlib_image

    bob_image = numpy.zeros(shape=(dlib_image.shape[2], dlib_image.shape[0], dlib_image.shape[1]), dtype="uint8")
    if change_color:
        bob_image[0, :, :] = dlib_image[:, :, 2] # R
        bob_image[1, :, :] = dlib_image[:, :, 1] # G
        bob_image[2, :, :] = dlib_image[:, :, 0] # B
    else:
        bob_image[0, :, :] = dlib_image[:, :, 0] # R
        bob_image[1, :, :] = dlib_image[:, :, 1] # G
        bob_image[2, :, :] = dlib_image[:, :, 2] # B

    return bob_image


Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
53 54 55 56 57 58 59 60 61 62
def bounding_box_2_rectangle(bb):
    """
    Converrs a bob.ip.facedetect.BoundingBox to dlib.rectangle
    """

    assert isinstance(bb, BoundingBox)
    return dlib.rectangle(bb.topleft[1], bb.topleft[0],
                          bb.bottomright[1], bb.bottomright[0])


63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
def rectangle_2_bounding_box(rectangle):
    """
    Converts dlib.rectangle to bob.ip.facedetect.BoundingBox 
    """

    assert isinstance(rectangle, dlib.rectangle)

    top  = numpy.max( [0, rectangle.top()] )
    left = numpy.max( [0, rectangle.left()])
    height = numpy.min( [top,  rectangle.height()] )
    width  = numpy.min( [left, rectangle.width() ] )

    return BoundingBox((top, left),
                       (height, width))
    
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
78 79 80 81


# gets sphinx autodoc done right - don't remove it
__all__ = [_ for _ in dir() if not _.startswith('_')]