|
5 | 5 | - Extractors: |
6 | 6 | - BestFramesExtractor: For extracting best frames from all videos from any directory. |
7 | 7 | - TopImagesExtractor: For extracting images with top percent evaluating from any directory. |
| 8 | +LICENSE |
| 9 | +======= |
| 10 | +Copyright (C) 2024 Bartłomiej Flis |
| 11 | +
|
| 12 | +This program is free software: you can redistribute it and/or modify |
| 13 | +it under the terms of the GNU General Public License as published by |
| 14 | +the Free Software Foundation, either version 3 of the License, or |
| 15 | +(at your option) any later version. |
| 16 | +
|
| 17 | +This program is distributed in the hope that it will be useful, |
| 18 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +GNU General Public License for more details. |
| 21 | +
|
| 22 | +You should have received a copy of the GNU General Public License |
| 23 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
8 | 24 | """ |
9 | 25 | from concurrent.futures import ThreadPoolExecutor |
10 | 26 | from pathlib import Path |
@@ -88,17 +104,17 @@ def _list_input_directory_files(self, extensions: tuple[str], |
88 | 104 | logger.debug("Listed file paths: %s", files) |
89 | 105 | return files |
90 | 106 |
|
91 | | - def _evaluate_images(self, images: list[np.ndarray]) -> np.array: |
| 107 | + def _evaluate_images(self, normalized_images: np.ndarray) -> np.array: |
92 | 108 | """ |
93 | 109 | Rating all images in provided images batch using already initialized image evaluator. |
94 | 110 |
|
95 | 111 | Args: |
96 | | - images (list[np.ndarray]): List of images in numpy ndarrays. |
| 112 | + normalized_images (list[np.ndarray]): Already normalized images np.ndarray for evaluating. |
97 | 113 |
|
98 | 114 | Returns: |
99 | 115 | np.array: Array with images scores in given images order. |
100 | 116 | """ |
101 | | - scores = np.array(self._image_evaluator.evaluate_images(images)) |
| 117 | + scores = np.array(self._image_evaluator.evaluate_images(normalized_images)) |
102 | 118 | return scores |
103 | 119 |
|
104 | 120 | @staticmethod |
@@ -139,6 +155,21 @@ def _save_images(self, images: list[np.ndarray]) -> None: |
139 | 155 | for future in futures: |
140 | 156 | future.result() |
141 | 157 |
|
| 158 | + @staticmethod |
| 159 | + def _normalize_images(images: list[np.ndarray], target_size: tuple[int, int]) -> np.ndarray: |
| 160 | + """ |
| 161 | + Normalize all images in given list to target size for further operations. |
| 162 | +
|
| 163 | + Args: |
| 164 | + images (list[np.ndarray]): List of np.ndarray images to normalize. |
| 165 | + target_size (tuple[int, int]): Images will be normalized to this size. |
| 166 | +
|
| 167 | + Returns: |
| 168 | + np.ndarray: All images as a one numpy array. |
| 169 | + """ |
| 170 | + normalized_images = OpenCVImage.normalize_images(images, target_size) |
| 171 | + return normalized_images |
| 172 | + |
142 | 173 | @staticmethod |
143 | 174 | def _add_prefix(prefix: str, path: Path) -> Path: |
144 | 175 | """ |
@@ -169,7 +200,7 @@ def _signal_readiness_for_shutdown() -> None: |
169 | 200 | class ExtractorFactory: |
170 | 201 | """Extractor factory for getting extractors class by their names.""" |
171 | 202 | @staticmethod |
172 | | - def get_extractor(extractor_name: str) -> Type[Extractor]: |
| 203 | + def create_extractor(extractor_name: str) -> Type[Extractor]: |
173 | 204 | """ |
174 | 205 | Match extractor class by its name and return its class. |
175 | 206 |
|
@@ -221,12 +252,16 @@ def _extract_best_frames(self, video_path: Path) -> list[np.ndarray]: |
221 | 252 | list[np.ndarray]: List of best images(frames) from the given video. |
222 | 253 | """ |
223 | 254 | best_frames = [] |
224 | | - frames_batch_generator = OpenCVVideo.get_next_video_frames(video_path, self._config.batch_size) |
| 255 | + frames_batch_generator = OpenCVVideo.get_next_frames(video_path, self._config.batch_size) |
225 | 256 | for frames in frames_batch_generator: |
226 | 257 | if not frames: |
227 | 258 | continue |
228 | | - logger.debug("Frames pack generated.") |
229 | | - scores = self._evaluate_images(frames) |
| 259 | + logger.debug("Frames batch generated.") |
| 260 | + if self._config.all_frames: |
| 261 | + best_frames.extend(frames) |
| 262 | + continue |
| 263 | + normalized_images = self._normalize_images(frames, self._config.target_image_size) |
| 264 | + scores = self._evaluate_images(normalized_images) |
230 | 265 | selected_frames = self._get_best_frames(frames, scores, |
231 | 266 | self._config.compering_group_size) |
232 | 267 | best_frames.extend(selected_frames) |
@@ -268,7 +303,8 @@ def process(self) -> None: |
268 | 303 | for batch_index in range(0, len(images_paths), self._config.batch_size): |
269 | 304 | batch = images_paths[batch_index:batch_index + self._config.batch_size] |
270 | 305 | images = self._read_images(batch) |
271 | | - scores = self._evaluate_images(images) |
| 306 | + normalized_images = self._normalize_images(images, self._config.target_image_size) |
| 307 | + scores = self._evaluate_images(normalized_images) |
272 | 308 | top_images = self._get_top_percent_images(images, scores, |
273 | 309 | self._config.top_images_percent) |
274 | 310 | self._save_images(top_images) |
|
0 commit comments