class_docstr: This is the object detection class for images in the ImageAI library. It provides support for RetinaNet
      , YOLOv3 and TinyYOLOv3 object detection networks . After instantiating this class, you can set it's properties and
  make object detections using it's pre-defined functions.
  
  The following functions are required to be called before object detection can be made
  * setModelPath()
  * At least of of the following and it must correspond to the model set in the setModelPath()
  [setModelTypeAsRetinaNet(), setModelTypeAsYOLOv3(), setModelTypeAsTinyYOLOv3()]
  * loadModel() [This must be called once only before performing object detection]
  
  Once the above functions have been called, you can call the detectObjectsFromImage() function of
  the object detection instance object at anytime to obtain observable objects in any image.
class_docstr_tok: ["This","is","the","object","detection","class","for","images","in","the","ImageAI","library",".","It","provides","support","for","RetinaNet",",","YOLOv3","and","TinyYOLOv3","object","detection","networks",".","After","instantiating","this","class",",","you","can","set","it","'s","properties","and","make","object","detections","using","it","'s","pre-defined","functions",".","The","following","functions","are","required","to","be","called","before","object","detection","can","be","made","*","setModelPath","(",")","*","At","least","of","of","the","following","and","it","must","correspond","to","the","model","set","in","the","setModelPath","(",")","[","setModelTypeAsRetinaNet","(",")",",","setModelTypeAsYOLOv3","(",")",",","setModelTypeAsTinyYOLOv3","(",")","]","*","loadModel","(",")","[","This","must","be","called","once","only","before","performing","object","detection","]","Once","the","above","functions","have","been","called",",","you","can","call","the","detectObjectsFromImage","(",")","function","of","the","object","detection","instance","object","at","anytime","to","obtain","observable","objects","in","any","image","."]
docstr: 'detectObjectsFromImage()' function is used to detect objects observable in the given image path:
          * input_image , which can be a filepath, image numpy array or image file stream
          * 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) , file path/numpy array/image file stream of the image. Acceptable values are "file", "array" and "stream"
          * 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, 50 by default) , option to set the minimum percentage probability for nominating a detected object for output.
          * 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 Tensorflow 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 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","path",":","*","input_image",",","which","can","be","a","filepath",",","image","numpy","array","or","image","file","stream","*","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",")",",","file","path\/numpy","array\/image","file","stream","of","the","image",".","Acceptable","values","are","``","file","''",",","``","array","''","and","``","stream","''","*","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",",","50","by","default",")",",","option","to","set","the","minimum","percentage","probability","for","nominating","a","detected","object","for","output",".","*","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","Tensorflow","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","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,
      display_percentage_probability=True,
      display_object_name=True,
      display_box=True,
      thread_safe=False,
      custom_objects=None,
  ):
      """
      'detectObjectsFromImage()' function is used to detect objects observable in the given image path:
              * input_image , which can be a filepath, image numpy array or image file stream
              * 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) , file path/numpy array/image file stream of the image. Acceptable values are "file", "array" and "stream"
              * 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, 50 by default) , option to set the minimum percentage probability for nominating a detected object for output.
              * 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 Tensorflow 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 display_percentage_probability:
      :param display_object_name:
      :param thread_safe:
      :return image_frame:
      :return output_objects_array:
      :return detected_objects_image_array:
      """
  
      if self.__modelLoaded == False:
          raise ValueError(
              "You must call the loadModel() function before making object detection."
          )
      elif self.__modelLoaded == True:
          try:
              model_detections = list()
              detections = list()
              image_copy = None
  
              detected_objects_image_array = []
              min_probability = minimum_percentage_probability / 100
  
              if input_type == "file":
                  input_image = cv2.imread(input_image)
              elif input_type == "array":
                  input_image = np.array(input_image)
  
              detected_copy = input_image
              image_copy = input_image
  
              if (
                  self.__modelType == "yolov3"
                  or self.__modelType == "tinyyolov3"
              ):
                  image_h, image_w, _ = detected_copy.shape
                  detected_copy = preprocess_input(
                      detected_copy, self.__yolo_model_image_size
                  )
  
                  model = self.__model_collection[0]
                  yolo_result = model.predict(detected_copy)
  
                  model_detections = retrieve_yolo_detections(
                      yolo_result,
                      self.__yolo_anchors,
                      min_probability,
                      self.__nms_thresh,
                      self.__yolo_model_image_size,
                      (image_w, image_h),
                      self.numbers_to_names,
                  )
  
              elif self.__modelType == "retinanet":
                  detected_copy = preprocess_image(detected_copy)
                  detected_copy, scale = resize_image(detected_copy)
  
                  model = self.__model_collection[0]
                  boxes, scores, labels = model.predict_on_batch(
                      np.expand_dims(detected_copy, axis=0)
                  )
  
                  boxes /= scale
  
                  for box, score, label in zip(
                      boxes[0], scores[0], labels[0]
                  ):
                      # scores are sorted so we can break
                      if score < min_probability:
                          break
  
                      detection_dict = dict()
                      detection_dict["name"] = self.numbers_to_names[
                          label
                      ]
                      detection_dict["percentage_probability"] = (
                          score * 100
                      )
                      detection_dict["box_points"] = box.astype(
                          int
                      ).tolist()
                      model_detections.append(detection_dict)
  
              counting = 0
              objects_dir = output_image_path + "-objects"
  
              for detection in model_detections:
                  counting += 1
                  label = detection["name"]
                  percentage_probability = detection[
                      "percentage_probability"
                  ]
                  box_points = detection["box_points"]
  
                  if custom_objects is not None:
                      if custom_objects[label] != "valid":
                          continue
  
                  detections.append(detection)
  
                  if display_object_name == False:
                      label = None
  
                  if display_percentage_probability == False:
                      percentage_probability = None
  
                  image_copy = draw_boxes(
                      image_copy,
                      box_points,
                      display_box,
                      label,
                      percentage_probability,
                      self.__box_color,
                  )
  
                  if extract_detected_objects == True:
                      splitted_copy = image_copy.copy()[
                          box_points[1] : box_points[3],
                          box_points[0] : box_points[2],
                      ]
                      if output_type == "file":
                          if os.path.exists(objects_dir) == False:
                              os.mkdir(objects_dir)
                          splitted_image_path = os.path.join(
                              objects_dir,
                              detection["name"]
                              + "-"
                              + str(counting)
                              + ".jpg",
                          )
                          cv2.imwrite(
                              splitted_image_path, splitted_copy
                          )
                          detected_objects_image_array.append(
                              splitted_image_path
                          )
                      elif output_type == "array":
                          detected_objects_image_array.append(
                              splitted_copy
                          )
  
              if output_type == "file":
                  cv2.imwrite(output_image_path, image_copy)
  
              if extract_detected_objects == True:
                  if output_type == "file":
                      return detections, detected_objects_image_array
                  elif output_type == "array":
                      return (
                          image_copy,
                          detections,
                          detected_objects_image_array,
                      )
  
              else:
                  if output_type == "file":
                      return detections
                  elif output_type == "array":
                      return image_copy, detections
  
          except:
              raise ValueError(
                  "Ensure you specified correct input image, input type, output type and/or output image path "
              )
  
imports: import cv2
  from imageai.Detection.keras_retinanet import models
  from imageai.Detection.keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
  from imageai.Detection.keras_retinanet.utils.visualization import draw_box, draw_caption
  import matplotlib.pyplot
  import matplotlib.image
  import numpy
  import tensorflow
  import os
  from tensorflow.keras import backend
  from tensorflow.keras.layers import Input
  from PIL import Image
  import colorsys
  import warnings
  from imageai.Detection.YOLO.yolov3 import tiny_yolov3_main, yolov3_main
  from imageai.Detection.YOLO.utils import letterbox_image, yolo_eval, preprocess_input, retrieve_yolo_detections, draw_boxes
label_desc: Use image node_id 7 for calling the ObjectDetection 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, display_percentage_probability, display_object_name, display_box, thread_safe, custom_objects) and returns: detections,  detected_objects_image_array, detections, image_copy,  detections,  detected_objects_image_array, image_copy,  detections