Object Detection : MXNet SSD Example

Prepare

  1. cd /home/water/mxnet/example/ssd
  2. wget https://github.com/zhreshold/mxnet-ssd/releases/download/v0.6/resnet50_ssd_512_voc0712_trainval.zi
  3. unzip resnet50_ssd_512_voc0712_trainval.zip
  4. python data/demo/download_demo_images.py

Test

  1. python demo.py --gpu 0
  2. python demo.py --epoch 0 --images ./data/demo/dog.jpg --thresh 0.5
  3. python demo.py --cpu --network resnet50 --data-shape 512
  4. . . . . . .
  5. python demo.py --images ./data/demo/person.jpg --thresh 0.3
  6. python demo.py --images ./data/demo/person.jpg --network inceptionv3

Result














Matplotlib RGB & OpenCV BGR

Matplotlib RGB & OpenCV BGR

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import cv2
  4.  
  5. img = np.array([
  6.     [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
  7.     [[255, 255, 0], [255, 0, 255], [0, 255, 255]],
  8.     [[255, 255, 255], [128, 128, 128], [0, 0, 0]],
  9. ], dtype=np.uint8)
  10.  
  11. plt.imsave('img_pyplot.png', img)
  12. cv2.imwrite('img_cv2.jpg', img)


QT + OpenCV Example

Source

  1. #include <QCoreApplication>
  2.  
  3. #include <opencv2/opencv.hpp>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     //QCoreApplication a(argc, argv);
  8.  
  9.     using namespace cv;
  10.     Mat image = imread("/Users/water/Downloads/Share.jpg");
  11.     imshow("Output", image);
  12.  
  13.     waitKey(1000);
  14.  
  15.     return 0;
  16.  
  17.     //return a.exec();
  18. }

pro

  1. QT -= gui
  2.  
  3. CONFIG += c++11 console
  4. CONFIG -= app_bundle
  5.  
  6. # The following define makes your compiler emit warnings if you use
  7. # any Qt feature that has been marked deprecated (the exact warnings
  8. # depend on your compiler). Please consult the documentation of the
  9. # deprecated API in order to know how to port your code away from it.
  10. DEFINES += QT_DEPRECATED_WARNINGS
  11.  
  12. # You can also make your code fail to compile if it uses deprecated APIs.
  13. # In order to do so, uncomment the following line.
  14. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  15. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  16.  
  17. SOURCES += \
  18.         main.cpp
  19.  
  20. # Default rules for deployment.
  21. qnx: target.path = /tmp/$${TARGET}/bin
  22. else: unix:!android: target.path = /opt/$${TARGET}/bin
  23. !isEmpty(target.path): INSTALLS += target
  24.  
  25. INCLUDEPATH += /usr/local/include
  26. INCLUDEPATH += /usr/local/include/opencv
  27. INCLUDEPATH += /usr/local/include/opencv2
  28. LIBS += -L/usr/local/lib \
  29.  -lopencv_core \
  30.  -lopencv_highgui \
  31.  -lopencv_imgproc \
  32.   -lopencv_imgcodecs \

How to Build Qt From Source Codes (QT 5.12 + Visual Studio 2017)

msvc-desktop.conf

  1. C:\Qt\5.12.0\Src\qtbase\mkspecs\common\msvc-desktop.conf
  2.  
  3. QMAKE_CFLAGS_RELEASE    = $QMAKE_CFLAGS_OPTIMIZE -MT
  4. QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $QMAKE_CFLAGS_OPTIMIZE -Zi -MT
  5. QMAKE_CFLAGS_DEBUG      = -Zi -MTd

qt5vars.cmd

  1. REM Set up \Microsoft Visual Studio 2017, where arch is \c amd64, \c x86, etc.
  2. CALL "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x86
  3. SET _ROOT=C:\Qt\5.12.0\Src
  4. SET PATH=%_ROOT%\qtbase\bin;%_ROOT%\gnuwin32\bin;%PATH%
  5. REM Uncomment the below line when using a git checkout of the source repository
  6. REM SET PATH=%_ROOT%\qtrepotools\bin;%PATH%
  7. SET _ROOT=

make & make install

  1. C:\Qt\5.12.0\Src>qt5vars.cmd
  2. C:\Qt\5.12.0\Src>configure -opensource -confirm-license
  3. C:\Qt\5.12.0\Src>jom -j 8
  4. C:\Qt\5.12.0\Src>jom install

QT5 International

Create Project & Generate .ts

  1. lupdate TestInternational.pro -ts zh_CN.ts

Translate with Linguist

Save & Release to Generate .qm

Add Code to Choose Language

  1. #include "widget.h"
  2. #include <QApplication>
  3.  
  4. #include <QTranslator>
  5.  
  6. #include <QDebug>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication a(argc, argv);
  11.  
  12.     QTranslator trans;
  13.      if(trans.load(a.applicationDirPath() + "/../../../zh_CN.qm"))
  14.         a.installTranslator(&trans);
  15.  
  16.     Widget w;
  17.     w.show();
  18.  
  19.     return a.exec();
  20. }

The Result

OpenCV3 On ARM

Source Code Dir

CMake-GUI Configure

Configure/Generate

Unix Makefiles
Specify options for cross-compiling

Choose ARM – LINUX

Operating System : arm-linux
C Compilers :
C++ Compilers :
Target Root : The Cross Compiler BIN Directory

CMAKE_INSTALL_PREFIX

make & make install

  1. make
  2. make install

Maybe Need It !


<>

The END

  1. tar jcvf opencv-arm.tar.bz2 opencv-arm/

Lucas-Kanade & Farneback

LucasKanade Tracker

  1.         // Check if there are points to track
  2.         if(!trackingPoints[0].empty())
  3.         {
  4.             // Status vector to indicate whether the flow for the corresponding features has been found
  5.             vector<uchar> statusVector;
  6.  
  7.             // Error vector to indicate the error for the corresponding feature
  8.             vector<float> errorVector;
  9.  
  10.             // Check if previous image is empty
  11.             if(prevGrayImage.empty())
  12.             {
  13.                 curGrayImage.copyTo(prevGrayImage);
  14.             }
  15.  
  16.             // Calculate the optical flow using Lucas-Kanade algorithm
  17.             calcOpticalFlowPyrLK(prevGrayImage, curGrayImage, trackingPoints[0], trackingPoints[1], statusVector, errorVector, windowSize, 3, terminationCriteria, 0, 0.001);
  18.  
  19.             int count = 0;
  20.  
  21.             // Minimum distance between any two tracking points
  22.             int minDist = 7;
  23.  
  24.             for(int i=0; i < trackingPoints[1].size(); i++)
  25.             {
  26.                 if(pointTrackingFlag)
  27.                 {
  28.                     // If the new point is within 'minDist' distance from an existing point, it will not be tracked
  29.                     if(norm(currentPoint - trackingPoints[1][i]) <= minDist)
  30.                     {
  31.                         pointTrackingFlag = false;
  32.                         continue;
  33.                     }
  34.                 }
  35.  
  36.                 // Check if the status vector is good
  37.                 if(!statusVector[i])
  38.                     continue;
  39.  
  40.                 trackingPoints[1][count++] = trackingPoints[1][i];
  41.  
  42.                 // Draw a filled circle for each of the tracking points
  43.                 int radius = 8;
  44.                 int thickness = 2;
  45.                 int lineType = 8;
  46.                 circle(image, trackingPoints[1][i], radius, Scalar(0,255,0), thickness, lineType);
  47.             }
  48.  
  49.             trackingPoints[1].resize(count);
  50.         }
  51.  
  52.         // Refining the location of the feature points
  53.         if(pointTrackingFlag && trackingPoints[1].size() < maxNumPoints)
  54.         {
  55.             vector<Point2f> tempPoints;
  56.             tempPoints.push_back(currentPoint);
  57.  
  58.             // Function to refine the location of the corners to subpixel accuracy.
  59.             // Here, 'pixel' refers to the image patch of size 'windowSize' and not the actual image pixel
  60.             cornerSubPix(curGrayImage, tempPoints, windowSize, cvSize(-1,-1), terminationCriteria);
  61.  
  62.             trackingPoints[1].push_back(tempPoints[0]);
  63.             pointTrackingFlag = false;
  64.         }

Farneback Tracker

  1.         // Check if the image is valid
  2.         if(prevGray.data)
  3.         {
  4.             // Initialize parameters for the optical flow algorithm
  5.             float pyrScale = 0.5;
  6.             int numLevels = 3;
  7.             int windowSize = 15;
  8.             int numIterations = 3;
  9.             int neighborhoodSize = 5;
  10.             float stdDeviation = 1.2;
  11.  
  12.             // Calculate optical flow map using Farneback algorithm
  13.             calcOpticalFlowFarneback(prevGray, curGray, flowImage, pyrScale, numLevels, windowSize, numIterations, neighborhoodSize, stdDeviation, OPTFLOW_USE_INITIAL_FLOW);
  14.  
  15.             // Convert to 3-channel RGB
  16.             cvtColor(prevGray, flowImageGray, COLOR_GRAY2BGR);
  17.  
  18.             // Draw the optical flow map
  19.             drawOpticalFlow(flowImage, flowImageGray);
  20.  
  21.             // Display the output image
  22.             imshow(windowName, flowImageGray);
  23.         }

Other Morphological Operators

Opening

  1. Mat performOpening(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage, tempImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply morphological opening to the image using the structuring element
  19.     erode(inputImage, tempImage, element);
  20.     dilate(tempImage, outputImage, element);
  21.  
  22.     // Return the output image
  23.     return outputImage;
  24. }

Closing

  1. Mat performClosing(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage, tempImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply morphological opening to the image using the structuring element
  19.     dilate(inputImage, tempImage, element);
  20.     erode(tempImage, outputImage, element);
  21.  
  22.     // Return the output image
  23.     return outputImage;
  24. }

Morphological Gradient

  1. Mat performMorphologicalGradient(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage, tempImage1, tempImage2;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply morphological gradient to the image using the structuring element
  19.     dilate(inputImage, tempImage1, element);
  20.     erode(inputImage, tempImage2, element);
  21.  
  22.     // Return the output image
  23.     return tempImage1 - tempImage2;
  24. }

TopHat

  1. Mat performTopHat(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply top hat operation to the image using the structuring element
  19.     outputImage = inputImage - performOpening(inputImage, morphologyElement, morphologySize);
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

BlackHat

  1. Mat performBlackHat(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply black hat operation to the image using the structuring element
  19.     outputImage = performClosing(inputImage, morphologyElement, morphologySize) - inputImage;
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

Slimming/Thickening The Shapes

Erosion

  1. Mat performErosion(Mat inputImage, int erosionElement, int erosionSize)
  2. {
  3.     Mat outputImage;
  4.     int erosionType;
  5.  
  6.     if(erosionElement == 0)
  7.         erosionType = MORPH_RECT;
  8.  
  9.     else if(erosionElement == 1)
  10.         erosionType = MORPH_CROSS;
  11.  
  12.     else if(erosionElement == 2)
  13.         erosionType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(erosionType, Size(2*erosionSize + 1, 2*erosionSize + 1), Point(erosionSize, erosionSize));
  17.  
  18.     // Erode the image using the structuring element
  19.     erode(inputImage, outputImage, element);
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

Dilation

  1. Mat performDilation(Mat inputImage, int dilationElement, int dilationSize)
  2. {
  3.     Mat outputImage;
  4.     int dilationType;
  5.  
  6.     if(dilationElement == 0)
  7.         dilationType = MORPH_RECT;
  8.  
  9.     else if(dilationElement == 1)
  10.         dilationType = MORPH_CROSS;
  11.  
  12.     else if(dilationElement == 2)
  13.         dilationType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for dilation
  16.     Mat element = getStructuringElement(dilationType, Size(2*dilationSize + 1, 2*dilationSize + 1), Point(dilationSize, dilationSize));
  17.  
  18.     // Dilate the image using the structuring element
  19.     dilate(inputImage, outputImage, element);
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }