shlok bhadani
shlok bhadani

Reputation: 19

Is it better to use a hogDescriptor or a haar Cascade for any color and human detection work?

My project is-

  1. There is a camera looking at a group of people.
  2. It will detect the people and find out the one wearing a blue cap
  3. It will then tell the camera to move a certain amount so that a laser placed above the camera alines with the person wearing the blue cap.
  4. All I need to find out is which one is faster HOGDescriptor or HaarCascade for this project.
  5. I am using c++ for this project but if you want you can send the code in python considering it doesn't use numpy

My needs-I just want to find out weather HaarCascade or HOGDescriptor will give me better performance and quality for the camera I have (720p) & Will the speed of the program (more fps in camera) increase if I bump the camera up to a 1440p

Keep in mind that my code might look shaby but this is the work for some IT Fair and they may check my code later so I'll fix it later.

I tried using opencv and the camera works slowly(720p) and it doesn't the detect the face of the person when his head is tilted a little upwards. If there is a way to make the haar Cascade Classifier faster than kindly tell or should I learn HOGDescriptor for this project.

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <vector>

using namespace cv;

Scalar lower(110, 50, 50);
Scalar upper(130, 255, 255);

int returnWhitePixelCount(Mat image);
int returnWhitestImage(std::vector<Mat> faceImages);

int main()
{
    Mat img, frame, croppedImage;
    std::string xmlFile = "Resources/haarcascade_frontalface_default.xml";
    std::vector<Rect> facePaths;
    std::vector<Mat> imagePaths;

    CascadeClassifier faceCascade;
    VideoCapture cap(0);
    faceCascade.load(xmlFile);

    if (faceCascade.empty()) std::cout << "no xml file" << std::endl;

    while (true) {
        cap >> img;
        resize(img, frame, Size(600, 450));

        faceCascade.detectMultiScale(frame, facePaths, 1.1, 9);

        imagePaths.clear();

        for (int i = 0; i < facePaths.size(); i++) {
            facePaths[i].x -= 50;
            facePaths[i].y -= 50;
            facePaths[i].width += 50;
            facePaths[i].height += 50;
            Mat croppedImage = img(facePaths[i]);
            imagePaths.push_back(croppedImage);
        }

        int correctImageCount = returnWhitestImage(imagePaths);
        returnWhitestImage(imagePaths);

        for (int i = 0; i < facePaths.size(); i++) {
            rectangle(frame, facePaths[i].tl(), facePaths[i].br(), Scalar(255, 255, 0), 3);
            if (i == correctImageCount) {
                rectangle(frame, facePaths[i].tl(), facePaths[i].br(), Scalar(0, 255, 255), 3);
            }
        }

        imshow("frame", frame);

        if (waitKey(1) == 27) break;
    }

    return 0;
}

int returnWhitePixelCount(Mat image) {
    Mat imgHSV, mask;
    cvtColor(image, imgHSV, COLOR_BGR2HSV);
    inRange(imgHSV, lower, upper, mask);
    static int whitePixelCount = 0;

    for (int i = 0; i < image.rows; i++) {
        for (int j = 0; j < image.cols; j++) {
            int x = image.at<uchar>(i, j);

            if (x == 255) whitePixelCount++;
        }
    }

    return whitePixelCount;
}

int returnWhitestImage(std::vector<Mat> faceImages) {
    std::vector<int> whitePixelCounts;
    int highestValue = 0;

    for (int i = 0; i < faceImages.size(); i++) {
        int pixelCount = returnWhitePixelCount(faceImages[i]);
        whitePixelCounts.push_back(pixelCount);
    };

    for (int i = 0; i < whitePixelCounts.size(); i++) {
        if (whitePixelCounts[i] > whitePixelCounts[highestValue]) highestValue = i;
    };

    std::cout << highestValue << ", " << faceImages.size() << std::endl;

    return highestValue;
}

This is the code that I have written in c++ for the project. It works properly but I just need to find weather HaarCascade or HOGDescriptor will do it faster and properly.

Upvotes: 1

Views: 48

Answers (0)

Related Questions