Object detection under 20 lines
Tags: machinelearning, python, codenewbie
Published: 2020-11-05 18:59:33

So you have been fascinated by what Computer Vision can do. Most of the people consider this hard and full of math. This also causes many people lose their interest in the subject. So I have set out to make Computer Vision easier than ever before.
Well, I present You Visionlib. A library which makes Computer Vision as simple as possible.
You can check out the GitHub page [HERE](https://github.com/ashwinvin/visionlib)

# So lets get started
- Download and install [Python3](https://python.org)from the official website.

- Install Visionlib and its dependencies
{% gist https://gist.github.com/ashwinvin/9122b008caba04067ca596e891e0da34
%}

This should install the Dependencies. Now install Visionlib by
{% gist https://gist.github.com/ashwinvin/f72a7eee21866079724597995c447e87 %}

Great, now that you have installed Visionlib, its time to write some code.

## CODING TIME

Create a file and paste this code. Save the code with any name but it should end with ‘.py’
{% gist https://gist.github.com/ashwinvin/55b141205540b160933474ccadd3c9f0
%}

![Before Detection](https://miro.medium.com/max/700/1*7UXnllRtJUaUcMvOx2pm1g.jpeg)
![After Detection](https://miro.medium.com/max/700/1*TC-nx5o6fpXgTz-G8E6cSQ.jpeg)

Only **20** lines of code is required for Object Detection. Never Been This Easy Right ?

Now let me explain what is actually happening. Lets break down the code into little pieces.

```python
from visionlib.object.detection.detection import ODetection                      
import argparse                       
import cv2
```

The first line import’s the visionlib library’s object detection module. The second line import’s argparse which parses the command-line options.Lastly the third line import’s the opencv which is an image processing library.

```python
detector = ODetection()                      
parser = argparse.ArgumentParser()                       
parser.add_argument("img_path", help="Path to image")                       
parser.add_argument("--model", help="The model to use for detecting", dest="model")                       
parser.add_argument("--enable-gpu", help="Set to true to enable GPU support",dest="enable_gpu", default=False,type=bool)
```

This snippet of code initializes the command-line argument parser aka argparse.

```python
detector = ODetection()
detector.set_detector(args.model)
```

This is where everything is initialized. The first line initializes the class. The second sets the detector to use. Still remember argparse, we can set the detector when start it through command-line.

```python
box, label, conf = detector.detect_objects(img, enable_gpu=args.enable_gpu)
```

This is where the magic happens . This is the line where object detection happens. We pass in the image and a bool specifying whether we want 
to use GPU. After detection we get three lists , the bounding box, the detected class for the object and the confidence for the object.

```python
dimg = detector.draw_bbox(img, box, label, conf)
```

This line does the hard work of drawing the bounding boxes ,the labels and the confidence. After drawing we get the image with drawing.
```python
cv2.imshow("Object Detection using Visionlib", dimg)                       
cv2.waitkey(0)
```

Don’t we want display our detected images. Of course we do want to display the image. These two lines help us display the image.

This is just the beginning. With Visionlib, we can do more than object detection. It can also do Face Tracking, Facial Keypoint Detection, Gender Prediction and Object Classification. As the time passes, it would try its best to become a general purpose Computer Vision Library.For more examples and documentation visit the Github Page

{% github ashwinvin/visionlib %}

Have any doubts or suggestions.. Join our [discord server](https://discord.gg/bY9Ehrv) to talk with us

Do share your comments down below!!