1

I am having a bit of trouble in trying to get the opencv face detection to work in QT with my basler cam; I have tried many different approaches to get it to work, using many different sample codes online. I just can’t seem to get it to work at all; in addition the attempts I have made have lowered my frame rate.

The code I used to capture a video with the basler cam is working great, I’m just having trouble implementing the face detection part. I will paste the code I have so far for the camera and opencv below. The code does get me a few red boxes appearing now and then, but it isn’t stable. I am also getting this error

Failed to load OpenCL runtime

I’m not sure what I am doing wrong, also is there a way to implement the face detection without lowering the frame rate, as it is already slow

  #include "mainwindow.h"
#include "ui_mainwindow.h"

#include <opencv2/opencv.hpp>

#include <pylon/PylonIncludes.h>
//#include <pylon/PylonGUI.h>
//#ifdef PYLON_WIN_BUILD
//#include <pylon/PylonGUI.h>
//#endif

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include<time.h>
#include<stdlib.h>


using namespace cv;

// Namespace for using pylon objects.
using namespace Pylon;


// Namespace for using cout.
using namespace std;


static const uint32_t c_countOfImagesToGrab = 100;


cv::CascadeClassifier faceCade;

String faceCascadeName = "/usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
String FaceDetectWindow = "Face Detector Window";
String FaceDetectGrayWindow = "Face Detector Gray Window";
size_t i;

vector<Rect> faces;

  cv::Mat camFrames, grayFrames;

int main()
{


    // The exit code of the sample application.
    int exitCode = 0;

    // Automagically call PylonInitialize and PylonTerminate to ensure
    // the pylon runtime system is initialized during the lifetime of this object.
    Pylon::PylonAutoInitTerm autoInitTerm;

faceCade.load( faceCascadeName );

    CGrabResultPtr ptrGrabResult;
    namedWindow("CV_Image",WINDOW_AUTOSIZE);



        CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
          cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
           camera.Open();

           GenApi::CIntegerPtr width(camera.GetNodeMap().GetNode("Width"));
               GenApi::CIntegerPtr height(camera.GetNodeMap().GetNode("Height"));
               Mat cv_img(width->GetValue(), height->GetValue(), CV_8UC3);

               camera.StartGrabbing();
                  CPylonImage image;
                  CImageFormatConverter fc;
                   fc.OutputPixelFormat = PixelType_BGR8packed;

                   while(camera.IsGrabbing()){
                      camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded()){

    fc.Convert(image, ptrGrabResult);


                   cv_img = cv::Mat(ptrGrabResult->GetHeight(),     ptrGrabResult->GetWidth(), CV_8UC3,(uint8_t*)image.GetBuffer());





                   //cvtColor(cv_img, grayFrames, cv::COLOR_BGR2GRAY);
                    //equalizeHist(grayFrames, grayFrames);
                    faceCade.detectMultiScale(cv_img, faces, 1.1, 2, 0, Size(160, 160));




                    for (int i = 0; i < faces.size(); i++)
                  {
                        //Mat faceROI = grayFrames(faces[i]);
                        rectangle(cv_img, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35),  Scalar(0, 0, 255), 1, 1, 0);
                        Point center(faces[i].x + faces[i].width * 0.5,faces[i].y + faces[i].height * 0.5);



}


                   imshow("CV_Image",cv_img);
                   //imshow("FaceDetectGrayWindow", grayFrames);
                     waitKey(1);
                     if(waitKey(30)==27){
                                          camera.StopGrabbing();
                                    }
}

                   }


}
}

Thank you

Vijay Yadav
  • 91
  • 13
  • Did you try this?: https://stackoverflow.com/questions/25723059/failed-to-load-opencl-runtime-opencv3-python – Rama Aug 25 '17 at 16:04

1 Answers1

0

i'm not quite sure about this but detectMultiScale function works with image in cv_8u type , and as i see you are using cv_8uc3, as i know cv_8u it's 8 bit pixel with 1 channel, cv_8uc3 it's alos 8 bit but 3 channels, you need to convert your image to gray scale , i saw you did that but you comment it?!!! look at this link opencv_face_detection.

maybe that will fix your problem, and some people advice you to install opencl

sudo apt-get install ocl-icd-opencl-dev
shadow
  • 767
  • 3
  • 8
  • 20