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 section context.
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)
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.
Example of termination if no objects were found in the previous modules.
import cv2 def main(context, module_item): if len(context['detectedRectangles']) == 0: context['exit'] = True
To work with the image, you can use the OpenCV library offering a variety of editing methods.
Some errors and text outputs can be seen in the console which can be enabled by the ‘show console’ button.