Created
October 28, 2015 08:19
-
-
Save alexanderkoumis/25daeec3fff3c7e1da0d to your computer and use it in GitHub Desktop.
Python OpenCV C++ Wrapper (CPython)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/contrib/contrib.hpp> | |
#include <opencv2/objdetect/objdetect.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
int find_face(const char* cascade_file, const char* image_file) | |
{ | |
cv::CascadeClassifier cascade; | |
if(cascade.load(std::string(cascade_file))) | |
{ | |
cv::Mat frame = cv::imread(image_file); | |
cv::Mat frame_gray; | |
std::vector<cv::Rect> faces; | |
cv::cvtColor(frame, frame_gray, CV_BGR2GRAY); | |
cv::equalizeHist(frame_gray,frame_gray); | |
cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) ); | |
for(int i = 0; i < (int)faces.size(); ++i) | |
{ | |
cv::Point pt1 = faces[i].tl(); | |
cv::Size sz = faces[i].size(); | |
cv::Point pt2(pt1.x+sz.width, pt1.y+sz.height); | |
cv::rectangle(frame, pt1, pt2, cv::Scalar(255)); | |
} | |
cv::imshow("faces", frame); | |
cv::waitKey(0); | |
} | |
else | |
{ | |
std::cout << "Error: " << cascade_file << " is not a valid haarcascade file" << std::endl; | |
return 1; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Python.h> | |
extern int find_face(const char* cascade_file, const char* image_file); | |
static PyObject* find_face_system(PyObject *self, PyObject *args) | |
{ | |
const char* cascade_path; | |
const char* image_path; | |
if (PyArg_ParseTuple(args, "ss", &cascade_path, &image_path)) | |
{ | |
int status = find_face(cascade_path, image_path); | |
return Py_BuildValue("i", status); | |
} | |
return NULL; | |
} | |
static PyMethodDef OpenCVGPUMethods[] = { | |
{"find_face", find_face_system, METH_VARARGS, | |
"OpenCV GPU bindings."}, | |
{NULL, NULL, 0, NULL} /* Sentinel */ | |
}; | |
#if PY_VERSION_HEX >= 0x03000000 | |
/* Python 3.x code */ | |
static struct PyModuleDef opencvgpu = { | |
PyModuleDef_HEAD_INIT, | |
"opencvgpu", /* name of module */ | |
NULL, /* module documentation, may be NULL */ | |
-1, /* size of per-interpreter state of the module, | |
or -1 if the module keeps state in global variables. */ | |
OpenCVGPUMethods | |
}; | |
PyMODINIT_FUNC | |
PyInit_opencvgpu(void) | |
{ | |
(void) PyModule_Create(&opencvgpu); | |
} | |
#else | |
/* Python 2.x code */ | |
PyMODINIT_FUNC | |
initopencvgpu(void) | |
{ | |
(void) Py_InitModule("opencvgpu", OpenCVGPUMethods); | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from distutils.core import setup, Extension | |
from subprocess import check_output | |
# Pkg-config function from https://gist.github.com/abergmeier/9488990 | |
def pkgconfig(*packages, **kw): | |
flag_map = { | |
'-I': 'include_dirs', | |
'-L': 'library_dirs', | |
'-l': 'libraries'} | |
env = os.environ.copy() | |
# possible narrowing of PkgConfig environment variables | |
for token in check_output(['pkg-config', '--libs', '--cflags', ' '.join(packages)], env=env).split(): | |
key = token[:2] | |
try: | |
arg = flag_map[key] | |
value = token[2:] | |
except KeyError: | |
arg = 'extra_link_args' | |
value = token | |
kw.setdefault(arg, []).append(value) | |
for key, value in kw.iteritems(): # remove duplicated | |
kw[key] = list(set(value)) | |
return kw | |
opencv_deps = pkgconfig('opencv') | |
opencvgpumodule = Extension('opencvgpu', | |
define_macros = [('MAJOR_VERSION', '1'), | |
('MINOR_VERSION', '0')], | |
include_dirs = opencv_deps['include_dirs'], | |
libraries = opencv_deps['libraries'], | |
sources = ['opencvgpumodule.cpp', 'findface.cpp']) | |
setup (name = 'OpenCV GPU', | |
version = '1.0', | |
description = 'OpenCV GPU Bindings', | |
author = 'Alexander Koumis and Matthew Carlis', | |
author_email = '[email protected], [email protected]', | |
url = 'https://docs.python.org/extending/building', | |
long_description = ''' | |
OpenCV GPU Bindings | |
''', | |
ext_modules = [opencvgpumodule]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CPython wrapper example performing face detection (it doesn't actually use GPU but that's what will eventually be built with this).
Installation
Usage