def main(read_directory: str, save_directory: str, sub_size, prepare_size):
    xml_paths = glob.glob(f"{read_directory}/*.xml")
    Trace(f"read_directory is '{read_directory}' got {len(xml_paths)} to '{save_directory}'\nsub_size is {sub_size}"
          f"\tprepare_size is {prepare_size}", Trace.TRACE_INFO, __file__, main.__name__)

    for xml_path in xml_paths:
        # Create xml object
        xml_obj = XMLImage(xml_path=xml_path)
        # Read image path
        img_path = xml_obj.image_path()
        # Read image
        image = cv2.imread(img_path)
        # Run workflow
        list_image, list_xml_obj = workflow(image, sub_size, prepare_size, xml_obj)
        for i in range(len(list_image)):
            # Save sub data
            sub_image = list_image[i]
            sub_xml_obj = list_xml_obj[i]

            img_file_name = create_sub_name(img_path, i, save_directory)
            sub_xml_obj.image_filename = img_file_name.split(settings.SLASH)[-1]

            cv2.imwrite(img_file_name, sub_image)
            sub_xml_obj.save(create_sub_name(xml_path, i, save_directory))
            pass  # for i
        pass  # for xml_path
    pass  # main
Ejemplo n.º 2
0
def _show_image_colab(window_name: str, image: settings.CV2_IMAGE, delay: int):
    Trace(f"Image's window name: {window_name}", Trace.TRACE_DEBUG, __file__,
          _show_image_colab.__name__)
    cv2_imshow(image)
    delay = min(1, delay)
    time.sleep(delay / 1000)
    pass
Ejemplo n.º 3
0
def correct_labels(directory: str, shape: Tuple[int, int], old_label: str,
                   new_label: str, del_label: str, change_type: int):
    if change_type == LOWER_CASE:
        change_function = str.lower
    elif change_type == UPPER_CASE:
        change_function = str.upper
    elif change_type == TITLE:
        change_function = str.title
    else:
        change_function = str

    switch_labels = bool(old_label) and bool(new_label)
    delete_label_flag = bool(del_label)
    any_change = switch_labels or (change_function
                                   is not str) or delete_label_flag

    if not any_change:
        Trace(f"\nNo change needed.\n", Trace.TRACE_INFO, __file__,
              correct_labels.__name__)
        return

    old_label = old_label.lower()
    del_label = del_label.lower()

    xml_paths = glob.glob(os.path.join(directory, '*.xml'))
    for xml_path in xml_paths:
        # Read
        xml_obj = XMLImage(xml_path=xml_path)
        # Edit
        index = -1
        to_delete = []
        for box in xml_obj.boxes_classes:
            index += 1
            # Delete label
            if delete_label_flag and box.label.lower() == del_label:
                to_delete.append(index)
                # xml_obj.boxes_classes.pop(index)
                continue

            # Switch label
            if switch_labels and box.label.lower() == old_label:
                box.label = new_label

            # Correct label
            box.label = change_function(box.label).strip()
            pass  # for box
        for i in range(len(to_delete) - 1, -1, -1):
            xml_obj.boxes_classes.pop(to_delete[i])

        # Save
        xml_obj.save()
        pass  # for xml_path
    pass
Ejemplo n.º 4
0
    def _calculate(self):
        self.x_centre = (self.x_max + self.x_min) // 2
        self.y_centre = (self.y_max + self.y_min) // 2
        self.width = self.x_max - self.x_min
        self.height = self.y_max - self.y_min
        self.area = self.width * self.height

        self.is_correct = self.width > 0 and self.height > 0
        if not self.is_correct:
            Trace(f"Invalid BBox {str(self)}", Trace.TRACE_WARNING, __file__,
                  self._calculate.__name__)
        pass  # def _calculate
Ejemplo n.º 5
0
def black_bars(image: settings.CV2_IMAGE, shape2d,
               xml_obj: XMLImage) -> Tuple[settings.CV2_IMAGE, XMLImage]:
    h_img, w_img = image.shape
    h_out, w_out = shape2d
    if h_img > h_out or w_img > w_out:
        Trace(
            f"Given image ({image.shape}) is bigger than give output ({shape2d})",
            Trace.TRACE_WARNING, __file__, black_bars.__name__)
        image = image[:h_img, :w_img]
        pass
    new_image = np.zeros(shape2d).astype('uint8')
    new_image[:h_img, :w_img] = image
    return new_image, xml_obj.black_bars(shape2d)
Ejemplo n.º 6
0
def prepare_data(image: settings.CV2_IMAGE, shape2d, xml_obj: XMLImage = None):
    if settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_RESIZE:
        image, xml_obj = resize(image, shape2d, xml_obj)
    elif settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_BLACK_BARS:
        image, xml_obj = black_bars(image, shape2d, xml_obj)
    elif settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_CUT_MIDDLE:
        image, xml_obj = _cut_middle(image, shape2d, xml_obj)
    elif settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_CUT_TOP_LEFT:
        image, xml_obj = _cut_top_left(image, shape2d, xml_obj)
    elif settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_CUT_TOP_RIGHT:
        image, xml_obj = _cut_top_right(image, shape2d, xml_obj)
    elif settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_CUT_BOTTOM_LEFT:
        image, xml_obj = _cut_bottom_left(image, shape2d, xml_obj)
    elif settings.STG_PREPARE_METHOD == settings.STAGE_PREPARE_IMAGE_CUT_BOTTOM_RIGHT:
        image, xml_obj = _cut_bottom_right(image, shape2d, xml_obj)
    else:
        Trace(f"Wrong STG_PREPARE_METHOD = {settings.STG_PREPARE_METHOD}",
              Trace.TRACE_ERROR, __file__, prepare_data.__name__)
    return image, xml_obj
Ejemplo n.º 7
0
    def divide(self, shape2d) -> List:
        # Divide boxes
        sub_boxes = self._divide_boxes(shape2d)
        # Create XMLImages
        sub_xmls = list()
        for boxes_list in sub_boxes:
            xml_text = self.create_xml_text(
                folder=self.folder,
                image_filename=self.image_filename,
                path=self.path,
                width=shape2d[1],
                height=shape2d[0],
                depth=self.depth,
                boxes_classes=boxes_list
            )
            xml_obj = XMLImage(xml_text=xml_text)
            sub_xmls.append(xml_obj)
            pass

        Trace(f"From {len(self.boxes_classes)} to {sum([len(elem) for elem in sub_boxes])}"
              f" - {[len(elem) for elem in sub_boxes]} . File {self.image_filename}",
              Trace.TRACE_DEBUG, __file__, self.divide.__name__)

        return sub_xmls