diff --git a/src/dopt-sensor-anomalies/__init__.py b/src/dopt_sensor_anomalies/__init__.py similarity index 100% rename from src/dopt-sensor-anomalies/__init__.py rename to src/dopt_sensor_anomalies/__init__.py diff --git a/src/dopt_sensor_anomalies/constants.py b/src/dopt_sensor_anomalies/constants.py new file mode 100644 index 0000000..fe344eb --- /dev/null +++ b/src/dopt_sensor_anomalies/constants.py @@ -0,0 +1,10 @@ +from typing import Final + +THRESHOLD_BW: Final[int] = 63 # threshold to distringuish black (electrodes) and white areas +# model_path = [ +# r"C:\Users\demon\Documents\EKF\Modelle\patchcore_model_links.pth", +# r"C:\Users\demon\Documents\EKF\Modelle\patchcore_model_rechts.pth", +# ] # path to anomaly detection models +BACKBONE: Final[str] = "resnet18" # parameters for AI model +LAYERS: Final[tuple[str, str]] = ("layer1", "layer2") +RATIO: Final[float] = 0.05 diff --git a/src/dopt-sensor-anomalies/detection.py b/src/dopt_sensor_anomalies/detection.py similarity index 97% rename from src/dopt-sensor-anomalies/detection.py rename to src/dopt_sensor_anomalies/detection.py index 51a3e61..3f4ded9 100644 --- a/src/dopt-sensor-anomalies/detection.py +++ b/src/dopt_sensor_anomalies/detection.py @@ -19,21 +19,20 @@ from scipy.spatial import distance as dist from torch import as_tensor, cuda, device, float32, load, no_grad from torchvision.transforms.v2.functional import to_dtype, to_image -# input parameters +from dopt_sensor_anomalies import constants as const + +# input parameters: user-defined file_path = r"C:\Users\demon\Documents\EKF\Analyse_fuer_Florian\bild2.bmp" pixelsPerMetricX = 0.251 pixelsPerMetricY = 0.251 -# internal parameters -schwellwert = 63 # threshold to distringuish black (electrodes) and white areas +# internal parameters - configuration +schwellwert = 63 # threshold to distinguish black (electrodes) and white areas model_path = [ r"C:\Users\demon\Documents\EKF\Modelle\patchcore_model_links.pth", r"C:\Users\demon\Documents\EKF\Modelle\patchcore_model_rechts.pth", ] # path to anomaly detection models -backbone = "resnet18" # parameters for AI model -layers = ["layer1", "layer2"] -ratio = 0.05 # measuring @@ -114,7 +113,7 @@ def measure_length(file_path, pixelsPerMetricX, pixelsPerMetricY): # change colours in the image to black and white gray = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY) - _, binary = cv2.threshold(gray, schwellwert, 255, cv2.THRESH_BINARY) + _, binary = cv2.threshold(gray, const.THRESHOLD_BW, 255, cv2.THRESH_BINARY) # perform edge detection, identify rectangular shapes kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) @@ -350,7 +349,9 @@ def anomaly_detection(file_path, data_csv, sensor_images): folder_path = path.dirname(file_path) # reconstruct the model and initialize the engine - model = Patchcore(backbone=backbone, layers=layers, coreset_sampling_ratio=ratio) + model = Patchcore( + backbone=const.BACKBONE, layers=const.LAYERS, coreset_sampling_ratio=const.RATIO + ) engine = Engine() # preparation for plot