Hi there,
I am developing a module which has a an image and associated gobject rectangle input. Currently the model gets stuck in “starting” phase when I click run. Here is my associated run_module function:
import cv2
import os
input_path_dict will have input file paths with keys corresponding to the input names set in the cli.
import cv2
import os
def run_module(input_path_dict, output_folder_path, coordinates, min_hysteresis=100, max_hysteresis=200):
“”"
This function should load input resources from input_path_dict, do any pre-processing steps, run the algorithm,
save all outputs to output_folder_path, AND return the outputs_path_dict.
:param input_path_dict: Dictionary of input resource paths indexed by input names.
:param output_folder_path: Directory where to save output results.
:param coordinates: Array with 4 values representing the crop rectangle [x1, y1, x2, y2].
:param min_hysteresis: Tunable parameter with a default value.
:param max_hysteresis: Tunable parameter with a default value.
:return: Dictionary of output result paths.
“”"
Preprocessing
Get input file paths from dictionary
input_img_path = input_path_dict[‘Input Image’] # Ensure the key matches the CLI input name
Load image in grayscale
img = cv2.imread(input_img_path, 0)
Extract cropping coordinates
x1, y1, x2, y2 = coordinates
print(x1, y1, x2, y2)
Crop the image
cropped_img = img[y1:y2, x1:x2]
Run algorithm
Apply Canny edge detection
#edges_detected = cv2.Canny(cropped_img, min_hysteresis, max_hysteresis)
Save output
Get filename without extension
input_img_name = os.path.splitext(os.path.basename(input_img_path))[0]
Generate output file path
output_img_path = os.path.join(output_folder_path, f"{input_img_name}_cropped_edges.jpg")
Save output image
cv2.imwrite(output_img_path, cropped_img)
Create dictionary of output paths
output_paths_dict = {‘Output Image’: output_img_path}
Return output paths dictionary
return output_paths_dict
if name == ‘main’:
Place some code to test implementation
Define input_path_dict and output_folder_path
input_path_dict = {}
current_directory = os.getcwd()
Place test image in current directory
input_path_dict[‘Input Image’] = os.path.join(current_directory,‘test_image.jpg’) # KEY MUST MATCH INPUT NAME SET IN CLI
output_folder_path = current_directory
Run algorithm and return output_paths_dict
output_paths_dict = run_module(input_path_dict, output_folder_path, min_hysteresis=100, max_hysteresis=200)
Get outPUT file path from dictionary
output_img_path = output_paths_dict[‘Output Image’] # KEY MUST MATCH OUTPUT NAME SET IN CLI
Load data
out_img = cv2.imread(output_img_path, 0)
Display output image and ensure correct output
#cv2.imshow(“Results”,out_img)