Python C + + extensions
Some time ago read an article, http://blog.jobbole.com/78859/, quite touched, so combined with their work knowledge made a simple Python motion detection: The motion detection algorithm using C + + implementation, encapsulated into Python can be called format, Specific Process 1.
Figure 1
First look at the engineering configuration of C + +, 2
Figure 2
C + + section code:
#include "stdafx.h"
#include "Python.h"
#include "C:\Python27\Lib\site-packages\numpy\core\include\numpy\arrayobject.h"
#include "Motiondetector.h"
Static int md_grid_w = 32;
Static int md_grid_h = 32;
static int md_noise_level = 150;
static int md_sensitivity =;
Class Videodetector
{
Public:
~videodetector ()
{
if (m_motiondetector!) = NULL)
{
delete m_motiondetector;
m_motiondetector = NULL;
printf ("~videodetector\r\n");
}
}
videodetector (int width, int height): m_width (width), m_height (height)
{
m_motiondetector = new Simple_md::motiondetector ();
if (M_motiondetector! = NULL)
{
m_motiondetector- >init (Md_grid_w, Md_grid_h, m_width, height);
m_motiondetector->set_noise_level (Md_noise_level);
m_motiondetector->set_sensitivity (md_sensitivity);
Setup Default Zone
std::vector<simple_md::zone> zones;
Simple_md::zone Z;
z.sensitivity = 100;
for (int i = 0; i < Md_grid_w * MD_GRID_H; ++i)
{
Z.mask.push_back (100);
}
Zones.push_back (z);
M_motiondetector->set_zones (zones);
M_motiondetector->set_md_enbale ();
printf ("videodetector\r\n");
}
}
int Process_frame (uint8_t *frame)
{
if (m_motiondetector! = NULL && frame! = null)
{
M_motiondetector->feed_frame (frame, m_width, M_height, M_width-1, GetTickCount (), NULL, 0, NULL, 0, NULL, 0);
return M_motiondetector->get_state ();
}
return 0;
}
int test (void)
{
return m_width * m_height;
}
Private
Videodetector ();
Simple_md::motiondetector *m_motiondetector;
int m_width;
int m_height;
};
static void Pydelvideodetector (void *ptr)
{
printf ("videodetector_uninit\n");
Videodetector *tmp = Static_cast<videodetector *> (PTR);
Delete tmp;
Return
}
Pyobject *videodetector_init (Pyobject *self, Pyobject *args)
{
printf ("videodetector_init\n");
int arg1 = 0;
int arg2 = 0;
int ret = pyarg_parsetuple (args, "II", &arg1, &ARG2);
if (ret = = 0)
{
printf ("Videodetector_init fail\n");
return NULL;
}
Videodetector *VD = new Videodetector (arg1, arg2);
Return Pycobject_fromvoidptr (VD, pydelvideodetector);
}
#define F (x0) (* (uint8_t*) Pyarray_data (Py_pix) + (x0) * Pyarray_strides (Py_pix) [0]))
#define SHAPE (i) (py_pix->dimensions[(i)))
Pyobject *videodetector_process (Pyobject *self, Pyobject *args)
{
printf ("videodetector_process\n");
Pyobject *py_obj = 0;
Pyarrayobject *py_pix = 0;
Npy_int64 idx = 0;
int ret = pyarg_parsetuple (args, "OO", &py_obj, &py_pix);
if (ret = = 0)
{
printf ("Videodetector_process fail\n");
return NULL;
}
uint8_t *frame = (uint8_t *) malloc (sizeof (uint8_t) * shape (0));
if (frame = = NULL)
{
return NULL;
}
for (idx = 0; idx < shape (0); idx++)
{
* (frame + idx) = (uint8_t) f (IDX);
}
printf ("-------process_frame start-------\ n");
void * tmp = PYCOBJECT_ASVOIDPTR (py_obj);
videodetector *VD = static_cast<videodetector *> (TMP);
int result = Vd->process_frame (frame);
free (frame);
frame = NULL;
printf ("-------Process_frame End (%d)-------\ n", result);
return py_buildvalue ("i", result);
}
Pyobject *videodetector_test (Pyobject *self, Pyobject *args)
{
printf ("videodetector_test\n");
Pyobject *pynum = 0;
int ret = pyarg_parsetuple (args, "O", &pynum);
if (ret = = 0)
{
printf ("Videodetector_test fail\n");
return NULL;
}
void * tmp = PYCOBJECT_ASVOIDPTR (pynum);
Videodetector *VD = Static_cast<videodetector *> (TMP);
int result = Vd->test ();
Return Py_buildvalue ("I", result);
}
Static Pymethoddef videodetector_methods[] = {
{"Videodetector", Videodetector_init, Meth_varargs},
{"Test", Videodetector_test, Meth_varargs},
{"Process", Videodetector_process, Meth_varargs},
{null, NULL, 0}
};
Pymodinit_func initvideodetector (void)
{
Py_initmodule ("Videodetector", videodetector_methods);
}
videodetector.py Code:
From videodetector Import *
Class Videodetector (object):
def __init__ (self, arg1, arg2):
Self._base = Videodetector (arg1, arg2)
def test (self):
Return Test (Self._base)
def process (self, frame):
return process (Self._base, frame)
camera.py Code:
Import Cv2
Import time
Import datetime
Import NumPy as NP
From videodetector Import *
Frame_num = 0
def motion_detect (obj, frame):
Global Frame_num
If frame_num% 5 = = 0:
Print Frame_num
pixels = []
For ch in frame.tostring ():
Pixels.append (ORD (CH))
#rgb2yuv = Np.array ([[0.299, 0.587, 0.114],
# [-0.14713,-0.28886, 0.436],
# [0.615,-0.51499,-0.10001]])
#rgb = Np.array (pixels). Reshape (len (pixels)/3, 3)
#yuv = Np.dot (RGB, RGB2YUV. T
#y = Np.array (yuv[:, 0]). Reshape (Len (YUV), 1)
#print Yuv.shape
#print Y.shape
#print obj.process (y)
RGB = Np.array (pixels). Reshape (len (pixels)/3, 3)
r = rgb[:, 0];
g = rgb[:, 1];
b = rgb[:, 2];
Raw = r + G + B;
#print Datetime.datetime.now (). Second
#print Datetime.datetime.now (). microsecond
Print Raw.shape;
Print obj.process (RAW)
Frame_num + = 1
Camera = Cv2. Videocapture (0)
fps = Camera.get (Cv2.cv.CV_CAP_PROP_FPS)
(width, height) = (int (camera.get (Cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int (Camera.get (Cv2.cv.CV_CAP_PROP_FRAME_HEIGHT )))
Print width
Print height
obj = videodetector (width, height)
#print Obj.test ()
Print ("program Begin")
While True:
(grabbed, frame) = Camera.read ()
If not grabbed:
Continue
Cv2.imshow ("Frame", frame)
Motion_detect (obj, frame)
Key = Cv2.waitkey (1) & 0xFF
If key = = Ord ("q"):
Break
Print ("program End")
Camera.release ()
Cv2.destroyallwindows ()
Print ("program release")
Effect 3:
Figure 3
Python C + + extensions