Skip to content

obj_detection

Erik Handeland edited this page May 3, 2022 · 2 revisions

Getting Started

Install

There are a view dependecies that can be tricky to install. Tensorflow lite is one of them, which currently does not support Python 3.10+. Tested on linux, pythom 3.9

# Requires the latest pip
pip install --upgrade pip

#install cv2 for image processing
pip install opencv-python
pip install numpy

# Install tensorflow
pip install tensorflow
pip install tflite_support>=0.3.0

# install local package objdetection must clone and cd into the repo
pip install -e .

Usage

Args
  • IMG_PATH #(REQUIRED) Path to .png or .jpg image
  • MODEL_NAME #(REQUIRED) Name of one of the models listed in the `obj_detection/models` directory
  • MIN_CONF_LEVEL #(OPTIONAL) minimum confidence level to accept (float 0-1), default 0.5
  • GRAPH_NAME #(OPTIONAL) name of .tflite file, default detect.tflite
  • LABELMAP_NAME #(OPTIONAL) name of label file, default labelmap.txt
  • SAVED_IMG_PATH #(OPTIONAL) Where or not to save image with detection boxes, default null
  • COORDS #(OPTIONAL) Where or not to return coordinates of detect object, default False
  from obj_detection import objDetection
  
  result = objDetection(model_name, img_path)
  print("Number of vehicles: ", result["vehicles"])
  print("Number of pedestrians: ", result["pedestrians"])
  print("Number of objects: ", result["objects"])
  print("Error: ", result["error"])

Using a custom model

To train a custom model checkout the train a model wiki page. Once you have your model trained you'll need to put it int he following format:

MODEL_NAME/
|--- detect.tflite # must be named detect otherwise you'll need to pass the name via GRAPH_NAME
|--- labelmap.txt # Can be omitted if using a TFLite file that contains metadata.
|--- info.txt # Optional - model details such as source, etc.

Your model should include the following output_details:
[0] - Bounding box coordinates of detected objects
[1] - Class index of detected objects
[2] - Confidence of detected objects

Design Details

Parameters

Parameters
  • IMG_PATH #(REQUIRED) Path to .png or .jpg image
  • MODEL_NAME #(REQUIRED) Name of one of the models listed in the `obj_detection/models` directory
  • MIN_CONF_LEVEL #(OPTIONAL) minimum confidence level to accept (float 0-1), default 0.5
  • GRAPH_NAME #(OPTIONAL) name of .tflite file, default detect.tflite
  • LABELMAP_NAME #(OPTIONAL) name of label file, default labelmap.txt
  • SAVED_IMG_PATH #(OPTIONAL) Where or not to save image with detection boxes, default null
  • COORDS #(OPTIONAL) Where or not to return coordinates of detect object, default False

Returns

Returns a object with the following fields:
"vehicles": 3, # total vehicles detected
"pedestrians": 0, # total pedestrians detected
"confidence-threshold": 0.5 # min confidence level allowed
"objects": [] # list of objects detected
   "name": car # Name of object
   "confidence": 65.2 # confidence for this object
   "coord": {} # coordinates of detection box
"error": # Empty if no error was found otherwise contains error code

Sample responce:

{
    "vehicles": 3, # total vehicles detected
    "pedestrians": 0, # total pedestrians detected 
    "confidence-threshold": 0.5 # min confidence level allowed
    "objects": [ # list of objects detected
         {'name': 'car',  # obj name: car, person, ect
         'confidence': 0.65625,  # confidence for this object
         'coord': {'p1': {'x': 165, 'y': 413},  # coordinates, two points, bottom left, top right
                      'p2': {'x': 794, 'y': 646}}
          } ...],
 }

Errors

"No labelmap found"
No labelmap in metadata and no labelmap.txt found, check that your have label data
"Invalid model-metadata path"
detect.tflite was not found in model folder. Check that your calling the right model or that you've reinstalled the package after adding a custom model
"Invalid model-metadata output details..."
Your probably using TFLite Model meant for JS or Dart

Clone this wiki locally