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