menpodetect 0.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ from menpodetect.dlib import *
2
+ from menpodetect.opencv import *
3
+
4
+ from ._version import __version__
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.7.0'
22
+ __version_tuple__ = version_tuple = (0, 7, 0)
23
+
24
+ __commit_id__ = commit_id = 'g14ce14aff'
menpodetect/detect.py ADDED
@@ -0,0 +1,137 @@
1
+ from __future__ import division
2
+ import numpy as np
3
+ from menpo.transform import UniformScale
4
+
5
+
6
+ def _greyscale(image):
7
+ r"""
8
+ Convert image to greyscale if needed. If the image has more than 3 channels,
9
+ then the average greyscale is taken. A copy of the image as greyscale is
10
+ returned (single channel).
11
+
12
+ Parameters
13
+ ----------
14
+ image : `menpo.image.Image`
15
+ The image to convert.
16
+
17
+ Returns
18
+ -------
19
+ image : `menpo.image.Image`
20
+ A greyscale version of the image.
21
+ """
22
+ if image.n_channels != 1:
23
+ if image.n_channels == 3:
24
+ # Use luminosity for RGB images
25
+ image = image.as_greyscale(mode="luminosity")
26
+ else:
27
+ # Fall back to the average of the channels for other kinds
28
+ # of images
29
+ image = image.as_greyscale(mode="average")
30
+ return image
31
+
32
+
33
+ def menpo_image_to_uint8(image, channels_at_back=True):
34
+ r"""
35
+ Return the given image as a uint8 array. This is a copy of the image.
36
+
37
+ Parameters
38
+ ----------
39
+ image : `menpo.image.Image`
40
+ The image to convert. If already uint8, only the channels will be
41
+ rolled to the last axis.
42
+ channels_at_back : `bool`, optional
43
+ If ``True``, the image channels are placed onto the last axis (the back)
44
+ as is common in many imaging packages. This is contrary to the Menpo
45
+ default where channels are the first axis (at the front).
46
+
47
+ Returns
48
+ -------
49
+ uint8_image : `ndarray`
50
+ `uint8` Numpy array, channels as the back (last) axis if
51
+ ``channels_at_back == True``.
52
+ """
53
+ if channels_at_back:
54
+ uint8_im = image.pixels_with_channels_at_back(out_dtype=np.uint8)
55
+ # Handle the dead axis on greyscale images
56
+ if uint8_im.ndim == 3 and uint8_im.shape[-1] == 1:
57
+ uint8_im = uint8_im[..., 0]
58
+ else:
59
+ from menpo.image.base import denormalize_pixels_range
60
+
61
+ uint8_im = denormalize_pixels_range(image.pixels, np.uint8)
62
+ # Handle the dead axis on greyscale images
63
+ if uint8_im.ndim == 3 and uint8_im.shape[0] == 1:
64
+ uint8_im = uint8_im[0]
65
+ return uint8_im
66
+
67
+
68
+ def detect(
69
+ detector_callable,
70
+ image,
71
+ greyscale=True,
72
+ image_diagonal=None,
73
+ group_prefix="object",
74
+ channels_at_back=True,
75
+ ):
76
+ r"""
77
+ Apply the general detection framework.
78
+
79
+ This involves converting the image to greyscale if necessary, rescaling
80
+ the image to a given diagonal, performing the detection, and attaching
81
+ the scaled landmarks back onto the original image.
82
+
83
+ uint8 images cannot be converted to greyscale by this framework, so must
84
+ already be greyscale or ``greyscale=False``.
85
+
86
+ Parameters
87
+ ----------
88
+ detector_callable : `callable` or `function`
89
+ A callable object that will perform detection given a single parameter,
90
+ a `uint8` numpy array with either no channels, or channels as the
91
+ *last* axis.
92
+ image : `menpo.image.Image`
93
+ A Menpo image to detect. The bounding boxes of the detected objects
94
+ will be attached to this image.
95
+ greyscale : `bool`, optional
96
+ Convert the image to greyscale or not.
97
+ image_diagonal : `int`, optional
98
+ The total size of the diagonal of the image that should be used for
99
+ detection. This is useful for scaling images up and down for detection.
100
+ group_prefix : `str`, optional
101
+ The prefix string to be appended to each each landmark group that is
102
+ stored on the image. Each detection will be stored as group_prefix_#
103
+ where # is a count starting from 0.
104
+ channels_at_back : `bool`, optional
105
+ If ``True``, the image channels are placed onto the last axis (the back)
106
+ as is common in many imaging packages. This is contrary to the Menpo
107
+ default where channels are the first axis (at the front).
108
+
109
+ Returns
110
+ -------
111
+ bounding_boxes : `list` of `menpo.shape.PointDirectedGraph`
112
+ A list of bounding boxes representing the detections found.
113
+ """
114
+ d_image = image
115
+
116
+ if greyscale:
117
+ d_image = _greyscale(d_image)
118
+
119
+ if image_diagonal is not None:
120
+ scale_factor = image_diagonal / image.diagonal()
121
+ d_image = d_image.rescale(scale_factor)
122
+
123
+ pcs = detector_callable(
124
+ menpo_image_to_uint8(d_image, channels_at_back=channels_at_back)
125
+ )
126
+
127
+ if image_diagonal is not None:
128
+ s = UniformScale(1 / scale_factor, n_dims=2)
129
+ pcs = [s.apply(pc) for pc in pcs]
130
+
131
+ padding_magnitude = len(str(len(pcs)))
132
+ for i, pc in enumerate(pcs):
133
+ key = "{prefix}_{num:0{mag}d}".format(
134
+ mag=padding_magnitude, prefix=group_prefix, num=i
135
+ )
136
+ image.landmarks[key] = pc
137
+ return pcs
@@ -0,0 +1,11 @@
1
+ from menpo.base import MenpoMissingDependencyError
2
+
3
+
4
+ try:
5
+ from .detect import load_dlib_frontal_face_detector, DlibDetector
6
+ from .train import train_dlib_detector
7
+ except MenpoMissingDependencyError:
8
+ pass
9
+
10
+ # Remove from scope
11
+ del MenpoMissingDependencyError
@@ -0,0 +1,41 @@
1
+ import dlib
2
+ from menpo.shape import bounding_box
3
+
4
+
5
+ def rect_to_pointgraph(rect):
6
+ r"""
7
+ Convert a dlib.rect to a menpo.shape.PointDirectedGraph.
8
+ This enforces a particular point ordering.
9
+
10
+ Parameters
11
+ ----------
12
+ rect : `dlib.rect`
13
+ The bounding box to convert.
14
+
15
+ Returns
16
+ -------
17
+ bounding_box : `menpo.shape.PointDirectedGraph`
18
+ A menpo PointDirectedGraph giving the bounding box.
19
+ """
20
+ return bounding_box((rect.top(), rect.left()), (rect.bottom(), rect.right()))
21
+
22
+
23
+ def pointgraph_to_rect(pg):
24
+ r"""
25
+ Convert a `menpo.shape.PointCloud` to a `dlib.rect`.
26
+
27
+ Parameters
28
+ ----------
29
+ pg : `menpo.shape.PointDirectedGraph`
30
+ The Menpo PointDirectedGraph to convert into a rect. No check is done
31
+ to see if the PointDirectedGraph actually is a rectangle.
32
+
33
+ Returns
34
+ -------
35
+ bounding_rect : `dlib.rect`
36
+ A dlib Rectangle.
37
+ """
38
+ min_p, max_p = pg.bounds()
39
+ return dlib.rectangle(
40
+ left=int(min_p[1]), top=int(min_p[0]), right=int(max_p[1]), bottom=int(max_p[0])
41
+ )
@@ -0,0 +1,143 @@
1
+ from __future__ import division
2
+ from functools import partial
3
+ from pathlib import Path
4
+
5
+ from menpo.base import MenpoMissingDependencyError
6
+
7
+ try:
8
+ import dlib
9
+ except ImportError:
10
+ raise MenpoMissingDependencyError("dlib")
11
+
12
+ from menpodetect.detect import detect
13
+ from .conversion import rect_to_pointgraph
14
+
15
+
16
+ class _dlib_detect(object):
17
+ r"""
18
+ A utility callable that allows the caching of a dlib detector.
19
+
20
+ This callable is important for presenting the correct parameters to the
21
+ user. It also marshalls the return type of the detector back to
22
+ `menpo.shape.PointDirectedGraph`.
23
+
24
+ Parameters
25
+ ----------
26
+ model : `Path` or `str` or `dlib.simple_object_detector`
27
+ Either a path to a `dlib.simple_object_detector` or a
28
+ `dlib.fhog_object_detector` or the detector itself.
29
+
30
+ Raises
31
+ ------
32
+ ValueError
33
+ If a path was provided and it does not exist.
34
+ """
35
+
36
+ def __init__(self, model):
37
+ if isinstance(model, str) or isinstance(model, Path):
38
+ m_path = Path(model)
39
+ if not Path(m_path).exists():
40
+ raise ValueError("Model {} does not exist.".format(m_path))
41
+ # There are two different kinds of object detector, the
42
+ # simple_object_detector and the fhog_object_detector, but we
43
+ # can't tell which is which from the file name. Therefore, try one
44
+ # and then the other. Unfortunately, it throws a runtime error,
45
+ # which we have to catch.
46
+ try:
47
+ model = dlib.simple_object_detector(str(m_path))
48
+ except RuntimeError:
49
+ model = dlib.fhog_object_detector(str(m_path))
50
+ self._dlib_model = model
51
+
52
+ def __call__(self, uint8_image, n_upscales=0):
53
+ r"""
54
+ Perform a detection using the cached dlib detector.
55
+
56
+ Parameters
57
+ ----------
58
+ uint8_image : `ndarray`
59
+ An RGB (3 Channels) or Greyscale (1 Channel) numpy array of uint8
60
+ n_upscales : `int`, optional
61
+ Number of times to upscale the image when performing the detection,
62
+ may increase the chances of detecting smaller objects.
63
+
64
+ Returns
65
+ ------
66
+ bounding_boxes : `list` of `menpo.shape.PointDirectedGraph`
67
+ The detected objects.
68
+ """
69
+ # Dlib doesn't handle the dead last axis
70
+ if uint8_image.shape[-1] == 1:
71
+ uint8_image = uint8_image[..., 0]
72
+ rects = self._dlib_model(uint8_image, n_upscales)
73
+ return [rect_to_pointgraph(r) for r in rects]
74
+
75
+
76
+ class DlibDetector(object):
77
+ r"""
78
+ A generic dlib detector.
79
+
80
+ Wraps a dlib object detector inside the menpodetect framework and provides
81
+ a clean interface to expose the dlib arguments.
82
+ """
83
+
84
+ def __init__(self, model):
85
+ self._detector = _dlib_detect(model)
86
+
87
+ def __call__(
88
+ self,
89
+ image,
90
+ greyscale=False,
91
+ image_diagonal=None,
92
+ group_prefix="dlib",
93
+ n_upscales=0,
94
+ ):
95
+ r"""
96
+ Perform a detection using the cached dlib detector.
97
+
98
+ The detections will also be attached to the image as landmarks.
99
+
100
+ Parameters
101
+ ----------
102
+ image : `menpo.image.Image`
103
+ A Menpo image to detect. The bounding boxes of the detected objects
104
+ will be attached to this image.
105
+ greyscale : `bool`, optional
106
+ Convert the image to greyscale or not.
107
+ image_diagonal : `int`, optional
108
+ The total size of the diagonal of the image that should be used for
109
+ detection. This is useful for scaling images up and down for
110
+ detection.
111
+ group_prefix : `str`, optional
112
+ The prefix string to be appended to each each landmark group that is
113
+ stored on the image. Each detection will be stored as group_prefix_#
114
+ where # is a count starting from 0.
115
+ n_upscales : `int`, optional
116
+ Number of times to upscale the image when performing the detection,
117
+ may increase the chances of detecting smaller objects.
118
+
119
+ Returns
120
+ ------
121
+ bounding_boxes : `list` of `menpo.shape.PointDirectedGraph`
122
+ The detected objects.
123
+ """
124
+ detect_partial = partial(self._detector, n_upscales=n_upscales)
125
+ return detect(
126
+ detect_partial,
127
+ image,
128
+ greyscale=greyscale,
129
+ image_diagonal=image_diagonal,
130
+ group_prefix=group_prefix,
131
+ )
132
+
133
+
134
+ def load_dlib_frontal_face_detector():
135
+ r"""
136
+ Load the dlib frontal face detector.
137
+
138
+ Returns
139
+ -------
140
+ detector : `DlibDetector`
141
+ The frontal face detector.
142
+ """
143
+ return DlibDetector(dlib.get_frontal_face_detector())
@@ -0,0 +1,91 @@
1
+ from menpo.base import MenpoMissingDependencyError
2
+
3
+ try:
4
+ import dlib
5
+ except ImportError:
6
+ raise MenpoMissingDependencyError("dlib")
7
+
8
+ from menpodetect.detect import menpo_image_to_uint8
9
+ from .conversion import pointgraph_to_rect
10
+
11
+
12
+ def train_dlib_detector(
13
+ images,
14
+ epsilon=0.01,
15
+ add_left_right_image_flips=False,
16
+ verbose_stdout=False,
17
+ C=5,
18
+ detection_window_size=6400,
19
+ num_threads=None,
20
+ ):
21
+ r"""
22
+ Train a dlib detector with the given list of images.
23
+
24
+ This is intended to easily train a list of menpo images that have their
25
+ bounding boxes attached as landmarks. Each landmark group on the image
26
+ will have a tight bounding box extracted from it and then dlib will
27
+ train given these images.
28
+
29
+ Parameters
30
+ ----------
31
+ images : `list` of `menpo.image.Image`
32
+ The set of images to learn the detector from. Must have landmarks
33
+ attached to **every** image, a bounding box will be extracted for each
34
+ landmark group.
35
+ epsilon : `float`, optional
36
+ The stopping epsilon. Smaller values make the trainer's solver more
37
+ accurate but might take longer to train.
38
+ add_left_right_image_flips : `bool`, optional
39
+ If ``True``, assume the objects are left/right symmetric and add in
40
+ left right flips of the training images. This doubles the size of the
41
+ training dataset.
42
+ verbose_stdout : `bool`, optional
43
+ If ``True``, will allow dlib to output its verbose messages. These
44
+ will only be printed to the stdout, so will **not** appear in an IPython
45
+ notebook.
46
+ C : `int`, optional
47
+ C is the usual SVM C regularization parameter. Larger values of C will
48
+ encourage the trainer to fit the data better but might lead to
49
+ overfitting.
50
+ detection_window_size : `int`, optional
51
+ The number of pixels inside the sliding window used. The default
52
+ parameter of ``6400 = 80 * 80`` window size.
53
+ num_threads : `int` > 0 or ``None``
54
+ How many threads to use for training. If ``None``, will query
55
+ multiprocessing for the number of cores.
56
+
57
+ Returns
58
+ -------
59
+ detector : `dlib.simple_object_detector`
60
+ The trained detector. To save this detector, call save on the returned
61
+ object and pass a string path.
62
+
63
+ Examples
64
+ --------
65
+ Training a simple object detector from a list of menpo images and save it
66
+ for later use:
67
+
68
+ >>> images = list(mio.import_images('./images/path'))
69
+ >>> in_memory_detector = train_dlib_detector(images, verbose_stdout=True)
70
+ >>> in_memory_detector.save('in_memory_detector.svm')
71
+ """
72
+ rectangles = [
73
+ [pointgraph_to_rect(lgroup.bounding_box()) for lgroup in im.landmarks.values()]
74
+ for im in images
75
+ ]
76
+ image_pixels = [menpo_image_to_uint8(im) for im in images]
77
+
78
+ if num_threads is None:
79
+ import multiprocessing
80
+
81
+ num_threads = multiprocessing.cpu_count()
82
+
83
+ options = dlib.simple_object_detector_training_options()
84
+ options.epsilon = epsilon
85
+ options.add_left_right_image_flips = add_left_right_image_flips
86
+ options.be_verbose = verbose_stdout
87
+ options.C = C
88
+ options.detection_window_size = detection_window_size
89
+ options.num_threads = num_threads
90
+
91
+ return dlib.train_simple_object_detector(image_pixels, rectangles, options)