File: test_numpy_returns.py

package info (click to toggle)
dlib 19.24%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 284,176 kB
  • sloc: cpp: 316,461; xml: 26,260; python: 1,628; sh: 356; java: 229; makefile: 179; javascript: 73; perl: 18
file content (83 lines) | stat: -rw-r--r-- 2,828 bytes parent folder | download | duplicates (3)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
import pickle

import dlib
import pytest

import utils

# Paths are relative to dlib root
image_path = "examples/faces/Tom_Cruise_avp_2014_4.jpg"
shape_path = "tools/python/test/shape.pkl"
face_chip_path = "tools/python/test/test_face_chip.npy"

def get_test_image_and_shape():
    img = dlib.load_rgb_image(image_path)
    shape = utils.load_pickled_compatible(shape_path)
    return img, shape
 
def get_test_face_chips():
    rgb_img, shape = get_test_image_and_shape()
    shapes = dlib.full_object_detections()
    shapes.append(shape)
    return dlib.get_face_chips(rgb_img, shapes)

def get_test_face_chip():
    rgb_img, shape = get_test_image_and_shape()
    return dlib.get_face_chip(rgb_img, shape)

@pytest.mark.skipif(not utils.is_numpy_installed(), reason="requires numpy")
def test_partition_pixels():
    truth = (102, 159, 181);
    img, shape = get_test_image_and_shape()

    assert(dlib.partition_pixels(img) == truth[0])
    assert(dlib.partition_pixels(img,3) == truth)

    # Call all these versions of this mainly to make sure binding to
    # various image types works.
    assert(dlib.partition_pixels(img[:,:,0]) == 125)
    assert(dlib.partition_pixels(img[:,:,0].astype('float32')) == 125)
    assert(dlib.partition_pixels(img[:,:,0].astype('float64')) == 125)
    assert(dlib.partition_pixels(img[:,:,0].astype('uint16')) == 125)
    assert(dlib.partition_pixels(img[:,:,0].astype('uint32')) == 125)


# The tests below will be skipped if Numpy is not installed
@pytest.mark.skipif(not utils.is_numpy_installed(), reason="requires numpy")
def test_get_face_chip():
    import numpy
    face_chip = get_test_face_chip()
    expected = numpy.load(face_chip_path)
    assert numpy.array_equal(face_chip, expected)

@pytest.mark.skipif(not utils.is_numpy_installed(), reason="requires numpy")
def test_get_face_chips():
    import numpy
    face_chips = get_test_face_chips()
    expected = numpy.load(face_chip_path)
    assert numpy.array_equal(face_chips[0], expected)

@pytest.mark.skipif(not utils.is_numpy_installed(), reason="requires numpy")
def test_regression_issue_1220_get_face_chip():
    """
    Memory leak in Python get_face_chip
    https://github.com/davisking/dlib/issues/1220
    """
    face_chip = get_test_face_chip()
    # we expect two references:
    # 1.) the local variable 
    # 2.) the temporary passed to getrefcount
    assert sys.getrefcount(face_chip) == 2

@pytest.mark.skipif(not utils.is_numpy_installed(), reason="requires numpy")
def test_regression_issue_1220_get_face_chips():
    """
    Memory leak in Python get_face_chip
    https://github.com/davisking/dlib/issues/1220
    """
    face_chips = get_test_face_chips()
    count = sys.getrefcount(face_chips)
    assert count == 2
    count = sys.getrefcount(face_chips[0])
    assert count == 2