DEV Community

Cover image for Object detection under 20 lines
Ashwin Vinod
Ashwin Vinod

Posted on

Object detection under 20 lines

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

So lets get started

  • Download and install Python3from the official website.

  • Install Visionlib and its dependencies

This should install the Dependencies. Now install Visionlib by

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’

Before Detection
After Detection

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.

from visionlib.object.detection.detection import ODetection                      
import argparse                       
import cv2
Enter fullscreen mode Exit fullscreen mode

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.

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)
Enter fullscreen mode Exit fullscreen mode

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

detector = ODetection()
detector.set_detector(args.model)
Enter fullscreen mode Exit fullscreen mode

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.

box, label, conf = detector.detect_objects(img, enable_gpu=args.enable_gpu)
Enter fullscreen mode Exit fullscreen mode

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.

dimg = detector.draw_bbox(img, box, label, conf)
Enter fullscreen mode Exit fullscreen mode

This line does the hard work of drawing the bounding boxes ,the labels and the confidence. After drawing we get the image with drawing.

cv2.imshow("Object Detection using Visionlib", dimg)                       
cv2.waitkey(0)
Enter fullscreen mode Exit fullscreen mode

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 logo ashwinvin / Visionlib

A simple, easy to use, feature rich, customizable cv library

logo

Visionlib

Upload Python Package

A simple high level API made for assisting in CV-related projects.

Features

  • Track faces using

    • MTCNN module
    • Dlib hog Based detector
    • Opencv Haar cascades
    • Dnn based model
  • Predict Gender

  • Detect Objects

    • Yolo v3
    • tiny-yolo
  • Classify Images

    • VGG 16
    • Inception v3
    • Xception
  • Detect Facial Keypoints

    • MTCNN
    • Dlib 68 point shape detector

TODO

  • Find a Pre-Trained model for pose estimation and implement.
  • Implement a motion detector preferably using Opencv
  • Fix the unnecessary loading for TensorFlow

Installation

Note: Windows compatibility is not tested

Dependencies

sudo apt-get install build-essential cmake pkg-config

sudo apt-get install libx11-dev libatlas-base-dev

sudo apt-get install libgtk-3-dev libboost-python-dev

This should install Dependencies required by dlib.

pip install visionlib

This will install visionlib.

Optional

If You want to install from source git clone https://github.com/ashwinvin/Visionlib.git

cd visionlib

pip install .

Face Detection

Detecting face in an image is easy . This will return the image with bounding box and box coordinates.

from

Have any doubts or suggestions.. Join our discord server to talk with us

Do share your comments down below!!

Top comments (10)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
 
Sloan, the sloth mascot
Comment deleted
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
 
Sloan, the sloth mascot
Comment deleted
 
Sloan, the sloth mascot
Comment deleted

Some comments have been hidden by the post's author - find out more