|
| 1 | +# General imports |
| 2 | +import glob |
| 3 | +from os.path import basename, expanduser, isdir |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +# ETS imports |
| 9 | +from traits.api import ( |
| 10 | + Bool, Directory, Event, HasStrictTraits, Instance, List, observe, Property |
| 11 | +) |
| 12 | +from traits_futures.api import ( |
| 13 | + CallFuture, submit_call, TraitsExecutor |
| 14 | +) |
| 15 | + |
| 16 | +# Local imports |
| 17 | +from pycasa.model.image_file import ImageFile, SUPPORTED_FORMATS |
| 18 | + |
| 19 | +FILENAME_COL = "filename" |
| 20 | +NUM_FACE_COL = "Num. faces" |
| 21 | + |
| 22 | + |
| 23 | +class ImageFolder(HasStrictTraits): |
| 24 | + """ Model for a folder of images. |
| 25 | + """ |
| 26 | + directory = Directory(expanduser("~")) |
| 27 | + |
| 28 | + images = List(Instance(ImageFile)) |
| 29 | + |
| 30 | + data = Instance(pd.DataFrame) |
| 31 | + |
| 32 | + data_updated = Event |
| 33 | + |
| 34 | + traits_executor = Instance(TraitsExecutor) |
| 35 | + |
| 36 | + future = Instance(CallFuture) |
| 37 | + |
| 38 | + executor_idle = Property(Bool, depends_on="future.done") |
| 39 | + |
| 40 | + def __init__(self, **traits): |
| 41 | + # Don't forget this! |
| 42 | + super(ImageFolder, self).__init__(**traits) |
| 43 | + if not isdir(self.directory): |
| 44 | + msg = f"The provided directory isn't a real directory: " \ |
| 45 | + f"{self.directory}" |
| 46 | + raise ValueError(msg) |
| 47 | + self.data = self._create_metadata_df() |
| 48 | + |
| 49 | + @observe("directory") |
| 50 | + def _update_images(self, event): |
| 51 | + self.images = [ |
| 52 | + ImageFile(filepath=file) |
| 53 | + for fmt in SUPPORTED_FORMATS |
| 54 | + for file in glob.glob(f"{self.directory}/*{fmt}") |
| 55 | + ] |
| 56 | + |
| 57 | + @observe("images.items") |
| 58 | + def _update_metadata(self, event): |
| 59 | + self.data = self._create_metadata_df() |
| 60 | + |
| 61 | + def _create_metadata_df(self): |
| 62 | + if not self.images: |
| 63 | + return pd.DataFrame({FILENAME_COL: [], NUM_FACE_COL: []}) |
| 64 | + return pd.DataFrame([ |
| 65 | + { |
| 66 | + FILENAME_COL: basename(img.filepath), |
| 67 | + NUM_FACE_COL: np.nan, |
| 68 | + **img.metadata |
| 69 | + |
| 70 | + } |
| 71 | + for img in self.images |
| 72 | + ]) |
| 73 | + |
| 74 | + def compute_num_faces_background(self, **kwargs): |
| 75 | + self.future = submit_call( |
| 76 | + self.traits_executor, |
| 77 | + self._compute_num_faces, |
| 78 | + **kwargs |
| 79 | + ) |
| 80 | + |
| 81 | + def _compute_num_faces(self, **kwargs): |
| 82 | + return [ |
| 83 | + len(img_file.detect_faces(**kwargs)) |
| 84 | + for img_file in self.images |
| 85 | + ] |
| 86 | + |
| 87 | + @observe("future:done") |
| 88 | + def _update_data(self, event): |
| 89 | + num_faces_all_images = self.future.result |
| 90 | + for idx, num_faces in enumerate(num_faces_all_images): |
| 91 | + self.data[NUM_FACE_COL].iat[idx] = num_faces |
| 92 | + self.data_updated = True |
| 93 | + |
| 94 | + def _get_executor_idle(self): |
| 95 | + return self.future is None or self.future.done |
0 commit comments