Object Detection

Max Kleiner
6 min readJul 31, 2020

////////////////////////////////////////////////////////////////////////////

Machine Learning VIII

This tutorial explains how to implement object detection in Python using the ImageAI library with the help of TensorFlow and some image examples.

____________________________________________________________________________

maXbox Starter 75 — Object Detection

From Document to Recognition?

Detect the Rect!

This tutor puts a trip to the kingdom of object recognition with computer vision knowledge and an image classifier.

Object detection has been witnessing a rapid revolutionary change in some fields of computer vision. Its involvement in the combination of object classification as well as object recognition makes it one of the most challenging topics in the domain of machine learning & vision.

First we need a library with modules. ImageAI is a Python library built to empower developers to build applications and systems with self-contained deep learning and Computer Vision capabilities using a few lines of straight forward code. But to use ImageAI you need to install a few dependencies namely:

  • TensorFlow
  • OpenCV
  • Keras and ImageAI itself to install with $ pip3
    install imageAI

Now download the TinyYOLOv3 model file (33.9 MB) that contains a pretrained classification model that will be used for object detection:

https://sourceforge.net/projects/maxbox/files/Examples/EKON/EKON24/ImageDetector/yolo-tiny.h5/download

Then we need 3 necessary folders.

  • input\
  • models\yolo-tiny.h5
  • output

Now put an image for detection in the input folder, for example: teaching.jpg

Open now your preferred text editor for writing Python code (in my case maXbox) and create a new file detector.py or some valid file name.

In line 2 we import ObjectDetection class from the ImageAI library.

from imageai.Detection import ObjectDetection

As the next thing we create an instance of the class ObjectDetection, as shown in line 5 above:

detector = ObjectDetection()

It goes on with the declaration of the previous created paths:

model_path = “./models/yolo-tiny.h5”

input_path = “./input/teaching.jpg”

output_path = “./output/the_newimage.jpg”

In this tutorial, as I mentioned we’ll be using the pre-trained TinyYOLOv3 model, so I use the setModelTypeAsTinyYOLOv3() function to load our model:

#using the pre-trained TinyYOLOv3 model,

detector.setModelTypeAsTinyYOLOv3()

detector.setModelPath(model_path)

#loads model from path specified above using the setModelPath() class method.

detector.loadModel()

To detect only some of the objects above, I will need to call the CustomObjects method and set the name of the object(s) we want to detect to through. The rest are False by default. In our example, we detect customized only person, laptop and bottle. The boat is to test some negative test (maybe it find some message in the bottle with a boat in it ;-)).

custom= detector.CustomObjects(person=True,boat=True,laptop=True,bottle=True)

detections = detector.detectCustomObjectsFromImage(custom_objects=custom, \

input_image=input_path, output_image_path=output_path,\

minimum_percentage_probability=10)

Another reason for the custom instance is that I can define the threshold to find things. Unlike the normal detectObjectsFromImage() function, this needs an extra parameter which is “custom_object” which accepts the dictionary returned by the CustomObjects() function. In the sample below, we set the detection function to report only detections we want:

for eachItem in detections:

print(eachItem[“name”] , “ : “, eachItem[“percentage_probability”])

laptop : 57.53162503242493

bottle : 10.687477886676788

bottle : 11.373373866081238

person : 11.838557571172714

person : 12.098842114210129

person : 15.951324999332428

person : 31.1357319355011

person : 98.0242371559143

image detector compute ends…

With the parameter minimum_percentage_probability=30 it could not find the 2 bottles, and yes we got an output!:

So we get 8 objects with color frames and corresponding probability. The 2 bottles by lila color and astonishing the yellow frame is the laptop. Funny but really effective!

This is the function (detectCustomObjectsFromImage) that performs object detection task after the model as loaded. It can be called many times to detect objects in any number of images.

Script detector2.py

# ImageAI is a Python library built to empower Computer Vision

from imageai.Detection import ObjectDetection

#Using TensorFlow backend.

detector = ObjectDetection()

model_path = “./models/yolo-tiny.h5”

input_path = “./input/teaching.jpg”

output_path = “./output/teachwseenewimage2345.jpg”

#using the pre-trained TinyYOLOv3 model,

detector.setModelTypeAsTinyYOLOv3()

detector.setModelPath(model_path)

detector.loadModel()

#detection = detector.detectObjectsFromImage(input_image=input_path, \

# output_image_path=output_path)

custom= detector.CustomObjects(person=True,boat=True, laptop=True,bottle=True)

detections = detector.detectCustomObjectsFromImage(custom_objects=custom, \

input_image=input_path, output_image_path=output_path,\

minimum_percentage_probability=10)

for eachItem in detections:

print(eachItem[“name”] , “ : “, eachItem[“percentage_probability”])

print(‘image detector compute ends…’)

https://stackabuse.com/object-detection-with-imageai-in-python/

https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo-tiny.h5

https://imageai.readthedocs.io/en/latest/detection/index.html

There are 80 possible objects that you can detect with the ObjectDetection class, and they are as seen below (not ordered).

person, bicycle, car, motorcycle, airplane,

bus, train, truck, boat, traffic light, fire hydrant, stop_sign,

parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot dog, pizza, donot, cake, chair, couch, potted plant, bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair dryer, toothbrush.

To detect only some of the objects above, you will need to call the CustomObjects function and set the name of the object(s) you want to detect.

[gallery ids=”1303,1304" type=”rectangular”]

Trains and Street

[gallery ids=”1305,1306" type=”rectangular”]

Note:

Histogram of oriented gradients (HOG) is basically a feature descriptor that is used to detect objects in image processing and other computer vision techniques. The Histogram of oriented gradients descriptor technique includes occurrences of gradient orientation in localised portions of an image, such as detection window, region of interest (ROI), among others. Advantage of HOG-like features is their simplicity, and it is easier to understand information they carry.

Appendix: See also two other classifiers

SGDClassifier LogisticRegressionCVSGDClassifier

incrementally trained logistic regression (when given parameter loss=”log”).

LogisticRegressionCV

Logistic regression with built-in cross validation

Notes:

The underlying C implementation uses a random number generator to select features when fitting the model. It is thus not uncommon, to have slightly different results for the same input data. If that happens, try with a smaller tol parameter or set random state to 0.

Mathematically, a histogram is a mapping of bins (intervals or numbers) to frequencies. More technically, it can be used to approximate a probability density function (PDF) of the underlying variable that we see later on.

The script and data can be found:

http://www.softwareschule.ch/examples/detector2.htm

https://sourceforge.net/projects/maxbox/files/Examples/EKON/EKON24/ImageDetector/

http://www.softwareschule.ch/examples/classifier_compare2confusion2.py.txt

Author: Max Kleiner

Ref:

http://www.softwareschule.ch/box.htm

https://scikit-learn.org/stable/modules/

https://realpython.com/python-histograms/

https://imageai.readthedocs.io/en/latest/detection/index.html

Doc:

https://maxbox4.wordpress.com

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Max Kleiner
Max Kleiner

Written by Max Kleiner

Max Kleiner's professional environment is in the areas of OOP, UML and coding - among other things as a trainer, developer and consultant.

No responses yet

Write a response