# Towards Explainable Augmented Intelligence (AI) for Crack Characterization
## 3 The AutoNDE Code for Semi-Automatic Crack Characterization
### Explainable AI  
The AI module of AutoNDE is a decision tree, which selects images that appear to contain defects, characterizes these defects and then groups similar images. Note that by their nature, decision trees produce explainable results: all the reasoning can be traced. The decision tree comprises the following submodules:  
1. _ImageSelection_ submodule selects images containing one or two blobs (bright spots), two small contour selected features, one large feature or maybe, one blob and one small feature. If a blob and a contour selected feature are detected at the same location it is the feature parameters that are used to characterize the potential diffraction spot. If one of the contour selected features is slightly bigger than 7 mm it is still treated as a possible diffraction spot. 2. _DefectCharacterization_ carries out calculations of the extent (notch length in the imaged plane), depth (the smallest of distances between notch tips and specimen surfaces) and orientation (the angle the notch makes in the imaged plane with the mean specimen surface) of the detected planar defect. The calculations are based on parameters of the bounding boxes, which the _FindContour_ OpenCV function draws around the objects or else on parameters of blobs detected by the _DetectBlob_ function. Planar cracks are expected to produce two types of images, specular reflections and TOFD (Time of Diffraction) images, which contain two diffraction spots surrounding notch tips. When the image contains one large feature (interpreted as a specular notch image), the extent is calculated as the longest box side; the depth, as the shortest distance between box vertices and specimen surfaces; and orientation as orientation of box's longest side. For TOFD, the extent is calculated as the largest distance between vertices of their bounding boxes; the depth, as the shortest distance between vertices of these boxes and specimen surfaces; and orientation, as orientation of the line connecting the gravity centers of the boxes. If only two small features are identified the code draws a straight yellow line connecting their gravity centers. 3. _ImageGrouping_ checks whether each selected image appears to be similar to the pivot image in the group \(g=1\), 2,..., \(G\), that is contains a notch with a similar extent E and orientation O at a similar location \(C\) (so that the coordinates of the gravity centers of the notches are similar). The pivot is the image with the smallest \(D_{m}\) in the group. If the image is similar to the pivot it is added to the group; otherwise, it is used as a pivot for the next group. The crack parameters are referred to below as \(v=E\), \(O\), \(C\), respectively. For each group a preliminary report is compiled, describing the weighted averages \(\overline{v}_{g}\). \[\overline{v}_{g}=\frac{\sum_{m=1}^{M_{g}}\widehat{w}_{g,m}v_{g,m}}{\widehat{ M}_{g}},\ \widehat{w}_{g,m}=w_{e,g,m}\cdot w_{o,g,m}\cdot w_{l,g,m}\cdot w_{add},\] (5) \[w_{o,g,m}=w\left(\Delta v_{g,m},\ t_{v}\right).\]Above \(\hat{M}_{g}=\sum\limits_{m=1}^{M_{g}}\hat{w}_{g,m}\) is a modified number of images in group \(g\), with \(M_{g}\)--the number of images in group; \(\Delta v_{g,m}=\left|v_{g,m}-v_{g,0}\right|\) is the deviation of parameter \(v_{g,m}\) in the group g and image \(m\) from the corresponding parameter \(v_{g,0}\) in the pivot image (in case of the gravity center location, this deviation is the distance between centers); \(t_{g}\) is the acceptable threshold for this deviation; and the weighting function, which smoothes transition over this threshold is  
\[w(u,\;U)=\left\{ \begin{array}{c}1,\;\text{if}\;u<U\\ e^{-\left(\frac{u}{U}-1\right)^{2}},\;\text{otherwise},\end{array} \right. \tag{6}\]  
see Figure 6. The following thresholds have been established by trial and error:  
\[t_{e}=t_{l}=2.1\;\text{mm},\;t_{o}=21^{\circ}. \tag{7}\]  
The weight \(w_{add}\) is used to taper off the probability of almost horizontal cracks situated very close to the top surface or backwall.