11# General imports
22from matplotlib .figure import Figure
3+ from matplotlib import patches
34
45# ETS imports
5- from traits .api import Instance , observe
6- from traitsui .api import Item , ModelView , View
6+ from traits .api import Button , Instance , observe
7+ from traitsui .api import HGroup , Item , ModelView , Spring , View
78
89# Local imports
910from ets_tutorial .util .mpl_figure_editor import MplFigureEditor
@@ -17,9 +18,16 @@ class ImageFileView(ModelView):
1718
1819 figure = Instance (Figure )
1920
21+ detect_button = Button ("Detect faces" )
22+
2023 view = View (
2124 Item ("model.filepath" , style = "readonly" , show_label = False ),
22- Item ("figure" , editor = MplFigureEditor (), show_label = False )
25+ Item ("figure" , editor = MplFigureEditor (), show_label = False ),
26+ HGroup (
27+ Spring (),
28+ Item ("detect_button" , show_label = False ),
29+ Spring (),
30+ ),
2331 )
2432
2533 @observe ("model.filepath" )
@@ -28,3 +36,21 @@ def build_mpl_figure(self, event):
2836 axes = figure .add_subplot (111 )
2937 axes .imshow (self .model .to_array ())
3038 self .figure = figure
39+
40+ def _detect_button_fired (self ):
41+ self .model .detect_faces ()
42+
43+ @observe ("model.faces" )
44+ def update_mpl_figure_with_faces (self , events ):
45+ axes = self .figure .get_axes ()[0 ]
46+ for face in self .model .faces :
47+ axes .add_patch (
48+ patches .Rectangle (
49+ (face ['c' ], face ['r' ]),
50+ face ['width' ],
51+ face ['height' ],
52+ fill = False ,
53+ color = 'r' ,
54+ linewidth = 2
55+ )
56+ )
0 commit comments