Usage Examples

This page shows several examples of how to use the current code module. The examples are structured as a copy-and-paste code that works without any knowledge of programming.

(Some codes might require primitive modification based on the description provided)

To implement any of these codes into your project it is necessary that you:

  1. Create a new Code module and name it according to the feature

  2. Delete all of the predefined example code - “make your canvas clean

  3. Copy and paste the code provided and modify only the specified variables.

 

add code.png

 

example new code.png

 

 

Exit image evaluation from flow:

It is possible to stop evaluation early to save time based on a specific condition e.g. a Classifier distinguishes product types and chooses different evaluation branches to meet the specific product requirements.

This is useful when using Parallelism to split the flow.

See the example of different evaluation flows based on product type:

 

  1. To stop the evaluation copy and paste the following code.

  2. Modify Program Settings part according to the classes you wish to filter. (In the above example list_of_exit_classes = ["Type 2"], therefore “Type 1” can pass through.)

def main(context): # List containing names of the classes that will exit the flow sooner. ######################## PROGRAM SETTINGS ################################### list_of_exit_classes = ["Type 1"] ######################## END PROGRAM SETTINGS ############################### for rectangle in context['detectedRectangles']: if rectangle['classNames'][0]['label'] in list_of_exit_classes: context['exit'] = True return

It is necessary to modify the list list_of_exit_classes based on what classes are contained in your current project that are not supposed to continue evaluating.

Crop image by a detected rectangle

Useful when the image contains unnecessary elements and the goal is to focus only on a certain part.

To do this:

  1. Train a Detector module (or other) to find the region of interest.

  2. Copy and paste the following code into the Code module.

  3. Modify the Program Settings part according to the class you wish to cut (or None if the rectangle does not have a class).

def main(context): ###### PROGRAM SETTINGS ######### label = "My Rectangle" ###### END PROGRAM SETTINGS ##### rectangle = find_rectangle(context, label) x_1, x_2 = int(rectangle['x']), int(rectangle['x'] + rectangle['width']) y_1, y_2 = int(rectangle['y']), int(rectangle['y'] + rectangle['height']) context['image'] = context['image'][y_1:y_2, x_1:x_2] def find_rectangle(context, label): for rectangle in context['detectedRectangles']: if label is None: return rectangle if label == rectangle['classNames'][0]['label']: return rectangle

It is necessary to modify the LABEL variable to contain the class name (or None) of the rectangle by which you want to crop the image.

Save evaluated images to folders

Useful when your goal is to inspect the evaluated images at a later time.

This code draws the found rectangles to the image and saves only the NG evaluated images.

  1. Create a new folder in Windows File Explorer where the images will be saved

  2. Copy and paste the following code into the Code module

  3. Modify Program Settings by specifying the folder path and drawn rectangle color

import cv2 import skimage import numpy as np from datetime import datetime from pathlib import Path RED = (0, 0, 255) GREEN = (0, 255, 0) BLUE = (255, 0, 0) YELLOW = (0, 255, 255) WHITE = (255, 255, 255) #################################### PROGRAM SETTINGS ####################################### # Specify the path to the save folder, image formats, and the wanted color of rectangles SAVE_FOLDER = r"C:\Users\Vox\Downloads" ORIGINAL_IMAGE_FORMAT = '.png' ANNOTATED_IMAGE_FORMAT = '.jpg' RECTANGLE_COLOR = RED ################################# END PROGRAM SETTINGS #################################### def main(context): # Get result result = context['result'] # When image has result TRUE, program stop. if result is True: return # Get image from context image = context['image'] # Save Original Image save_image_to_disc(image, filename_prefix = 'original_', image_format = ORIGINAL_IMAGE_FORMAT) # Draw rectangles to original image image = draw_rectangles_to_image(image, context, BARVA_RECTANGLU) # Save Anotated Image save_image_to_disc(image, filename_prefix = 'anotated_', image_format = ANNOTATED_IMAGE_FORMAT) def save_image_to_disc(image, filename_prefix:str = '', image_format = '.png'): timestamp = generate_timestamp() full_save_path = Path(SAVE_FOLDER).joinpath(filename_prefix + timestamp + image_format) cv2.imwrite(str(full_save_path), image) def generate_timestamp(): timestamp = datetime.now() formatted_timestamp = timestamp.strftime("%Y-%m-%d_%H-%M-%S_%f") return formatted_timestamp def draw_rectangles_to_image(image, context, color:tuple = (0,0,255)): for rect in context['detectedRectangles']: rect_start = (int(rect['x']), int(rect['y'])) rect_end = (int(rect['x'] + rect['width']), int(rect['y'] + rect['height'])) image = cv2.rectangle(image, rect_start, rect_end, color, 2) return image

It is necessary to modify at least SAVE_FOLDER with your path to the saving folder. Modifying ?_IMAGE_FORMAT or RECTANGLE_COLOR is not necessary.

The folder path to copy and paste is written here:

 

Send data to S7-1200 PLC

To send your custom data or data from Pekat to a PLC:

  1. Make sure that PLC has its Protection settings set up correctly via TIAportal - Same as in this example: https://snap7.sourceforge.net/snap7_client.html#1200_1500

  2. Copy and paste the code below and set correct values to all variables in Program Settings and Output Settings.

Use Heatmap as a mask

It is possible to use previously detected heatmap (from anomaly or surface) as a masking tool to hide specific objects or features.

  1. The project contains a trained anomaly/surface module producing a heatmap

  2. Copy and paste the following code

To get back the original image (delete the mask), copy and paste the following code into a different code module:

 

See image:

Surface detector finds the heatmap of an object → Code module masks out the image → Further detections are done → Code module unmasks back to the original.