Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

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 (smile)

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

add code.pngImage Added

example new code.pngImage Added

clean canvas.pngImage Added

Expand
titleStop image from continuing in evaluation

Exit image evaluation from flow:

It is possible to stop evaluation to save time based on a specific condition e.g. a Classifier returned a defective product class and therefore further evaluation is not needed.

ended evaluation.pngImage Added

To stop the evaluation copy and paste the following code:

Code Block
languagepy
def main(context):
    # List containing names of the classes that will exit the flow sooner

...

awfawf

awfwa wa fwa fwa

...

titleCut by a detected rectangle

...

.
    #
    # If previous modules found a rectangle with "My Class 1" the evaluation
    # will stop here.
    #
    # Change the list based on your real classes!
    list_of_exit_classes = ["My Class 1", "My Class 2"]
    
    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.

Expand
titleCrop image by a detected rectangle

Crop image by a detected rectangle

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

show_crop.pngImage Added

To do this you first need to train a Detector module (or other) to find the region of interest. Then copy the following code into the Code module:

Code Block
languagepy
def main(context):
    # Specify the class of the rectangle by which to crop the image
    # If your rectangle does not have a class input None
    LABEL = "My Rectangle" # None
    
    rectangle = find_rectangle(context)
    
    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):
    for rectangle in context['detectedRectangles']:
        if LABEL is None:
            return rectangle
        if LABEL == rectangle['classNames']['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.

Expand
titleSave evaluated images (with or without rectangles)

Save evaluated images to folders

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

Info

This code saves only the images that were evaluated as

Status
colourRed
titleng
.

To do this create a new folder you wish to save your images to. Then copy the following code into the Code module:

Code Block
languagepy
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)

#################################### START CODE 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 CODE 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:

save_folder_path.pngImage Added