class_docstr: This is the object detection class for using your custom trained models. It supports your custom trained YOLOv3 model and allows to you to perform object detection in images.
docstr: 'detectObjectsFromImage()' function is used to detect objects observable in the given image:
              * input_image , which can be a filepath or image numpy array in BGR
              * output_image_path (only if output_type = file) , file path to the output image that will contain the detection boxes and label, if output_type="file"
              * input_type (optional) , filepath/numpy array of the image. Acceptable values are "file" and "array"
              * output_type (optional) , file path/numpy array/image file stream of the image. Acceptable values are "file" and "array"
              * extract_detected_objects (optional) , option to save each object detected individually as an image and return an array of the objects' image path.
              * minimum_percentage_probability (optional, 30 by default) , option to set the minimum percentage probability for nominating a detected object for output.
              * nms_threshold (optional, o.45 by default) , option to set the Non-maximum suppression for the detection
              * display_percentage_probability (optional, True by default), option to show or hide the percentage probability of each object in the saved/returned detected image
              * display_display_object_name (optional, True by default), option to show or hide the name of each object in the saved/returned detected image
              * thread_safe (optional, False by default), enforce the loaded detection model works across all threads if set to true, made possible by forcing all Keras inference to run on the default graph
  
  
      The values returned by this function depends on the parameters parsed. The possible values returnable
      are stated as below
      - If extract_detected_objects = False or at its default value and output_type = 'file' or
          at its default value, you must parse in the 'output_image_path' as a string to the path you want
          the detected image to be saved. Then the function will return:
          1. an array of dictionaries, with each dictionary corresponding to the objects
              detected in the image. Each dictionary contains the following property:
              * name (string)
              * percentage_probability (float)
              * box_points (list of x1,y1,x2 and y2 coordinates)
  
      - If extract_detected_objects = False or at its default value and output_type = 'array' ,
        Then the function will return:
  
          1. a numpy array of the detected image
          2. an array of dictionaries, with each dictionary corresponding to the objects
              detected in the image. Each dictionary contains the following property:
              * name (string)
              * percentage_probability (float)
              * box_points (list of x1,y1,x2 and y2 coordinates)
  
      - If extract_detected_objects = True and output_type = 'file' or
          at its default value, you must parse in the 'output_image_path' as a string to the path you want
          the detected image to be saved. Then the function will return:
          1. an array of dictionaries, with each dictionary corresponding to the objects
              detected in the image. Each dictionary contains the following property:
              * name (string)
              * percentage_probability (float)
              * box_points (list of x1,y1,x2 and y2 coordinates)
          2. an array of string paths to the image of each object extracted from the image
  
      - If extract_detected_objects = True and output_type = 'array', the the function will return:
          1. a numpy array of the detected image
          2. an array of dictionaries, with each dictionary corresponding to the objects
              detected in the image. Each dictionary contains the following property:
              * name (string)
              * percentage_probability (float)
              * box_points (list of x1,y1,x2 and y2 coordinates)
          3. an array of numpy arrays of each object detected in the image
  
  :param input_image:
  :param output_image_path:
  :param input_type:
  :param output_type:
  :param extract_detected_objects:
  :param minimum_percentage_probability:
  :param nms_treshold:
  :param display_percentage_probability:
  :param display_object_name:
  :param thread_safe:
  :return image_frame:
  :return output_objects_array:
  :return detected_objects_image_array:
docstr_tok: ["'detectObjectsFromImage","(",")","'","function","is","used","to","detect","objects","observable","in","the","given","image",":","*","input_image",",","which","can","be","a","filepath","or","image","numpy","array","in","BGR","*","output_image_path","(","only","if","output_type","=","file",")",",","file","path","to","the","output","image","that","will","contain","the","detection","boxes","and","label",",","if","output_type=","''","file","''","*","input_type","(","optional",")",",","filepath\/numpy","array","of","the","image",".","Acceptable","values","are","``","file","''","and","``","array","''","*","output_type","(","optional",")",",","file","path\/numpy","array\/image","file","stream","of","the","image",".","Acceptable","values","are","``","file","''","and","``","array","''","*","extract_detected_objects","(","optional",")",",","option","to","save","each","object","detected","individually","as","an","image","and","return","an","array","of","the","objects","'","image","path",".","*","minimum_percentage_probability","(","optional",",","30","by","default",")",",","option","to","set","the","minimum","percentage","probability","for","nominating","a","detected","object","for","output",".","*","nms_threshold","(","optional",",","o.45","by","default",")",",","option","to","set","the","Non-maximum","suppression","for","the","detection","*","display_percentage_probability","(","optional",",","True","by","default",")",",","option","to","show","or","hide","the","percentage","probability","of","each","object","in","the","saved\/returned","detected","image","*","display_display_object_name","(","optional",",","True","by","default",")",",","option","to","show","or","hide","the","name","of","each","object","in","the","saved\/returned","detected","image","*","thread_safe","(","optional",",","False","by","default",")",",","enforce","the","loaded","detection","model","works","across","all","threads","if","set","to","true",",","made","possible","by","forcing","all","Keras","inference","to","run","on","the","default","graph","The","values","returned","by","this","function","depends","on","the","parameters","parsed",".","The","possible","values","returnable","are","stated","as","below","-","If","extract_detected_objects","=","False","or","at","its","default","value","and","output_type","=","'file","'","or","at","its","default","value",",","you","must","parse","in","the","'output_image_path","'","as","a","string","to","the","path","you","want","the","detected","image","to","be","saved",".","Then","the","function","will","return",":","1.","an","array","of","dictionaries",",","with","each","dictionary","corresponding","to","the","objects","detected","in","the","image",".","Each","dictionary","contains","the","following","property",":","*","name","(","string",")","*","percentage_probability","(","float",")","*","box_points","(","list","of","x1",",","y1",",","x2","and","y2","coordinates",")","-","If","extract_detected_objects","=","False","or","at","its","default","value","and","output_type","=","'array","'",",","Then","the","function","will","return",":","1.","a","numpy","array","of","the","detected","image","2.","an","array","of","dictionaries",",","with","each","dictionary","corresponding","to","the","objects","detected","in","the","image",".","Each","dictionary","contains","the","following","property",":","*","name","(","string",")","*","percentage_probability","(","float",")","*","box_points","(","list","of","x1",",","y1",",","x2","and","y2","coordinates",")","-","If","extract_detected_objects","=","True","and","output_type","=","'file","'","or","at","its","default","value",",","you","must","parse","in","the","'output_image_path","'","as","a","string","to","the","path","you","want","the","detected","image","to","be","saved",".","Then","the","function","will","return",":","1.","an","array","of","dictionaries",",","with","each","dictionary","corresponding","to","the","objects","detected","in","the","image",".","Each","dictionary","contains","the","following","property",":","*","name","(","string",")","*","percentage_probability","(","float",")","*","box_points","(","list","of","x1",",","y1",",","x2","and","y2","coordinates",")","2.","an","array","of","string","paths","to","the","image","of","each","object","extracted","from","the","image","-","If","extract_detected_objects","=","True","and","output_type","=","'array","'",",","the","the","function","will","return",":","1.","a","numpy","array","of","the","detected","image","2.","an","array","of","dictionaries",",","with","each","dictionary","corresponding","to","the","objects","detected","in","the","image",".","Each","dictionary","contains","the","following","property",":","*","name","(","string",")","*","percentage_probability","(","float",")","*","box_points","(","list","of","x1",",","y1",",","x2","and","y2","coordinates",")","3.","an","array","of","numpy","arrays","of","each","object","detected","in","the","image",":","param","input_image",":",":","param","output_image_path",":",":","param","input_type",":",":","param","output_type",":",":","param","extract_detected_objects",":",":","param","minimum_percentage_probability",":",":","param","nms_treshold",":",":","param","display_percentage_probability",":",":","param","display_object_name",":",":","param","thread_safe",":",":","return","image_frame",":",":","return","output_objects_array",":",":","return","detected_objects_image_array",":"]
code: def detectObjectsFromImage(
      self,
      input_image="",
      output_image_path="",
      input_type="file",
      output_type="file",
      extract_detected_objects=False,
      minimum_percentage_probability=50,
      nms_treshold=0.4,
      display_percentage_probability=True,
      display_object_name=True,
      thread_safe=False,
  ):
      """
  
      'detectObjectsFromImage()' function is used to detect objects observable in the given image:
                  * input_image , which can be a filepath or image numpy array in BGR
                  * output_image_path (only if output_type = file) , file path to the output image that will contain the detection boxes and label, if output_type="file"
                  * input_type (optional) , filepath/numpy array of the image. Acceptable values are "file" and "array"
                  * output_type (optional) , file path/numpy array/image file stream of the image. Acceptable values are "file" and "array"
                  * extract_detected_objects (optional) , option to save each object detected individually as an image and return an array of the objects' image path.
                  * minimum_percentage_probability (optional, 30 by default) , option to set the minimum percentage probability for nominating a detected object for output.
                  * nms_threshold (optional, o.45 by default) , option to set the Non-maximum suppression for the detection
                  * display_percentage_probability (optional, True by default), option to show or hide the percentage probability of each object in the saved/returned detected image
                  * display_display_object_name (optional, True by default), option to show or hide the name of each object in the saved/returned detected image
                  * thread_safe (optional, False by default), enforce the loaded detection model works across all threads if set to true, made possible by forcing all Keras inference to run on the default graph
  
  
          The values returned by this function depends on the parameters parsed. The possible values returnable
          are stated as below
          - If extract_detected_objects = False or at its default value and output_type = 'file' or
              at its default value, you must parse in the 'output_image_path' as a string to the path you want
              the detected image to be saved. Then the function will return:
              1. an array of dictionaries, with each dictionary corresponding to the objects
                  detected in the image. Each dictionary contains the following property:
                  * name (string)
                  * percentage_probability (float)
                  * box_points (list of x1,y1,x2 and y2 coordinates)
  
          - If extract_detected_objects = False or at its default value and output_type = 'array' ,
            Then the function will return:
  
              1. a numpy array of the detected image
              2. an array of dictionaries, with each dictionary corresponding to the objects
                  detected in the image. Each dictionary contains the following property:
                  * name (string)
                  * percentage_probability (float)
                  * box_points (list of x1,y1,x2 and y2 coordinates)
  
          - If extract_detected_objects = True and output_type = 'file' or
              at its default value, you must parse in the 'output_image_path' as a string to the path you want
              the detected image to be saved. Then the function will return:
              1. an array of dictionaries, with each dictionary corresponding to the objects
                  detected in the image. Each dictionary contains the following property:
                  * name (string)
                  * percentage_probability (float)
                  * box_points (list of x1,y1,x2 and y2 coordinates)
              2. an array of string paths to the image of each object extracted from the image
  
          - If extract_detected_objects = True and output_type = 'array', the the function will return:
              1. a numpy array of the detected image
              2. an array of dictionaries, with each dictionary corresponding to the objects
                  detected in the image. Each dictionary contains the following property:
                  * name (string)
                  * percentage_probability (float)
                  * box_points (list of x1,y1,x2 and y2 coordinates)
              3. an array of numpy arrays of each object detected in the image
  
      :param input_image:
      :param output_image_path:
      :param input_type:
      :param output_type:
      :param extract_detected_objects:
      :param minimum_percentage_probability:
      :param nms_treshold:
      :param display_percentage_probability:
      :param display_object_name:
      :param thread_safe:
      :return image_frame:
      :return output_objects_array:
      :return detected_objects_image_array:
      """
  
      if self.__model is None:
          raise ValueError(
              "You must call the loadModel() function before making object detection."
          )
      else:
          if output_type == "file":
              # from the image file, lets keep the directory and the filename, but remove its  format
              # if output_image_path is path/to/the/output/image.png
              # then output_image_folder is  path/to/the/output/image
              # let's check if it is in the appropriated format soon to fail early
              output_image_folder, n_subs = re.subn(
                  r"\.(?:jpe?g|png|tif|webp|PPM|PGM)$",
                  "",
                  output_image_path,
                  flags=re.I,
              )
              if n_subs == 0:
                  # if no substitution was done, the given output_image_path is not in a supported format,
                  # raise an error
                  raise ValueError(
                      "output_image_path must be the path where to write the image. "
                      "Therefore it must end as one the following: "
                      "'.jpg', '.png', '.tif', '.webp', '.PPM', '.PGM'. {} found".format(
                          output_image_path
                      )
                  )
              elif extract_detected_objects:
                  # Results must be written as files and need to extract detected objects as images,
                  # let's create a folder to store the object's images
                  objects_dir = output_image_folder + "-objects"
  
                  os.makedirs(objects_dir, exist_ok=True)
  
          self.__object_threshold = minimum_percentage_probability / 100
          self.__nms_threshold = nms_treshold
  
          output_objects_array = []
          detected_objects_image_array = []
  
          if input_type == "file":
              image = cv2.imread(input_image)
          elif input_type == "array":
              image = input_image
          else:
              raise ValueError(
                  "input_type must be 'file' or 'array'. {} found".format(
                      input_type
                  )
              )
  
          image_frame = image.copy()
  
          height, width, channels = image.shape
  
          image = cv2.resize(
              image, (self.__input_size, self.__input_size)
          )
  
          image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  
          image = image.astype("float32") / 255.0
  
          # expand the image to batch
          image = np.expand_dims(image, 0)
  
          if self.__model_type == "yolov3":
              if thread_safe == True:
                  with K.get_session().graph.as_default():
                      yolo_results = self.__model.predict(image)
              else:
                  yolo_results = self.__model.predict(image)
  
              boxes = list()
  
              for idx, result in enumerate(yolo_results):
                  box_set = self.__detection_utils.decode_netout(
                      result[0],
                      self.__model_anchors[idx],
                      self.__object_threshold,
                      self.__input_size,
                      self.__input_size,
                  )
                  boxes += box_set
  
              self.__detection_utils.correct_yolo_boxes(
                  boxes,
                  height,
                  width,
                  self.__input_size,
                  self.__input_size,
              )
  
              self.__detection_utils.do_nms(boxes, self.__nms_threshold)
  
              (
                  all_boxes,
                  all_labels,
                  all_scores,
              ) = self.__detection_utils.get_boxes(
                  boxes, self.__model_labels, self.__object_threshold
              )
  
              for object_box, object_label, object_score in zip(
                  all_boxes, all_labels, all_scores
              ):
                  each_object_details = dict()
                  each_object_details["name"] = object_label
                  each_object_details[
                      "percentage_probability"
                  ] = object_score
  
                  if object_box.xmin < 0:
                      object_box.xmin = 0
                  if object_box.ymin < 0:
                      object_box.ymin = 0
  
                  each_object_details["box_points"] = [
                      object_box.xmin,
                      object_box.ymin,
                      object_box.xmax,
                      object_box.ymax,
                  ]
                  output_objects_array.append(each_object_details)
  
              drawn_image = (
                  self.__detection_utils.draw_boxes_and_caption(
                      image_frame.copy(),
                      all_boxes,
                      all_labels,
                      all_scores,
                      show_names=display_object_name,
                      show_percentage=display_percentage_probability,
                  )
              )
  
              if extract_detected_objects:
                  for cnt, each_object in enumerate(
                      output_objects_array
                  ):
                      splitted_image = image_frame[
                          each_object["box_points"][1] : each_object[
                              "box_points"
                          ][3],
                          each_object["box_points"][0] : each_object[
                              "box_points"
                          ][2],
                      ]
                      if output_type == "file":
                          splitted_image_path = os.path.join(
                              objects_dir,
                              "{}-{:05d}.jpg".format(
                                  each_object["name"], cnt
                              ),
                          )
  
                          cv2.imwrite(
                              splitted_image_path, splitted_image
                          )
                          detected_objects_image_array.append(
                              splitted_image_path
                          )
                      elif output_type == "array":
                          detected_objects_image_array.append(
                              splitted_image.copy()
                          )
  
              if output_type == "file":
                  # we already validated that the output_image_path is a supported by OpenCV one
                  cv2.imwrite(output_image_path, drawn_image)
  
              if extract_detected_objects:
                  if output_type == "file":
                      return (
                          output_objects_array,
                          detected_objects_image_array,
                      )
                  elif output_type == "array":
                      return (
                          drawn_image,
                          output_objects_array,
                          detected_objects_image_array,
                      )
  
              else:
                  if output_type == "file":
                      return output_objects_array
                  elif output_type == "array":
                      return drawn_image, output_objects_array
  
imports: import os
  import re
  import numpy
  import json
  from imageai.Detection.Custom.voc import parse_voc_annotation
  from imageai.Detection.YOLO.yolov3 import yolov3_main, yolov3_train, dummy_loss
  from imageai.Detection.Custom.generator import BatchGenerator
  from imageai.Detection.Custom.utils.utils import normalize, evaluate, makedirs
  from tensorflow.keras.callbacks import ReduceLROnPlateau
  from tensorflow.keras.optimizers import Adam
  from imageai.Detection.Custom.callbacks import CustomModelCheckpoint
  from imageai.Detection.Custom.utils.multi_gpu_model import multi_gpu_model
  from imageai.Detection.Custom.gen_anchors import generateAnchors
  import tensorflow
  from tensorflow.keras.models import load_model
  from tensorflow.keras import Input
  from tensorflow.keras.callbacks import TensorBoard
  import tensorflow.keras.backend
  import cv2
label_desc: Use image node_id 6 for calling the CustomObjectDetection obj's underlying member method code with example usage: obj.detectObjectsFromImage(input_image, output_image_path, input_type, output_type, extract_detected_objects, minimum_percentage_probability, nms_treshold, display_percentage_probability, display_object_name, thread_safe) and returns: output_objects_array,  detected_objects_image_array, output_objects_array, drawn_image,  output_objects_array,  detected_objects_image_array, drawn_image,  output_objects_array