|
| 1 | +""" |
| 2 | +This module provides dependency management for extractors using FastAPI's dependency injection. |
| 3 | +LICENSE |
| 4 | +======= |
| 5 | +Copyright (C) 2024 Bartłomiej Flis |
| 6 | +
|
| 7 | +This program is free software: you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation, either version 3 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +This program is distributed in the hope that it will be useful, |
| 13 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | +""" |
| 20 | +from dataclasses import dataclass |
| 21 | +from typing import Type |
| 22 | + |
| 23 | +from fastapi import Depends |
| 24 | + |
| 25 | +from .image_evaluators import InceptionResNetNIMA |
| 26 | +from .image_processors import OpenCVImage |
| 27 | +from .video_processors import OpenCVVideo |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class ExtractorDependencies: |
| 32 | + """ |
| 33 | + Data class to hold dependencies for the extractor. |
| 34 | +
|
| 35 | + Attributes: |
| 36 | + image_processor (Type[OpenCVImage]): Processor for image processing. |
| 37 | + video_processor (Type[OpenCVVideo]): Processor for video processing. |
| 38 | + evaluator (Type[InceptionResNetNIMA]): Evaluator for image quality. |
| 39 | + """ |
| 40 | + image_processor: Type[OpenCVImage] |
| 41 | + video_processor: Type[OpenCVVideo] |
| 42 | + evaluator: Type[InceptionResNetNIMA] |
| 43 | + |
| 44 | + |
| 45 | +def get_image_processor() -> Type[OpenCVImage]: |
| 46 | + """ |
| 47 | + Provides the image processor dependency. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + Type[OpenCVImage]: The image processor class. |
| 51 | + """ |
| 52 | + return OpenCVImage |
| 53 | + |
| 54 | + |
| 55 | +def get_video_processor() -> Type[OpenCVVideo]: |
| 56 | + """ |
| 57 | + Provides the video processor dependency. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Type[OpenCVVideo]: The video processor class. |
| 61 | + """ |
| 62 | + return OpenCVVideo |
| 63 | + |
| 64 | + |
| 65 | +def get_evaluator() -> Type[InceptionResNetNIMA]: |
| 66 | + """ |
| 67 | + Provides the image evaluator dependency. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + Type[InceptionResNetNIMA]: The image evaluator class. |
| 71 | + """ |
| 72 | + return InceptionResNetNIMA |
| 73 | + |
| 74 | + |
| 75 | +def get_extractor_dependencies( |
| 76 | + image_processor=Depends(get_image_processor), |
| 77 | + video_processor=Depends(get_video_processor), |
| 78 | + evaluator=Depends(get_evaluator) |
| 79 | +) -> ExtractorDependencies: |
| 80 | + """ |
| 81 | + Provides the dependencies required for the extractor. |
| 82 | +
|
| 83 | + Args: |
| 84 | + image_processor (Type[OpenCVImage], optional): Dependency injection for image processor. |
| 85 | + video_processor (Type[OpenCVVideo], optional): Dependency injection for video processor. |
| 86 | + evaluator (Type[InceptionResNetNIMA], optional): Dependency injection for image evaluator. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + ExtractorDependencies: All necessary dependencies for the extractor. |
| 90 | + """ |
| 91 | + return ExtractorDependencies( |
| 92 | + image_processor=image_processor, |
| 93 | + video_processor=video_processor, |
| 94 | + evaluator=evaluator |
| 95 | + ) |
0 commit comments