File: test_opencv.py

package info (click to toggle)
willow 1.11.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,952 kB
  • sloc: xml: 20,346; python: 3,969; makefile: 153; sh: 11
file content (75 lines) | stat: -rw-r--r-- 2,441 bytes parent folder | download | duplicates (2)
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
import unittest

from numpy.testing import assert_allclose

from willow.image import JPEGImageFile
from willow.plugins.opencv import OpenCVColorImage, OpenCVGrayscaleImage
from willow.plugins.pillow import PillowImage


class TestOpenCVOperations(unittest.TestCase):
    def setUp(self):
        with open("tests/images/people.jpg", "rb") as f:
            # Open the image via Pillow
            pillow_image = PillowImage.open(JPEGImageFile(f))
            buffer_rgb = pillow_image.to_buffer_rgb()
            colour_image = OpenCVColorImage.from_buffer_rgb(buffer_rgb)
            self.image = OpenCVGrayscaleImage.from_color(colour_image)

        self.expected_features = [
            [41.0, 206.0],
            [16.0, 201.0],
            [243.0, 208.0],
            [79.0, 130.0],
            [120.0, 24.0],
            [43.0, 119.0],
            [40.0, 165.0],
            [37.0, 14.0],
            [250.0, 59.0],
            [98.0, 6.0],
            [78.0, 61.0],
            [201.0, 93.0],
            [8.0, 114.0],
            [189.0, 142.0],
            [292.0, 188.0],
            [201.0, 199.0],
            [7.0, 154.0],
            [198.0, 247.0],
            [235.0, 55.0],
            [22.0, 36.0],
        ]
        self.expected_faces = [(272, 89, 364, 181), (91, 165, 187, 261)]

    def test_get_size(self):
        width, height = self.image.get_size()
        self.assertEqual(width, 600)
        self.assertEqual(height, 400)

    def test_get_frame_count(self):
        frames = self.image.get_frame_count()
        self.assertEqual(frames, 1)

    def test_has_alpha(self):
        has_alpha = self.image.has_alpha()
        self.assertFalse(has_alpha)

    def test_has_animation(self):
        has_animation = self.image.has_animation()
        self.assertFalse(has_animation)

    def test_detect_features(self):
        features = self.image.detect_features()

        self.assertIsInstance(features, list)
        # There are 20 features in the image
        self.assertEqual(len(features), 20)
        assert_allclose(features, self.expected_features, atol=2)

    def test_detect_faces(self):
        faces = self.image.detect_faces()

        self.assertIsInstance(faces, list)
        # There are two faces in the image
        self.assertEqual(len(faces), 2)
        # Sort out list as there is no preferred order of the faces
        assert_allclose(sorted(faces), sorted(self.expected_faces), atol=2)