Skip to content

Instantly share code, notes, and snippets.

@ronoaldo
Last active December 23, 2016 12:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronoaldo/71009b7b91684fecb367 to your computer and use it in GitHub Desktop.
Save ronoaldo/71009b7b91684fecb367 to your computer and use it in GitHub Desktop.
Go + SWIG + Open CV 2.4
package facedetect
// #cgo CXXFLAGS: -std=c++11
// #cgo pkg-config: opencv
import "C"
%module facedetect
%{
#include "opencv2/opencv.hpp"
#include "util.hpp"
%}
%include <std_string.i>
%include <std_vector.i>
namespace cv {
#define CV_8U 0
#define CV_BGR2GRAY 6
#define CV_HAAR_SCALE_IMAGE 2
#define CV_LOAD_IMAGE_GRAYSCALE 0
class Mat {
public:
int flags;
int dims;
int rows, cols;
};
class Rect {
public:
int x, y, width, height;
};
class Size2i {
public:
Size2i(int width, int height);
int area();
int width;
int height;
};
class CascadeClassifier {
public:
bool load(
const std::string& filename);
void detectMultiScale(
const Mat& image,
std::vector<Rect>& objects,
double scaleFactor,
int minNeightbors,
int flags,
Size2i minSize,
Size2i maxSize);
};
Mat imdecode(Mat buf, int flags);
Mat imread(const std::string filename, int flags);
void cvtColor(Mat in, Mat out, int code, int destChanels);
}
namespace std {
%template(RectSlice) vector<cv::Rect>;
}
cv::Mat MatFromString(const std::string& buff);
package facedetect
import (
"io/ioutil"
"testing"
)
var HaarCascadeFrontalFacePath = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml"
func TestObjDetect(t *testing.T) {
b, err := ioutil.ReadFile("lena.jpg")
if err != nil {
t.Fatal(err)
}
m := MatFromString(string(b))
t.Logf("Raw pixels -> dims: %d, rows: %d, cols: %d", m.GetDims(), m.GetRows(), m.GetCols())
m = Imdecode(m, 0|CV_LOAD_IMAGE_GRAYSCALE)
t.Logf("Loaded image: Mat -> dims: %d, rows: %d, cols: %d", m.GetDims(), m.GetRows(), m.GetCols())
if m.GetRows() == 0 || m.GetCols() == 0 {
t.Errorf("Unable to decode image.")
}
c := NewCascadeClassifier()
t.Logf("Cascade loaded? %v", c.Load(HaarCascadeFrontalFacePath))
rect := NewRectSlice()
t.Logf("%#t", rect)
c.DetectMultiScale(m, rect, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, NewSize2i(1, 1), NewSize2i(0, 0))
t.Logf("Detected faces: %v", rect.Size())
for i := int64(0); i < rect.Size(); i++ {
rect := rect.Get(int(i))
t.Logf("Detected face position: (%d, %d), w=%d, h=%d",
rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight())
}
}
=== RUN TestObjDetect
--- PASS: TestObjDetect (0.51s)
facedetect_test.go:16: Raw pixels -> dims: 2, rows: 91814, cols: 1
facedetect_test.go:20: Loaded image: Mat -> dims: 2, rows: 512, cols: 512
facedetect_test.go:26: Cascade loaded? true
facedetect_test.go:29: %!t(facedetect.SwigcptrRectSlice=34918736)
facedetect_test.go:31: Detected faces: 1
facedetect_test.go:35: Detected face position: (221, 204), w=170, h=170
PASS
ok ronoaldo.gopkg.net/imgproc/facedetect 0.540s
#include "opencv2/core/core.hpp"
cv::Mat MatFromString(const std::string& data) {
std::vector<char> vectordata(data.begin(), data.end());
cv::Mat data_mat(vectordata,true);
return data_mat;
}
#include "opencv2/core/core.hpp"
cv::Mat MatFromString(const std::string& data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment