44from PIL .ExifTags import TAGS
55from skimage import data
66from skimage .feature import Cascade
7- import matplotlib .pyplot as plt
87import numpy as np
98
109# ETS imports
11- from traits .api import cached_property , Dict , File , HasStrictTraits , Property
10+ from traits .api import Array , cached_property , Dict , File , HasStrictTraits , \
11+ Property
1212
1313SUPPORTED_FORMATS = [".png" , ".jpg" , ".jpeg" ]
1414
@@ -20,6 +20,8 @@ class ImageFile(HasStrictTraits):
2020
2121 metadata = Property (Dict , depends_on = "filepath" )
2222
23+ data = Property (Array , depends_on = "filepath" )
24+
2325 def to_array (self ):
2426 file_ext = os .path .splitext (self .filepath )[1 ].lower ()
2527 if not self .filepath or file_ext not in SUPPORTED_FORMATS :
@@ -28,6 +30,10 @@ def to_array(self):
2830 with PIL .Image .open (self .filepath ) as img :
2931 return np .asarray (img )
3032
33+ @cached_property
34+ def _get_data (self ):
35+ return self .to_array ()
36+
3137 @cached_property
3238 def _get_metadata (self ):
3339 file_ext = os .path .splitext (self .filepath )[1 ].lower ()
@@ -43,20 +49,15 @@ def _get_metadata(self):
4349 else :
4450 return {}
4551
46- def detect_faces (self ):
52+ def detect_faces (self , scale_factor = 1.2 , step_ratio = 1 , min_size = 60 ,
53+ max_size = 600 ):
4754 """ Detect faces in the image.
4855 """
49- # Load the trained file from the module root.
5056 trained_file = data .lbp_frontal_face_cascade_filename ()
51-
52- # Initialize the detector cascade.
5357 detector = Cascade (trained_file )
54-
55- img = plt .imread (self .filepath )
56-
57- faces = detector .detect_multi_scale (img = img ,
58- scale_factor = 1.2 ,
59- step_ratio = 1 ,
60- min_size = (60 , 60 ),
61- max_size = (600 , 600 ))
58+ faces = detector .detect_multi_scale (img = self .data ,
59+ scale_factor = scale_factor ,
60+ step_ratio = step_ratio ,
61+ min_size = (min_size , min_size ),
62+ max_size = (max_size , max_size ))
6263 return faces
0 commit comments