Commit 580452c4 authored by Jeeken's avatar Jeeken
Browse files

handle command line arguments

parent 34c9e1ca
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ void ImgArmorDetectionApp::run() {
        cv::imshow("Aimed", img);

        if (cv::waitKey() == 'q') {
            cv::destroyAllWindows();
            std::cout << "quit..." << std::endl;
            break;
        }
    }
+1 −3
Original line number Diff line number Diff line
@@ -13,9 +13,7 @@ namespace ArmorDetection {
                  color(color),
                  debug(debug) {}

        virtual ~ArmorDetectionApp() {
            cv::destroyAllWindows();
        }
        virtual ~ArmorDetectionApp() {}

        virtual void run() = 0;

+4 −4
Original line number Diff line number Diff line
@@ -40,14 +40,14 @@ void Detector::hsv(const Mat &frame, Mat &lightArea, Mat &haloArea) {
    Mat hsvFrame;
    if (color == BarColor::BLUE) {
        cv::cvtColor(frame, hsvFrame, cv::COLOR_BGR2HSV);
        Scalar lowerBlueLight(90, 0, 200), upperBlueLight(120, 180, 255);
        Scalar lowerBlueLight(90, 0, 200), upperBlueLight(120, 140, 255);
        Scalar lowerBlueHalo(90, 120, 185), upperBlueHalo(120, 255, 255);
        cv::inRange(hsvFrame, lowerBlueLight, upperBlueLight, lightArea);
        cv::inRange(hsvFrame, lowerBlueHalo, upperBlueHalo, haloArea);
    } else { // For BarColor::RED
        // cheat openCV: swap Red and Blue channels implicitly
        cv::cvtColor(frame, hsvFrame, cv::COLOR_RGB2HSV);
        Scalar lowerBlueLight(100, 0, 200), upperBlueLight(130, 180, 255);
        Scalar lowerBlueLight(100, 0, 200), upperBlueLight(130, 150, 255);
        Scalar lowerBlueHalo(100, 120, 185), upperBlueHalo(130, 255, 255);
        cv::inRange(hsvFrame, lowerBlueLight, upperBlueLight, lightArea);
        cv::inRange(hsvFrame, lowerBlueHalo, upperBlueHalo, haloArea);
@@ -175,7 +175,7 @@ float squareRatio(RotatedRect light1, RotatedRect light2) {
float yDis(RotatedRect light1, RotatedRect light2) {
    float y1 = light1.center.y, y2 = light2.center.y;
    // FIXME in python: y coordinates may be negetive
    return std::fabs((y1 - y2) / std::min(y1, y2));
    return std::fabs((y1 - y2) / std::min(light1.size.height, light2.size.height));
};

bool Detector::getArmor(const std::forward_list<RotatedRect> &lights, Point &target) {
@@ -193,7 +193,7 @@ bool Detector::getArmor(const std::forward_list<RotatedRect> &lights, Point &tar
        for (size_t j = i + 1; j < jEnd; ++j) {
            auto &l1 = candidates[i], &l2 = candidates[j];
            float score = squareRatio(l1, l2) * 5.0f
                          + yDis(l1, l2) * 20.0f
                          + yDis(l1, l2) * 8.0f
                          + shapeSimilarity(l1, l2) * 3.0f
                          + parallel(l1, l2) * 1.2f;
            *(p++) = std::make_tuple(score, i, j);
+45 −15
Original line number Diff line number Diff line
#include <iostream>
#include <memory>
#include "ArmorDetectionApp.h"
#include <tclap/CmdLine.h>

typedef std::unique_ptr<ArmorDetectionApp> Ptr;
using namespace TCLAP;

int main(int argc, const char **argv) {
    CmdLine cmd("Armor Detection, Artinx CV 2018", ' ', " C++ v0.2");
    ValueArg<std::string> imgPathArg("i", "img", "Directory of images folder", false,
                                     "./img/", "/path/to/images/folder");
    ValueArg<std::string> videoPathArg("v", "video", "Path of video file", false,
                                       "armor.mp4", "/path/to/video");
    cmd.xorAdd(imgPathArg, videoPathArg);
    ValueArg<char> colorArg("c", "color", "Color of armor, 'b' for blue, 'r' for red", true, 'b', "color", cmd);
    SwitchArg debugArg("d", "debug", "Provide debug information", cmd, false);

int main(int argc, char **argv) {
    Ptr app;

    try {
//        app = Ptr(new ImgArmorDetectionApp(
//                BarColor::BLUE,
//                cv::Size(640, 480),
//                "/home/jeeken/Pictures/blue",
//                "jpg",
//                true
//        ));
        cmd.parse(argc, argv);

        char color = colorArg.getValue();
        BarColor armorColor;
        if (color == 'r')
            armorColor = BarColor::RED;
        else if (color == 'b')
            armorColor = BarColor::BLUE;
        else
            throw ArgException("invalid color", "c");

        if (videoPathArg.isSet()) {
            app = Ptr(new VideoArmorDetectionApp(
                BarColor::BLUE,
                    armorColor,
                    cv::Size(640, 480),
                "/home/jeeken/Videos/live_blue.avi",
                false
                    videoPathArg.getValue(),
                    debugArg.getValue()
            ));
        } else if (imgPathArg.isSet()) {
            app = Ptr(new ImgArmorDetectionApp(
                    armorColor,
                    cv::Size(640, 480),
                    imgPathArg.getValue(),
                    "jpg",  // TODO
                    debugArg.getValue()
            ));
        }

        app->run();

    } catch (ArgException &e) {
        std::cerr << "error: " << e.error() << " for " << e.argId() << std::endl;
    } catch (std::invalid_argument &e) {
        std::cerr << "invalid path: " << e.what() << std::endl;
    } catch (std::exception &e) {
        std::cerr << "Sorry: " << e.what() << std::endl;
        std::cerr << "runtime error: " << e.what() << std::endl;
    }

    return 0;