Skip to content
49 changes: 48 additions & 1 deletion source/pic2card/datagen/generate_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,41 @@
from lxml import etree as et
from mystique import config
from .utils import get_image_name
import cv2



def calculate_annotation(
elements_with_path: List[str],
elements_dimensions: List[tuple],
padded_image_element: List[Sequence],
has_merged_image_element: List[bool],
element_positions: List[str],
) -> List[List[tuple]]:
"""
Calculates the annotations for a given list of elements.

@param elements_with_path: list of elements path from elements directory
@param elements_dimensions: list of elements dimensions
@param padded_image_element: list of image elements after padding
@param has_merged_image_element: boolean True if the image has any merged elements
@param element_positions: list of the image element postions on canvas wrt config

@return: annotations
"""
annotations = []
padded_elements_height = [
element.shape[0] for element in padded_image_element
]
padded_elements_width = padded_image_element[0].shape[1]
number_of_elements = len(elements_with_path)

for index in range(number_of_elements):
if 'right' in element_positions[index]:
xmin = padded_elements_width - (elements_dimensions[index][1] + 10)
else:
xmin = 10

number_of_elements = len(elements_with_path)
for index in range(number_of_elements):
xmin = 10
Expand All @@ -36,11 +52,20 @@ def calculate_annotation(
sum([padded_elements_height[height] for height in range(index)])
+ 10
)
if 'right' in element_positions[index]:
xmax = padded_elements_width - 10
else:
xmax = elements_dimensions[index][1] + 10
xmax = elements_dimensions[index][1] + 10
ymax = (
sum([padded_elements_height[height] for height in range(index + 1)])
- 10
)

if has_merged_image_element:
if has_merged_image_element[index] and 'right' in element_positions[index]:
ymin = annotations[-1][0][1]
ymax = annotations[-1][1][1]
annotations.append([(xmin, ymin), (xmax, ymax)])
return annotations

Expand Down Expand Up @@ -91,19 +116,25 @@ def run_annotator(
elements_with_path: List[str],
elements_dimensions: List[tuple],
padded_image_element: List[Sequence],
has_merged_image_element: List[bool],
element_positions: List[str],
) -> List[List[tuple]]:
"""
Returns a list of x and y coords of the elements in the generated image
for both above and below threshold element numbers
@param elements_with_path: list of elements path from elements directory
@param elements_dimensions: list of elements dimensions
@param padded_image_element: list of image elements after padding
@param has_merged_image_element: boolean True if the image has any merged elements
@param element_positions: list of the image element postions on canvas wrt config

@return: annotations
"""
number_of_elements = len(elements_with_path)
if number_of_elements <= config.ELEMENT_COUNT_THRESHOLD:
annotations = calculate_annotation(
elements_with_path, elements_dimensions, padded_image_element
elements_with_path, elements_dimensions, padded_image_element,
has_merged_image_element, element_positions
)
else:
mid_value = number_of_elements // 2
Expand All @@ -115,6 +146,8 @@ def run_annotator(
left_elements_with_path,
left_elements_dimensions,
left_padded_image_element,
has_merged_image_element,
element_positions,
)

right_elements_with_path = elements_with_path[mid_value:]
Expand All @@ -125,6 +158,8 @@ def run_annotator(
right_elements_with_path,
right_elements_dimensions,
right_padded_image_element,
has_merged_image_element,
element_positions,
)

padded_pixels_for_right_element = (
Expand Down Expand Up @@ -158,7 +193,19 @@ def get_annotation_file(
layout.elements_with_path,
layout.element_dimensions,
padded_image_element,
layout.has_merged_elements,
layout.element_positions,
)

"""
#check annotations
for annotation in annotations:
cv2.rectangle(generated_image, annotation[0], annotation[1], (255, 0, 0), 2)
cv2.imshow('annotated_image', generated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""

# get annotation xml
annotation_xml = generate_annotation_xml(
annotations, generated_image_prop, element_type
Expand Down
Loading