Skip to content

Commit

Permalink
DOC: Add detailed docstrings for model architect and identification f…
Browse files Browse the repository at this point in the history
…unctions, enhancing documentation clarity
  • Loading branch information
acsenrafilho committed Jan 10, 2025
1 parent 920036d commit 0129375
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 6 deletions.
10 changes: 10 additions & 0 deletions cucaracha/ml_models/model_architect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@


class ModelArchitect(ABC):
"""
Abstract base class for defining model architectures.
Attributes:
modality (str): The modality of the model architecture. Expected values are defined in VALID_MODALITIES.
Methods:
get_model():
Abstract method to be implemented by subclasses to return the model architecture.
__str__():
Returns a string representation of the model architecture, including its modality.
"""
def __init__(self, **kwargs):
self.modality = kwargs.get('modality', None)
# valid_modalities = ['image_classification', 'image_keypoint_detection', 'image_object_detection']
Expand Down
38 changes: 38 additions & 0 deletions cucaracha/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@
def call_cucacha_image_task(
input: np.ndarray, doc_preset: str = 'cnh_cpf_rg', auto_fit: bool = True
):
"""
Processes an input image using a pre-trained model specified by the document preset.
Examples:
>>> import numpy as np
>>> from cucaracha.tasks import call_cucacha_image_task
>>> input_image = np.random.rand(224, 224, 3) # Example input image
>>> label, extra = call_cucacha_image_task(input_image, doc_preset='cnh_cpf_rg', auto_fit=True)
>>> isinstance(label, str)
True
>>> isinstance(extra, dict)
True
Note:
This method is directly oriented to image classification tasks. To see
what the presets availble to be used in this method, check the
`cucaracha.ml_models.CUCARACHA_PRESETS` variable and the list of
`image_classification` keys.
Info:
For the `auto_fit` option, If the input image is not consistent to the
ML model input shape, then the method will fit it before prediction.
If the user does not want this behavior, e.g. one may want to already
provide an input data with the correct shape, then the user should set
`auto_fit` to `False`.
Args:
input (np.ndarray): The image to be used in the ML model.
doc_preset (str, optional): Cucaracha preset to be used. Defaults to 'cnh_cpf_rg'.
auto_fit (bool, optional): Fits the input shape to ML model needs. Defaults to True.
Raises:
FileNotFoundError: If the preset is not located in the cucaracha models
ValueError: Input shape does not match the model input shape. Only raised when `auto_fit` is False.
Returns:
tuple: The predicted label and extra information.
"""
_check_input(input)
_check_doc_preset(doc_preset)

Expand Down
59 changes: 59 additions & 0 deletions cucaracha/tasks/identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,67 @@


def identify_personal_document(input: np.array, auto_fit: bool = True):
"""
Identify the personal document type from an image, seting the document
type based on the Brazilian personal documents such as CNH, CPF and RG.
If the document is not identified as CNH, CPF or RG, the method will return
the string 'others' to exemplify an unrecognized document type.
Note:
This method is not intended to be used for document verification, i.e.
it does not check if the document is valid or not, and also does not
collect any information from the document. It only identifies the type
as CNH, CPF or RG.
Note:
The method assumed that the input image is taken considering the
majority of the image space of being as the document itself. Images
with partial document or with a lot of noise may not be correctly
identified.
Info:
For the `auto_fit` option, If the input image is not consistent to the
ML model input shape, then the method will fit it before prediction.
If the user does not want this behavior, e.g. one may want to already
provide an input data with the correct shape, then the user should set
`auto_fit` to `False`.
Args:
input (np.array): An image representing the personal document.
auto_fit (bool, optional): Fits the input shape to ML model needs. Defaults to True.
Returns:
tuple: The predicted document type and extra information.
"""
return call_cucacha_image_task(input, 'cnh_cpf_rg', auto_fit)


def identify_document_is_signed(input: np.array, auto_fit: bool = True):
"""
Identify if the document is signed or not from an image.
Note:
This method is not intended to be used for document verification, i.e.
it does not check if the document is valid or not, and also does not
collect any information from the document. It only verifies whether
the document presents a signature or not.
Note:
The method assumes that the signature is well seen in the image, i.e.
it should be easily identified by a human eye.
Info:
For the `auto_fit` option, If the input image is not consistent to the
ML model input shape, then the method will fit it before prediction.
If the user does not want this behavior, e.g. one may want to already
provide an input data with the correct shape, then the user should set
`auto_fit` to `False`.
Args:
input (np.array): An image representing the document with or without a signature.
auto_fit (bool, optional): Fits the input shape to ML model needs. Defaults to True.
Returns:
tuple: The predicted document type and extra information.
"""
return call_cucacha_image_task(input, 'doc_is_signed', auto_fit)
2 changes: 1 addition & 1 deletion docs/api/aligment.md → docs/api/tasks/aligment.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ All the methods has a pattern to be an image filter, i.e. all the method must ha

Additional data can be provided, depending on the method, which can be seen in the dedicated documentation.

::: aligment
::: tasks.aligment
1 change: 1 addition & 0 deletions docs/api/tasks/identification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: tasks.identification
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ All the methods has a pattern to be an image filter, i.e. all the method must ha

Additional data can be provided, depending on the method, which can be seen in the dedicated documentation.

::: noise_removal
::: tasks.noise_removal
2 changes: 1 addition & 1 deletion docs/api/threshold.md → docs/api/tasks/threshold.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ must have an image as input and offer at least one image as output.
Additional data can be provided, depending on the method, which can be seen
in the dedicated documentation.

::: threshold
::: tasks.threshold

7 changes: 4 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ nav:
- 'faq.md'
- 'getting_started.md'
- 'api/document.md'
- 'api/threshold.md'
- 'api/aligment.md'
- 'api/noise_removal.md'
- 'api/tasks/identification.md'
- 'api/tasks/threshold.md'
- 'api/tasks/aligment.md'
- 'api/tasks/noise_removal.md'
- 'api/ml_models/image_classification/img_class.md'
- 'api/ml_models/image_segmentation/img_sem_seg.md'
- 'api/ml_trainers/img_trainers.md'
Expand Down

0 comments on commit 0129375

Please sign in to comment.