Code
Using the Python programming language, the Code module allows you to customize the application context as you choose. In a new project, there are several ready-made sample codes available that work with OpenCV. The context structure is described in Context.
Structure
The main method is ‘main(context, module_item)’. Its role is to edit the context that comes in as a parameter. Additional auxiliary methods can be added to the code. Standard libraries can be imported, typically together with the ‘cv2’ library. In the context, the image is under the ‘image’ attribute and its structure is a numpy array, see OpenCV documentation.
You can use the keyboard shortcut ‘Ctrl+S’ for saving.
Example of image reduction to half of its size
import cv2
def main(context, module_item):
context['image'] = cv2.resize(context['image'], None, fx=0.5, fy=0.5)
Working with heatmaps
If there is a module producing heatmaps (like Anomaly Detector or Surface Detection) in your project, you can access the heatmaps using context['heatmaps']
. It is a list of tuples containing heatmap image in numpy format and class information for each defect class.
To show a heatmap of selected class you can use the following code:
def main(context):
class_index = 0
heatmap, class_info = context['heatmaps'][class_index]
context['image'] = heatmap
context['heatmaps'] = []
Early Flow Termination
If the ‘exit’ attribute is set to ‘True’ in the context, the subsequent modules are skipped. If the code uses parallelism, only the current branch is terminated.
An example of termination is if no objects were found in the previous modules.
import cv2
def main(context, module_item):
if len(context['detectedRectangles']) == 0:
context['exit'] = True
OpenCV
To work with the image, you can use the OpenCV library offering a variety of editing methods.
Console
Some errors and text outputs can be seen in the console which can be enabled by the ‘show console’ button.
Adding new libraries
To add a new Python library you will need to copy the files created by the “pip install” command to the server folder. Default location: “C:\Program Files\PEKAT VISION X.X.X\server”