# Path: pycoral/adapters/common.py
# def output_tensor(interpreter, i):
# def input_details(interpreter, key):
# def input_size(interpreter):
# def input_tensor(interpreter):
# def set_input(interpreter, data):
# def set_resized_input(interpreter, size, resize):
# 
# Path: pycoral/adapters/detect.py
# class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
#   def width(self):
#   def height(self):
#   def area(self):
#   def valid(self):
#   def scale(self, sx, sy):
#   def translate(self, dx, dy):
#   def map(self, f):
#   def intersect(a, b):
#   def union(a, b):
#   def iou(a, b):
# def get_objects(interpreter,
#                 score_threshold=-float('inf'),
#                 image_scale=(1.0, 1.0)):
#   def make(i):
# 
# Path: pycoral/utils/dataset.py
# def read_label_file(file_path):
#   """Reads labels from a text file and returns it as a dictionary.
# 
#   This function supports label files with the following formats:
# 
#   + Each line contains id and description separated by colon or space.
#     Example: ``0:cat`` or ``0 cat``.
#   + Each line contains a description only. The returned label id's are based on
#     the row number.
# 
#   Args:
#     file_path (str): path to the label file.
# 
#   Returns:
#     Dict of (int, string) which maps label id to description.
#   """
#   with open(file_path, 'r', encoding='utf-8') as f:
#     lines = f.readlines()
#   ret = {}
#   for row_number, content in enumerate(lines):
#     pair = re.split(r'[:\s]+', content.strip(), maxsplit=1)
#     if len(pair) == 2 and pair[0].strip().isdigit():
#       ret[int(pair[0])] = pair[1].strip()
#     else:
#       ret[row_number] = content.strip()
#   return ret
# 
# Path: pycoral/utils/edgetpu.py
# def make_interpreter(model_path_or_content, device=None, delegate=None):
#   """Creates a new ``tf.lite.Interpreter`` instance using the given model.
# 
#   **Note:** If you have multiple Edge TPUs, you should always specify the
#   ``device`` argument.
# 
#   Args:
#      model_path_or_content (str or bytes): `str` object is interpreted as
#        model path, `bytes` object is interpreted as model content.
#      device (str): The Edge TPU device you want:
# 
#        + None      -- use any Edge TPU (this is the default)
#        + ":<N>"    -- use N-th Edge TPU (this corresponds to the enumerated
#          index position from :func:`list_edge_tpus`)
#        + "usb"     -- use any USB Edge TPU
#        + "usb:<N>" -- use N-th USB Edge TPU
#        + "pci"     -- use any PCIe Edge TPU
#        + "pci:<N>" -- use N-th PCIe Edge TPU
# 
#        If left as None, you cannot reliably predict which device you'll get.
#        So if you have multiple Edge TPUs and want to run a specific model on
#        each one, then you must specify the device.
#      delegate: A pre-loaded Edge TPU delegate object, as provided by
#        :func:`load_edgetpu_delegate`. If provided, the `device` argument
#        is ignored.
# 
#   Returns:
#      New ``tf.lite.Interpreter`` instance.
#   """
#   if delegate:
#     delegates = [delegate]
#   else:
#     delegates = [load_edgetpu_delegate({'device': device} if device else {})]
#   if isinstance(model_path_or_content, bytes):
#     return tflite.Interpreter(
#         model_content=model_path_or_content, experimental_delegates=delegates)
#   else:
#     return tflite.Interpreter(
#         model_path=model_path_or_content, experimental_delegates=delegates)


import argparse
import collections
import numpy as np
from PIL import Image
from PIL import ImageDraw
from pycoral.adapters import common
from pycoral.adapters import detect
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""An example to perform object detection with an image with added supports for smaller objects.

The following command runs this example for object detection using a
MobileNet model trained with the COCO dataset (it can detect 90 types
of objects):
```
bash examples/install_requirements.sh small_object_detection.py

python3 examples/small_object_detection.py \
  --model test_data/ssd_mobilenet_v2_coco_quant_no_nms_edgetpu.tflite \
  --label test_data/coco_labels.txt \
  --input test_data/kite_and_cold.jpg \
  --tile_size 1352x900,500x500,250x250 \
  --tile_overlap 50 \
  --score_threshold 0.5 \
  --output ${HOME}/object_detection_results.jpg
```

Note: this example demonstrate small object detection, using the method of
splitting the original image into tiles with some added overlaps in consecutive
tiles. The tile size can also be specified in multiple layers as
demonstrated on the above command. With the overlapping tiles and layers,
some object candidates may have overlapping bounding boxes. The example then
uses Non-Maximum-Suppressions to suppress the overlapping bounding boxes on the

# Path: pycoral/adapters/common.py
# def output_tensor(interpreter, i):
# def input_details(interpreter, key):
# def input_size(interpreter):
# def input_tensor(interpreter):
# def set_input(interpreter, data):
# def set_resized_input(interpreter, size, resize):
# 
# Path: pycoral/adapters/detect.py
# class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
#   def width(self):
#   def height(self):
#   def area(self):
#   def valid(self):
#   def scale(self, sx, sy):
#   def translate(self, dx, dy):
#   def map(self, f):
#   def intersect(a, b):
#   def union(a, b):
#   def iou(a, b):
# def get_objects(interpreter,
#                 score_threshold=-float('inf'),
#                 image_scale=(1.0, 1.0)):
#   def make(i):
# 
# Path: pycoral/utils/dataset.py
# def read_label_file(file_path):
#   """Reads labels from a text file and returns it as a dictionary.
# 
#   This function supports label files with the following formats:
# 
#   + Each line contains id and description separated by colon or space.
#     Example: ``0:cat`` or ``0 cat``.
#   + Each line contains a description only. The returned label id's are based on
#     the row number.
# 
#   Args:
#     file_path (str): path to the label file.
# 
#   Returns:
#     Dict of (int, string) which maps label id to description.
#   """
#   with open(file_path, 'r', encoding='utf-8') as f:
#     lines = f.readlines()
#   ret = {}
#   for row_number, content in enumerate(lines):
#     pair = re.split(r'[:\s]+', content.strip(), maxsplit=1)
#     if len(pair) == 2 and pair[0].strip().isdigit():
#       ret[int(pair[0])] = pair[1].strip()
#     else:
#       ret[row_number] = content.strip()
#   return ret
# 
# Path: pycoral/utils/edgetpu.py
# def make_interpreter(model_path_or_content, device=None, delegate=None):
#   """Creates a new ``tf.lite.Interpreter`` instance using the given model.
# 
#   **Note:** If you have multiple Edge TPUs, you should always specify the
#   ``device`` argument.
# 
#   Args:
#      model_path_or_content (str or bytes): `str` object is interpreted as
#        model path, `bytes` object is interpreted as model content.
#      device (str): The Edge TPU device you want:
# 
#        + None      -- use any Edge TPU (this is the default)
#        + ":<N>"    -- use N-th Edge TPU (this corresponds to the enumerated
#          index position from :func:`list_edge_tpus`)
#        + "usb"     -- use any USB Edge TPU
#        + "usb:<N>" -- use N-th USB Edge TPU
#        + "pci"     -- use any PCIe Edge TPU
#        + "pci:<N>" -- use N-th PCIe Edge TPU
# 
#        If left as None, you cannot reliably predict which device you'll get.
#        So if you have multiple Edge TPUs and want to run a specific model on
#        each one, then you must specify the device.
#      delegate: A pre-loaded Edge TPU delegate object, as provided by
#        :func:`load_edgetpu_delegate`. If provided, the `device` argument
#        is ignored.
# 
#   Returns:
#      New ``tf.lite.Interpreter`` instance.
#   """
#   if delegate:
#     delegates = [delegate]
#   else:
#     delegates = [load_edgetpu_delegate({'device': device} if device else {})]
#   if isinstance(model_path_or_content, bytes):
#     return tflite.Interpreter(
#         model_content=model_path_or_content, experimental_delegates=delegates)
#   else:
#     return tflite.Interpreter(
#         model_path=model_path_or_content, experimental_delegates=delegates)


# Path: examples/small_object_detection.py
import argparse
import collections
import numpy as np
from PIL import Image
from PIL import ImageDraw
from pycoral.adapters import common
from pycoral.adapters import detect
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter

#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""An example to perform object detection with an image with added supports for smaller objects.

The following command runs this example for object detection using a
MobileNet model trained with the COCO dataset (it can detect 90 types
of objects):
```
bash examples/install_requirements.sh small_object_detection.py

python3 examples/small_object_detection.py \
  --model test_data/ssd_mobilenet_v2_coco_quant_no_nms_edgetpu.tflite \
  --label test_data/coco_labels.txt \
  --input test_data/kite_and_cold.jpg \
  --tile_size 1352x900,500x500,250x250 \
  --tile_overlap 50 \
  --score_threshold 0.5 \
  --output ${HOME}/object_detection_results.jpg
```

Note: this example demonstrate small object detection, using the method of
splitting the original image into tiles with some added overlaps in consecutive
tiles. The tile size can also be specified in multiple layers as
demonstrated on the above command. With the overlapping tiles and layers,
some object candidates may have overlapping bounding boxes. The example then
uses Non-Maximum-Suppressions to suppress the overlapping bounding boxes on the

same objects. It then saves the result of the given image at the location