Coder Social home page Coder Social logo

opencv_ssd_mobilenet's Introduction

CPPND : Capstone Project

SSD MobileNet Object Detection in C++

Overview

I implemented the object detection model using OpenCV. The Deep Neural Network model I employed here is SSD(Single Shot MultiBox Detector) with MobileNet. This program reads an image file, which could be a single photo or a movie, and performs object detection, then shows the image with indicators(box for the object detected, category name, and confidence(%)). This model can detect 90 categories of objects (listed in model/object_detection_classes_coco.txt).

alt sweets

alt school zone

I was interested in making programs in which Deep Learning techinques are used by C++, so I decided to build this project as my capstone project(free theme) in C++ Nanodegree at Udacity. After experiments with prototypes, I realized performing object detection through the network would be computationally expensive, which made the movie stop at every frame to play and made it very slow. To solve this problem, I created a thread which execute object detection in an independent time order from the main thread. The most challenging part was to get the result of detection and draw it on the right frame without interupting the movie.

Structure

Outline

This program firstly takes command line options specified by the user and sets them to inner variables.

It launches a thread which reads frames of the movie from the image file (if it's a photo, just one frame). In this thread, frames being read are sent to "image queue", as well as to "detection queue" once in a certain number of frames. This happens in Graphic class.

Then the other thread is launched in SSDModel class, which obtains an image data from "detection queue" and performs object detection. The result is stored in a queue inside the class.

After, in the main thread, image data in "image queue" is retrieved one by one. In that loop, once in a certain number of frames the result of the detection is updated by popping from the queue in SSDModel. The result of the detection is drawn on the image data. The image is shown in a window.

Files and Classes

  • main.cpp: Includes main() function.

    • Takes command line options and sets parameters into inner variables.
    • Creates image_queue and detection_queue, creates SSDModel object and Graphic object, and calls functions in both objects which launch threads.
    • In a loop, it reads image data from the queue, gets the result of detection, draws it on the image and show.
  • Graphic.h Graphic.cpp: Defines Graphic class.

    • Launchs a thread which reads the image file
    • Draws the result of detection on the image
    • Stores information about the image
  • SSDModel.h SSDModel.cpp : Defines SSDModel class.

    • Loads the DNN model
    • Launchs a thread which performs object detection
    • Stores the result of detection in a queue, and retrieves it
  • MessageQueue.h : Define MessageQueue class.

    • Holds a queue, and provides functions to send and receive frames using the queue.
    • Provides functions which sends and receives the total number of frames sent

Contents

This repository contains:

  • src/ : Source files listed above

  • model/ : Files for the DNN model

    • SSD MobileNet model file : frozen_inference_graph.pb (download ssd_mobilenet_v2_coco from here)
    • SSD MobileNet config file : ssd_mobilenet_v2_coco_2018_03_29.pbtxt (download from here)
    • class file : object_detection_classes_coco.txt (download from here)
  • images/ : Sample photos and videos to test the program

  • result/ : Examples of output images

  • CMakeLists.txt : cmake configuration file

  • README.md : This file

Requirements

Build

In the root directory (this repository), execute the command below:

  1. mkdir build
  2. cd build
  3. cmake ..
  4. make

The executable(ssd_obj_detect) is created in the current directory(build).

Run

In build directory, run the executable like below:

./ssd_obj_detect [options] <image file>

This reads an image file and show the result of object detection. <image file> should be a path to the file you want to input.

Options

  • -c : specifies confidence threshold between 0 and 1.0. If omitted, default value is 0.5. (example: -c=0.3)
  • -n : specifies the threshold used for Non-max Suppression between 0 and 1.0. If omitted, default value is 0.5. (example: -n=0.7)
  • -h -? --help --usage: Show usage.

Example

./ssd_obj_detect -c=0.3 ../images/bunnings.JPG

Reference

This program is inspired by this sample implementation:

https://github.com/opencv/opencv/blob/master/samples/dnn/object_detection.cpp

opencv_ssd_mobilenet's People

Contributors

chiekon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.