Object Detection in a few lines of code

Daniel Boadzie
Analytics Vidhya
Published in
4 min readFeb 26, 2020

Object detection is a crucial part of computer vision. This fact can be seen in its numerous applications like vision for Autonomous Cars, Face Detection in phones, species counting and tracking in Wildlife Conservation, etc. Building an object detection model, however, is far from easy. For seasoned machine learning engineers, it means several lines of code using the Tensorflow Object Detection API or a similar tool for other frameworks.

On the other hand, you can also use one of several pre-trained models(like YOLOV3) for your custom task through Transfer Learning. There are other Object Detection platform providers like CustomVision.ai, Google’s Cloud AutoML, among others, who provides easy-to-use, one-click solutions for most business needs.

Detecto; the new kid

There is, however, a new kid in this space that is both open source and easy to use for object detection. The new kid is called Detecto. The following summary from the website clearly defines what Detecto is;

“Detecto is a Python package that allows you to build fully-functioning computer vision and object detection models with just 5 lines of code. Inference on still images and videos, transfer learning on custom datasets, and serialization of models to files are just a few of Detecto’s features”

Detection has cool features that is worth discussing. So, in this article and several that will come later, we are going to build a simple object detector in a few lines of code using Detecto and we will deploy it using the Streamlit framework.

Before we start though, let’s define what Object Detection is all about. Object Detection involves the localization and classification of an object in an image(and video). That means it helps us to locate the exact position of an object and tells us the class the object belongs to. Of course, it can also locate and classify multiple objects in an image/video. That sounds cool, right? I know!

Installing dependencies

Let’s start by creating a folder called object-detector(You can call it whatever you want). Next, create a virtual environment using the python3 venv with the following command within the folder

$ python3 -m venv detecto

Now, activate your virtual environment with the command

$ source detecto/bin/activate # if you are running on Linux or Mac$ detecto\Scripts\activate # for windows users

Now that you are in the virtual environment, install your dependencies using the following commands

pip install detectopip install streamlitpip install matplotlibpip install pillow
# You can combine all that by running:pip install detecto streamlit matplotlib pillow

Creating our Modules and UI

Now have successfully installed our dependencies, lets get to the main work. First, create a file called app.py to hold the interface of the app. Then add the following code;

import streamlit as stfrom PIL import Imagefrom predict import cat_vs_dog, detecto_mimport matplotlib.pyplot as pltdef main():     st.markdown(“# Object Dector”)     st.markdown(“#### A Simple Object detector using Detecto”)     st.markdown(“ — -”)     # getting the image from the user     uploaded_file = st.file_uploader(“Choose an pic…”, type=”jpg”)     if uploaded_file is not None:          st.image(uploaded_file, caption=’Your Image.’,         use_column_width=True)          st.success(“#### And taraaaaa!”)          result = detecto_m(uploaded_file) # passing image to our function         result = plt.plot() # plotting the result         st.pyplot(result)         st.balloons() #some fun ballons# to run our moduleif __name__==’__main__’:         main()

The following code begins by importing the libraries we will be using. Streamlit has a lot of cool prebuilt UI components that we can easily use in our app like the file uploader, markdown output, the cool balloons and the image components for showing images.

Creating our Artificial Brain

We also imported a function called detecto_m() from the predict.py module. Now let’s create this module and function.

# the imports
from detecto import core, utils, visualize​
def detecto_m(pic): image = utils.read_image(pic) model = core.Model() labels, boxes, scores = model.predict_top(image) result = visualize.show_labeled_image(image, boxes, labels) return result

In just 5 lines of code we have a pretty slick object detection capable of detection 80 object classes. How cool is that? Pretty dope right?

Running the App

We can run our app with the following command in the terminal

$ streamlit run app.py

And taraaaa you will see something like the following screen-shot at this URL http://localhost:8501/ in your default browser.

Detecto is pretty slick and hence deserves your attention. Let’s see what you build with this awesome package.

--

--

Daniel Boadzie
Analytics Vidhya

Data scientist | AI Engineer |Software Engineering|Trainer|Svelte Entusiast. Find out more about my me here https://www.linkedin.com/in/boadzie/