-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Object detect module #1570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object detect module #1570
Changes from all commits
be6ae17
0b42e48
bdd624e
a301eff
375db34
432fe35
ebc7324
b4abe6f
5efec34
531aba4
e917979
06fec5c
b20c9ef
29bc49c
d0211ef
ab395b9
815e771
88e65fa
7c99d90
9c1ba8b
342ad78
c90e896
70714d9
79370ce
e92d9f4
46cafe9
211a591
5b979f3
d70c280
20bc829
b8be66b
063e4a1
9aa3348
3b5a4dd
51ef679
bb57c4e
631f03b
4921994
462c17f
ee9c8aa
b9a92cc
e806d4d
8e839ae
3fee96d
36ba7ae
5f250ca
820abe6
a7a3c30
eeab27c
2e6d39f
7face0e
059dfc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| *.pyd | ||
| *.bak | ||
| *.c | ||
| *.cpp | ||
| *.new | ||
| *.md5 | ||
| *.old | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """ | ||
| ============== | ||
| Face Detection | ||
| ============== | ||
|
|
||
| This example shows how to detect faces on an image using object detection framework. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the object detection framework I can do one pass of language checking on the text here before we merge and file a PR. |
||
|
|
||
| First, you will need an xml file, from which the trained data can be read. | ||
| The framework works with files, trained using Multi-block Local Binary Patterns | ||
| Features (See `MB-LBP <plot_multiblock_local_binary_pattern.html>`_) and | ||
| Gentle Adaboost with attentional cascade. So, the detection framework will also | ||
| work with `xml files from OpenCV <https://github.com/Itseez/opencv/tree/master/data/lbpcascades>`_. | ||
| There you can find files that were trained to detect cat faces, profile faces and other things. | ||
| But if you want to detect frontal faces, the respective file is already included in | ||
| scikit-image. | ||
|
|
||
| Next you will have to specify the parameters for the `detect_multi_scale` function. | ||
| Here you can find the meaning of each of them. | ||
|
|
||
| First one is `scale_ratio`. To find all faces, the algorithm does the search on | ||
| multiple scales. This is done by changing the size of searching window. The smallest | ||
| window size is the size of window that was used in training. This size is specified | ||
| in the xml file with trained parameters. The `scale_ratio` parameter specifies by which | ||
| ratio the search window is increased on each step. If you increase this parameter, the | ||
| search time decreases and the accuracy decreases. So, faces on some scales can be not detected. | ||
|
|
||
| `step_ratio` specifies the step of sliding window that is used to search for faces on | ||
| each scale of the image. If this parameter is equal to one, then all the | ||
| possible locations are searched. If the parameter is greater than one, for example, two, | ||
| the window will be moved by two pixels and not all of the possible locations will be searched | ||
| for faces. By increasing this parameter we can reduce the working | ||
| time of the algorithm, but the accuracy will also be decreased. | ||
|
|
||
| `min_size` is the minimum size of search window during the scale search. `max_size` specifies | ||
| the maximum size of the window. If you know the size of faces on the images that you | ||
| want to search, you should specify these parameters as precisely as possible, because you | ||
| can avoid doing expensive computations and possibly decrease the amount | ||
| of false detections. You can save a lot of time by increasing the `min_size` | ||
| parameter, because the majority of time is spent on searching on the smallest scales. | ||
|
|
||
| `min_neighbour_number` and `intersection_score_threshold` parameters are made to | ||
| cluster the excessive detections of the same face and to filter out false detections. | ||
| True faces usually has a lot of dectections around them and false ones usually have | ||
| single detection. First algorithm searches for clusters: two rectangle detections | ||
| are placed in the same cluster if the intersection score between them | ||
| is larger then `intersection_score_threshold`. The intersection score is computed | ||
| using the equation (intersection area) / (small rectangle ratio). The described intersection | ||
| criteria was chosen over intersection over union to avoid a corner case when | ||
| small rectangle inside of a big one have small intersection score. Then each cluster | ||
| is thresholded using `min_neighbour_number` parameter which leaves the clusters | ||
| that have a same or bigger number of detections in them. | ||
|
|
||
| You should also take into account that false detections are inevitable and if you want | ||
| to have a really precise detector, you will have to train it yourself using | ||
| `OpenCV train cascade utility <http://docs.opencv.org/doc/user_guide/ug_traincascade.html>`_. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update us about the latest state of the training in scikit-image?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stefanv it's about to be finished. I wrote a blog post where I wrote about the |
||
| """ | ||
|
|
||
| import skimage.data as data | ||
| import skimage.future.detect as detect | ||
|
|
||
| from matplotlib import pyplot as plt | ||
| from matplotlib import patches | ||
|
|
||
| # Load the trained file from the module root. | ||
| trained_file = data.detect.frontal_face_cascade_xml() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd remove the trailing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stefanv will do. Also seems better for me. |
||
|
|
||
| # Initialize the detector cascade. | ||
| detector = detect.Cascade(trained_file) | ||
|
|
||
| img = data.astronaut() | ||
|
|
||
| detected = detector.detect_multi_scale(img=img, | ||
| scale_factor=1.2, | ||
| step_ratio=1, | ||
| min_size=(60, 60), | ||
| max_size=(123, 123)) | ||
|
|
||
| plt.imshow(img) | ||
| img_desc = plt.gca() | ||
| plt.set_cmap('gray') | ||
|
|
||
| for patch in detected: | ||
|
|
||
| img_desc.add_patch( | ||
| patches.Rectangle( | ||
| (patch['c'], patch['r']), | ||
| patch['width'], | ||
| patch['height'], | ||
| fill=False, | ||
| color='r', | ||
| linewidth=2 | ||
| ) | ||
| ) | ||
|
|
||
| plt.show() | ||
|
|
||
| """ | ||
| .. image:: PLOT2RST.current_figure | ||
| """ | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import os as _os | ||
| from .. import data_dir | ||
|
|
||
|
|
||
| def frontal_face_cascade_xml(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use this function to parse the XML as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stefanv I made it inside the original function because it takes time to load |
||
| """ | ||
| Returns the file's path to the trained xml file for frontal face detection | ||
| that was taken from OpenCV repository [1]_. | ||
|
|
||
| References | ||
| ---------- | ||
| .. [1] OpenCV lbpcascade trained files | ||
| https://github.com/Itseez/opencv/tree/master/data/lbpcascades | ||
| """ | ||
|
|
||
| return _os.path.join(data_dir, 'lbpcascade_frontalface_opencv.xml') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefanv Your thoughts on the module name ?