Skip to content

Commit bbcb8ef

Browse files
Add button to add ability to detect and draw faces in ImageFile view.
1 parent c6f85fc commit bbcb8ef

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

stage5.2_fuller_application/pycasa/model/image_file.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# ETS imports
1010
from traits.api import Array, cached_property, Dict, File, HasStrictTraits, \
11-
Property
11+
List, Property
1212

1313
SUPPORTED_FORMATS = [".png", ".jpg", ".jpeg"]
1414

@@ -22,6 +22,8 @@ class ImageFile(HasStrictTraits):
2222

2323
data = Property(Array, depends_on="filepath")
2424

25+
faces = List
26+
2527
def to_array(self):
2628
file_ext = os.path.splitext(self.filepath)[1].lower()
2729
if not self.filepath or file_ext not in SUPPORTED_FORMATS:
@@ -60,4 +62,5 @@ def detect_faces(self, scale_factor=1.2, step_ratio=1, min_size=60,
6062
step_ratio=step_ratio,
6163
min_size=(min_size, min_size),
6264
max_size=(max_size, max_size))
65+
self.faces = faces
6366
return faces

stage5.2_fuller_application/pycasa/ui/image_file_view.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# General imports
22
from 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
910
from 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

Comments
 (0)