generated from dopt-python/py311
Cython Integration and Test Case Enhancements #1
File diff suppressed because it is too large
Load Diff
@ -35,20 +35,6 @@ def midpoint(
|
|||||||
pt_A: npt.NDArray[np.floating],
|
pt_A: npt.NDArray[np.floating],
|
||||||
pt_B: npt.NDArray[np.floating],
|
pt_B: npt.NDArray[np.floating],
|
||||||
) -> tuple[float, float]:
|
) -> tuple[float, float]:
|
||||||
"""to identify the midpoint of a 2D area
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
pt_A : npt.NDArray[np.floating]
|
|
||||||
tuple of coordinates x, y; shape (2, )
|
|
||||||
pt_B : npt.NDArray[np.floating]
|
|
||||||
tuple of coordinates x, y; shape (2, )
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
tuple[float, float]
|
|
||||||
tuple of midpoint coordinates
|
|
||||||
"""
|
|
||||||
return ((pt_A[0] + pt_B[0]) * 0.5, (pt_A[1] + pt_B[1]) * 0.5)
|
return ((pt_A[0] + pt_B[0]) * 0.5, (pt_A[1] + pt_B[1]) * 0.5)
|
||||||
|
|
||||||
|
|
||||||
@ -57,22 +43,6 @@ def check_box_redundancy(
|
|||||||
box_2: t.Box,
|
box_2: t.Box,
|
||||||
tolerance: float = 5.0,
|
tolerance: float = 5.0,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""to check if bounding box has already been identified and is just a redundant one
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
box_1 : t.Box
|
|
||||||
tuple of box values: ((center_x, center_y), (width, height), angle)
|
|
||||||
box_2 : t.Box
|
|
||||||
tuple of box values: ((center_x, center_y), (width, height), angle)
|
|
||||||
tolerance : float, optional
|
|
||||||
distance threshold for width and height, by default 5.0
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool
|
|
||||||
redundancy evaluation
|
|
||||||
"""
|
|
||||||
# unpack the boxes
|
# unpack the boxes
|
||||||
c1, s1, _ = box_1
|
c1, s1, _ = box_1
|
||||||
c2, s2, _ = box_2
|
c2, s2, _ = box_2
|
||||||
@ -93,32 +63,6 @@ def measure_length(
|
|||||||
pixels_per_metric_X: float,
|
pixels_per_metric_X: float,
|
||||||
pixels_per_metric_Y: float,
|
pixels_per_metric_Y: float,
|
||||||
) -> tuple[t.CsvData, t.SensorImages]:
|
) -> tuple[t.CsvData, t.SensorImages]:
|
||||||
"""detect and measure the size of the electrodes
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
file_path : Path
|
|
||||||
path to file to analyse
|
|
||||||
pixels_per_metric_X : float
|
|
||||||
scaling parameter x dimension, Pixels per micrometer in image
|
|
||||||
pixels_per_metric_Y : float
|
|
||||||
scaling parameter y dimension, Pixels per micrometer in image
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
tuple[t.CsvData, t.SensorImages]
|
|
||||||
t.CsvData: (list) data to save as CSV according to requirements, contains strings and ints
|
|
||||||
t.SensorImages: (TypedDict) contains left and right image corresponding to each sensor
|
|
||||||
|
|
||||||
Raises
|
|
||||||
------
|
|
||||||
errors.ImageNotReadError
|
|
||||||
image was not read successfully
|
|
||||||
errors.ContourCalculationError
|
|
||||||
during contour detection there were several possible error causes
|
|
||||||
errors.InvalidElectrodeCount
|
|
||||||
an invalid number of electrodes were detected
|
|
||||||
"""
|
|
||||||
data_csv: list[str | int] = []
|
data_csv: list[str | int] = []
|
||||||
image = cv2.imread(str(file_path))
|
image = cv2.imread(str(file_path))
|
||||||
if image is None:
|
if image is None:
|
||||||
@ -243,25 +187,6 @@ def infer_image(
|
|||||||
image: npt.NDArray[np.uint8],
|
image: npt.NDArray[np.uint8],
|
||||||
model: Patchcore,
|
model: Patchcore,
|
||||||
) -> t.InferenceResult:
|
) -> t.InferenceResult:
|
||||||
"""evaluate one image
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
image : npt.NDArray[np.uint8]
|
|
||||||
represents image to be checked for anomalies
|
|
||||||
model : Patchcore
|
|
||||||
(loaded PyTorch state dictionary): model for anomaly detection
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
t.InferenceResult
|
|
||||||
contains:
|
|
||||||
img (numpy.ndarray)
|
|
||||||
anomaly_map_resized (numpy.ndarray): heatmap to visualize detected anomalies
|
|
||||||
anomaly_score (float): evaluation metric, in [0, 1] with close to 0 being no
|
|
||||||
anomaly detected
|
|
||||||
anomaly_label (bool): anomaly detected (1) or not (0)
|
|
||||||
"""
|
|
||||||
torch_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
torch_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||||
model.to(torch_device)
|
model.to(torch_device)
|
||||||
|
|
||||||
@ -301,19 +226,6 @@ def anomaly_detection(
|
|||||||
data_csv: t.CsvData,
|
data_csv: t.CsvData,
|
||||||
sensor_images: t.SensorImages,
|
sensor_images: t.SensorImages,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""load the model, call function for anomaly detection and store the results
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
file_path : Path
|
|
||||||
path to file to analyse
|
|
||||||
detection_models : t.DetectionModels
|
|
||||||
collection of model paths for the left and right sensor
|
|
||||||
data_csv : t.CsvData
|
|
||||||
(list) data to save as CSV according to requirements, contains strings and ints
|
|
||||||
sensor_images : t.SensorImages
|
|
||||||
_description_
|
|
||||||
"""
|
|
||||||
file_stem = file_path.stem
|
file_stem = file_path.stem
|
||||||
folder_path = file_path.parent
|
folder_path = file_path.parent
|
||||||
|
|
||||||
|
|||||||
135
src/dopt_sensor_anomalies/detection.pyi
Normal file
135
src/dopt_sensor_anomalies/detection.pyi
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import numpy.typing as npt
|
||||||
|
from anomalib.models import Patchcore
|
||||||
|
|
||||||
|
from dopt_sensor_anomalies import types as t
|
||||||
|
|
||||||
|
def midpoint(
|
||||||
|
pt_A: npt.NDArray[np.floating],
|
||||||
|
pt_B: npt.NDArray[np.floating],
|
||||||
|
) -> tuple[float, float]:
|
||||||
|
"""to identify the midpoint of a 2D area
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
pt_A : npt.NDArray[np.floating]
|
||||||
|
tuple of coordinates x, y; shape (2, )
|
||||||
|
pt_B : npt.NDArray[np.floating]
|
||||||
|
tuple of coordinates x, y; shape (2, )
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
tuple[float, float]
|
||||||
|
tuple of midpoint coordinates
|
||||||
|
"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def check_box_redundancy(
|
||||||
|
box_1: t.Box,
|
||||||
|
box_2: t.Box,
|
||||||
|
tolerance: float = 5.0,
|
||||||
|
) -> bool:
|
||||||
|
"""to check if bounding box has already been identified and is just a redundant one
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
box_1 : t.Box
|
||||||
|
tuple of box values: ((center_x, center_y), (width, height), angle)
|
||||||
|
box_2 : t.Box
|
||||||
|
tuple of box values: ((center_x, center_y), (width, height), angle)
|
||||||
|
tolerance : float, optional
|
||||||
|
distance threshold for width and height, by default 5.0
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
bool
|
||||||
|
redundancy evaluation
|
||||||
|
"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def measure_length(
|
||||||
|
file_path: Path,
|
||||||
|
pixels_per_metric_X: float,
|
||||||
|
pixels_per_metric_Y: float,
|
||||||
|
) -> tuple[t.CsvData, t.SensorImages]:
|
||||||
|
"""detect and measure the size of the electrodes
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
file_path : Path
|
||||||
|
path to file to analyse
|
||||||
|
pixels_per_metric_X : float
|
||||||
|
scaling parameter x dimension, Pixels per micrometer in image
|
||||||
|
pixels_per_metric_Y : float
|
||||||
|
scaling parameter y dimension, Pixels per micrometer in image
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
tuple[t.CsvData, t.SensorImages]
|
||||||
|
t.CsvData: (list) data to save as CSV according to requirements, contains strings and ints
|
||||||
|
t.SensorImages: (TypedDict) contains left and right image corresponding to each sensor
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
errors.ImageNotReadError
|
||||||
|
image was not read successfully
|
||||||
|
errors.ContourCalculationError
|
||||||
|
during contour detection there were several possible error causes
|
||||||
|
errors.InvalidElectrodeCount
|
||||||
|
an invalid number of electrodes were detected
|
||||||
|
"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def infer_image(
|
||||||
|
image: npt.NDArray[np.uint8],
|
||||||
|
model: Patchcore,
|
||||||
|
) -> t.InferenceResult:
|
||||||
|
"""evaluate one image
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
image : npt.NDArray[np.uint8]
|
||||||
|
represents image to be checked for anomalies
|
||||||
|
model : Patchcore
|
||||||
|
(loaded PyTorch state dictionary): model for anomaly detection
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
t.InferenceResult
|
||||||
|
contains:
|
||||||
|
img (numpy.ndarray)
|
||||||
|
anomaly_map_resized (numpy.ndarray): heatmap to visualize detected anomalies
|
||||||
|
anomaly_score (float): evaluation metric, in [0, 1] with close to 0 being no
|
||||||
|
anomaly detected
|
||||||
|
anomaly_label (bool): anomaly detected (1) or not (0)
|
||||||
|
"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def anomaly_detection(
|
||||||
|
file_path: Path,
|
||||||
|
detection_models: t.DetectionModels,
|
||||||
|
data_csv: t.CsvData,
|
||||||
|
sensor_images: t.SensorImages,
|
||||||
|
) -> None:
|
||||||
|
"""load the model, call function for anomaly detection and store the results
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
file_path : Path
|
||||||
|
path to file to analyse
|
||||||
|
detection_models : t.DetectionModels
|
||||||
|
collection of model paths for the left and right sensor
|
||||||
|
data_csv : t.CsvData
|
||||||
|
(list) data to save as CSV according to requirements, contains strings and ints
|
||||||
|
sensor_images : t.SensorImages
|
||||||
|
_description_
|
||||||
|
"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def pipeline(
|
||||||
|
user_file_path: str,
|
||||||
|
pixels_per_metric_X: float,
|
||||||
|
pixels_per_metric_Y: float,
|
||||||
|
) -> None: ...
|
||||||
Loading…
x
Reference in New Issue
Block a user