Skip to content

Instantly share code, notes, and snippets.

@ollewelin
Created September 14, 2016 04:33
Show Gist options
  • Save ollewelin/32904405e3585e66faba1f19e6b2f910 to your computer and use it in GitHub Desktop.
Save ollewelin/32904405e3585e66faba1f19e6b2f910 to your computer and use it in GitHub Desktop.
main.c
//File: simpletest_raspicam_cv.cpp
//Will test Trained Cascade Object detection
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <stdio.h>
#include <raspicam/raspicam_cv.h>
#include <opencv2/opencv.hpp>
#include "gpio_test.h"
#include <bcm2835.h>
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
//default capture width and height
//const int FRAME_WIDTH = 640;
//const int FRAME_HEIGHT = 480;
const int FRAME_WIDTH = 1280;
const int FRAME_HEIGHT = 960;
using namespace std;
using namespace cv;
Mat ReferenceFrame;
const string WindowName = "Object Detection example";
class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
{
public:
CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
IDetector(),
Detector(detector)
{
CV_Assert(detector);
}
void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)
{
Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
}
virtual ~CascadeDetectorAdapter()
{}
private:
CascadeDetectorAdapter();
cv::Ptr<cv::CascadeClassifier> Detector;
};
int main(int , char** )
{
int test = 0;
test = gpio_init();
namedWindow(WindowName);
Mat ReferenceFrame;
Mat GrayFrame;
vector<Rect> Faces;
raspicam::RaspiCam_Cv Camera;
Camera.set( CV_CAP_PROP_FORMAT, CV_8UC3 );
//Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
Camera.set( CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
Camera.set( CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
//Open camera
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
for(int l=0;l<2;l++)
{
Camera.grab();
Camera.retrieve ( ReferenceFrame);
imshow(WindowName, ReferenceFrame);
waitKey(1);
}
ReferenceFrame = GrayFrame;
std::string cascadeFrontalfilename = "/home/pi/OlleOpenCV2_3/cascade_obj.xml";
cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade);
cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade);
DetectionBasedTracker::Parameters params;
DetectionBasedTracker Detector(MainDetector, TrackingDetector, params);
if (!Detector.run())
{
printf("Error: Detector initialization failed\n");
return 2;
}
int antal =0;
for(int i= 0;i<1000;i++)
{
// VideoStream >> ReferenceFrame;
i=0;//Run forever comment out this for a limited run time
// cvtColor(image,HSV,COLOR_BGR2HSV);
Camera.grab();
Camera.retrieve ( ReferenceFrame);
cvtColor(ReferenceFrame, GrayFrame, COLOR_RGB2GRAY);
Detector.process(GrayFrame);
Detector.getObjects(Faces);
int thickness=5;
for (size_t i = 0; i < Faces.size(); i++)
{
rectangle(ReferenceFrame, Faces[i], Scalar(0,255,0), thickness);
printf("Object find X: %d", Faces[i].x);
printf(" Y: %d", Faces[i].y);
printf(" Height: %d", Faces[i].height);
printf(" Width: %d\n", Faces[i].width);
}
imshow(WindowName, ReferenceFrame);
waitKey(1);
//gpio_blink(); //Add this if you want to test GPIO pin but I have add 2x 0,5sec time delay in gpio_test.c so it will run slowly with this test GPIO In/Out example added
}
Detector.stop();
cout<<"Stop camera..."<<endl;
Camera.release();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment