我是SWIG的新手,我使用的相机使用SWIG将c ++包装在python中。 该相机很特别,因为我无法直接获取原始图像。然后,我找到/添加一个可以制作快照的C ++函数,这里是:
int PixyInterpreter::get_frame(void){
unsigned int size_frame = 8; // size of the frame to grab print
unsigned char current_frame[72000]; // ~largest possible given current hardware
unsigned char *pixels; //returned pointer to video frame buffer
int32_t response, fourcc;
int8_t renderflags;
int return_value;
uint16_t width, height;
uint32_t numPixels;
// stop blob processing
return_value = pixy_command("stop", END_OUT_ARGS, &response, END_IN_ARGS);
printf("STOP returned %d response %d\n", return_value, response);
response = 0;
return_value = pixy_command("cam_getFrame", // String id for remote procedure
0x01, 0x21, // mode 0 = 1280x800 25 fps
0x02, 0, // xoffset
0x02, 0, // yoffset
0x02, 320, // width
0x02, 200, // height (56 max @ 1280 w)
0, // separator
&response, // pointer to mem address for return value
&fourcc, //contrary to docs, the next 5 args are needed
&renderflags,
&width,
&height,
&numPixels,
&pixels, // pointer to mem address for returned frame
0);
printf("getFrame returned %d response %d\n", return_value, response);
printf("returned w %d h %d npix %d\n",width,height,numPixels);
// quit now if not successful:
if(return_value != 0) return return_value;
// save this block
memcpy(¤t_frame, pixels,numPixels);
// display average and size_frame x size_frame pixel dump
unsigned int i,j,ind,start;
// dump a few raw pixels
start=(height/2)*width+width/2; //roughly in middle of frame
for (i=0; i<size_frame; i++) {
for (j=0; j<size_frame; j++) {
ind = i*width + j + start;
printf(" %02x",frame[ind]);
}
printf("\n");
}
// run the last programm again
return_value = pixy_command("run", END_OUT_ARGS, &response, END_IN_ARGS);
printf("START returned %d response %d\n", return_value, response);
return numPixels;
}
此代码在C ++中工作正常,并在用Python包装时:打印像素值。
但是问题是我无法在Python中获得像素值(数组) ,因为代码c ++不会返回它。好的逻辑:)。
然后我的想法是在Python中使用字符数组,该数组将包含像素值,可以通过将其作为参数传递给函数get_frame进行修改。 C ++函数看起来像:
int PixyInterpreter::get_frame(unsigned char *current_frame)
和界面文件“ pixy.i” (部分):
%module pixy
%include "stdint.i"
%include "carrays.i"
%{
#define SWIG_FILE_WITH_INIT
#include "pixy.h"
%}
%array_class(unsigned char, charArray);
int get_frame(charArray * frame);
但不幸的是,在python中:
>> a = charArray(72000)
>> get_frame(a)
[...errors...]
它返回 *检测到缓冲区溢出* :python终止,它似乎发生在 memcpy !
*** buffer overflow detected ***: python terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f63d95227e5]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7f63d95c415c]
/lib/x86_64-linux-gnu/libc.so.6(+0x117160)[0x7f63d95c2160]
./_pixy.so(_ZN15PixyInterpreter9get_frameEPh+0x156)[0x7f63d801e636]
./_pixy.so(+0x10d37)[0x7f63d8015d37]
python(PyEval_EvalFrameEx+0x5ca)[0x4bc3fa]
python(PyEval_EvalCodeEx+0x306)[0x4b9ab6]
python[0x4eb30f]
python(PyRun_InteractiveOneFlags+0x190)[0x44a7a2]
python(PyRun_InteractiveLoopFlags+0xba)[0x44a56d]
python[0x43092e]
python(Py_Main+0x612)[0x493ae2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f63d94cb830]
python(_start+0x29)[0x4933e9]
======= Memory map: ========
00400000-006de000 r-xp 00000000 08:03 9969410 /usr/bin/python2.7
008dd000-008de000 r--p 002dd000 08:03 9969410 /usr/bin/python2.7
008de000-00955000 rw-p 002de000 08:03 9969410 /usr/bin/python2.7
00955000-00978000 rw-p 00000000 00:00 0
02404000-02548000 rw-p 00000000 00:00 0 [heap]
7f63d0000000-7f63d0021000 rw-p 00000000 00:00 0
7f63d0021000-7f63d4000000 ---p 00000000 00:00 0
7f63d6019000-7f63d601a000 ---p 00000000 00:00 0
7f63d601a000-7f63d681a000 rw-p 00000000 00:00 0
7f63d681a000-7f63d681b000 ---p 00000000 00:00 0
7f63d681b000-7f63d701b000 rw-p 00000000 00:00 0
7f63d701b000-7f63d7022000 r-xp 00000000 08:03 2490667 /lib/x86_64-linux-gnu/librt-2.23.so
7f63d7022000-7f63d7221000 ---p 00007000 08:03 2490667 /lib/x86_64-linux-gnu/librt-2.23.so
7f63d7221000-7f63d7222000 r--p 00006000 08:03 2490667 /lib/x86_64-linux-gnu/librt-2.23.so
7f63d7222000-7f63d7223000 rw-p 00007000 08:03 2490667 /lib/x86_64-linux-gnu/librt-2.23.so
7f63d7223000-7f63d7239000 r-xp 00000000 08:03 2494948 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f63d7239000-7f63d7438000 ---p 00016000 08:03 2494948 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f63d7438000-7f63d7439000 rw-p 00015000 08:03 2494948 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f63d7439000-7f63d75ab000 r-xp 00000000 08:03 9963184 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f63d75ab000-7f63d77ab000 ---p 00172000 08:03 9963184 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f63d77ab000-7f63d77b5000 r--p 00172000 08:03 9963184 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f63d77b5000-7f63d77b7000 rw-p 0017c000 08:03 9963184 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f63d77b7000-7f63d77bb000 rw-p 00000000 00:00 0
7f63d77bb000-7f63d77d2000 r-xp 00000000 08:03 2495090 /lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
7f63d77d2000-7f63d79d1000 ---p 00017000 08:03 2495090 /lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
7f63d79d1000-7f63d79d2000 r--p 00016000 08:03 2495090 /lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
7f63d79d2000-7f63d79d3000 rw-p 00017000 08:03 2495090 /lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
7f63d79d3000-7f63d79d9000 r-xp 00000000 08:03 9973881 /usr/lib/x86_64-linux-gnu/libboost_chrono.so.1.58.0
7f63d79d9000-7f63d7bd9000 ---p 00006000 08:03 9973881 /usr/lib/x86_64-linux-gnu/libboost_chrono.so.1.58.0
7f63d7bd9000-7f63d7bda000 r--p 00006000 08:03 9973881 /usr/lib/x86_64-linux-gnu/libboost_chrono.so.1.58.0
7f63d7bda000-7f63d7bdb000 rw-p 00007000 08:03 9973881 /usr/lib/x86_64-linux-gnu/libboost_chrono.so.1.58.0
7f63d7bdb000-7f63d7bde000 r-xp 00000000 08:03 9967987 /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0
7f63d7bde000-7f63d7ddd000 ---p 00003000 08:03 9967987 /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0
7f63d7ddd000-7f63d7dde000 r--p 00002000 08:03 9967987 /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0
7f63d7dde000-7f63d7ddf000 rw-p 00003000 08:03 9967987 /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0
7f63d7ddf000-7f63d7e03000 r-xp 00000000 08:03 9967992 /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0
7f63d7e03000-7f63d8002000 ---p 00024000 08:03 9967992 /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0
7f63d8002000-7f63d8004000 r--p 00023000 08:03 9967992 /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0
7f63d8004000-7f63d8005000 rw-p 00025000 08:03 9967992 /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0
7f63d8005000-7f63d802a000 r-xp 00000000 08:03 9699483 /home/leat/pixy/build/pantilt_in_python/_pixy.so
7f63d802a000-7f63d822a000 ---p 00025000 08:03 9699483 /home/leat/pixy/build/pantilt_in_python/_pixy.so
7f63d822a000-7f63d822b000 r--p 00025000 08:03 9699483 /home/leat/pixy/build/pantilt_in_python/_pixy.so
7f63d822b000-7f63d822d000 rw-p 00026000 08:03 9699483 /home/leat/pixy/build/pantilt_in_python/_pixy.so
7f63d822d000-7f63d8252000 r-xp 00000000 08:03 2495082 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f63d8252000-7f63d8451000 ---p 00025000 08:03 2495082 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f63d8451000-7f63d8455000 r--p 00024000 08:03 2495082 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f63d8455000-7f63d8456000 rw-p 00028000 08:03 2495082 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f63d8456000-7f63d8493000 r-xp 00000000 08:03 2495061 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f63d8493000-7f63d8693000 ---p 0003d000 08:03 2495061 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f63d8693000-7f63d8695000 r--p 0003d000 08:03 2495061 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f63d8695000-7f63d869b000 rw-p 0003f000 08:03 2495061 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f63d869b000-7f63d869c000 rw-p 00000000 00:00 0
7f63d869c000-7f63d86a1000 r-xp 00000000 08:03 10096750 /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7f63d86a1000-7f63d88a1000 ---p 00005000 08:03 10096750 /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7f63d88a1000-7f63d88a2000 r--p 00005000 08:03 10096750 /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7f63d88a2000-7f63d88a4000 rw-p 00006000 08:03 10096750 /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7f63d88a4000-7f63d8b81000 r--p 00000000 08:03 9963224 /usr/lib/locale/locale-archive
7f63d8b81000-7f63d8c89000 r-xp 00000000 08:03 2490396 /lib/x86_64-linux-gnu/libm-2.23.so
7f63d8c89000-7f63d8e88000 ---p 00108000 08:03 2490396 /lib/x86_64-linux-gnu/libm-2.23.so
7f63d8e88000-7f63d8e89000 r--p 00107000 08:03 2490396 /lib/x86_64-linux-gnu/libm-2.23.so
7f63d8e89000-7f63d8e8a000 rw-p 00108000 08:03 2490396 /lib/x86_64-linux-gnu/libm-2.23.so
7f63d8e8a000-7f63d8ea3000 r-xp 00000000 08:03 2490612 /lib/x86_64-linux-gnu/libz.so.1.2.8
7f63d8ea3000-7f63d90a2000 ---p 00019000 08:03 2490612 /lib/x86_64-linux-gnu/libz.so.1.2.8
7f63d90a2000-7f63d90a3000 r--p 00018000 08:03 2490612 /lib/x86_64-linux-gnu/libz.so.1.2.8
7f63d90a3000-7f63d90a4000 rw-p 00019000 08:03 2490612 /lib/x86_64-linux-gnu/libz.so.1.2.8
7f63d90a4000-7f63d90a6000 r-xp 00000000 08:03 2490656 /lib/x86_64-linux-gnu/libutil-2.23.so
7f63d90a6000-7f63d92a5000 ---p 00002000 08:03 2490656 /lib/x86_64-linux-gnu/libutil-2.23.so
7f63d92a5000-7f63d92a6000 r--p 00001000 08:03 2490656 /lib/x86_64-linux-gnu/libutil-2.23.so
7f63d92a6000-7f63d92a7000 rw-p 00002000 08:03 2490656 /lib/x86_64-linux-gnu/libutil-2.23.so
7f63d92a7000-7f63d92aa000 r-xp 00000000 08:03 2490651 /lib/x86_64-linux-gnu/libdl-2.23.so
7f63d92aa000-7f63d94a9000 ---p 00003000 08:03 2490651 /lib/x86_64-linux-gnu/libdl-2.23.so
7f63d94a9000-7f63d94aa000 r--p 00002000 08:03 2490651 /lib/x86_64-linux-gnu/libdl-2.23.so
7f63d94aa000-7f63d94ab000 rw-p 00003000 08:03 2490651 /lib/x86_64-linux-gnu/libdl-2.23.so
7f63d94ab000-7f63d966b000 r-xp 00000000 08:03 2490649 /lib/x86_64-linux-gnu/libc-2.23.so
7f63d966b000-7f63d986b000 ---p 001c0000 08:03 2490649 /lib/x86_64-linux-gnu/libc-2.23.so
7f63d986b000-7f63d986f000 r--p 001c0000 08:03 2490649 /lib/x86_64-linux-gnu/libc-2.23.so
7f63d986f000-7f63d9871000 rw-p 001c4000 08:03 2490649 /lib/x86_64-linux-gnu/libc-2.23.so
7f63d9871000-7f63d9875000 rw-p 00000000 00:00 0
7f63d9875000-7f63d988d000 r-xp 00000000 08:03 2490648 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f63d988d000-7f63d9a8c000 ---p 00018000 08:03 2490648 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f63d9a8c000-7f63d9a8d000 r--p 00017000 08:03 2490648 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f63d9a8d000-7f63d9a8e000 rw-p 00018000 08:03 2490648 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f63d9a8e000-7f63d9a92000 rw-p 00000000 00:00 0
7f63d9a92000-7f63d9ab8000 r-xp 00000000 08:03 2490647 /lib/x86_64-linux-gnu/ld-2.23.so
7f63d9ae2000-7f63d9b00000 r-xp 00000000 08:03 2490373 /lib/x86_64-linux-gnu/libudev.so.1.6.4
7f63d9b00000-7f63d9b01000 r--p 0001d000 08:03 2490373 /lib/x86_64-linux-gnu/libudev.so.1.6.4
7f63d9b01000-7f63d9b02000 rw-p 0001e000 08:03 2490373 /lib/x86_64-linux-gnu/libudev.so.1.6.4
7f63d9b39000-7f63d9c7f000 rw-p 00000000 00:00 0
7f63d9c7f000-7f63d9c86000 r--s 00000000 08:03 10227159 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f63d9c86000-7f63d9cb7000 rw-p 00000000 00:00 0
7f63d9cb7000-7f63d9cb8000 r--p 00025000 08:03 2490647 /lib/x86_64-linux-gnu/ld-2.23.so
7f63d9cb8000-7f63d9cb9000 rw-p 00026000 08:03 2490647 /lib/x86_64-linux-gnu/ld-2.23.so
7f63d9cb9000-7f63d9cba000 rw-p 00000000 00:00 0
7ffd99927000-7ffd99948000 rw-p 00000000 00:00 0 [stack]
7ffd99985000-7ffd99988000 r--p 00000000 00:00 0 [vvar]
7ffd99988000-7ffd9998a000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abandon (core dumped)
很长的帖子,很抱歉,但是如果有人有任何建议或解释我如何解决我的问题,我将不胜感激!
另外,我发现%Array_functions()和%array_class()不应与char或char类型一起使用 *:/
答案 0 :(得分:1)
如果您想从C ++接收数组并在C ++中反映您在Python级别上所做的更改,建议您使用NumPy SWIG绑定numpy.i
。
在这里,我正在使用ARGOUTVIEW_ARRAY1
,以便您在Python中所做的更改会立即反映在基础C ++内存中(尽管这意味着没有调整大小)。请记住,由于内存归C ++所有,因此您有责任清理内存。如果这不符合您的需求,则可以选择其他类型图。有good documentation。
test.i
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "test.hpp"
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply (unsigned char** ARGOUTVIEW_ARRAY1, int* DIM1) {(unsigned char** current_frame, int* numPixels)};
%include "test.hpp"
test.hpp
#pragma once
struct PixyInterpreter {
unsigned char *frame;
PixyInterpreter() : frame(NULL) {}
~PixyInterpreter() { delete[] frame; }
void get_frame(unsigned char **current_frame, int *numPixels) {
*numPixels = 12;
frame = new unsigned char[*numPixels];
frame[ 0] = 'H';
frame[ 1] = 'e';
frame[ 2] = 'l';
frame[ 3] = 'l';
frame[ 4] = 'o';
frame[ 5] = ' ';
frame[ 6] = 'W';
frame[ 7] = 'o';
frame[ 8] = 'r';
frame[ 9] = 'l';
frame[10] = 'd';
frame[11] = '!';
frame[12] = '\0';
*current_frame = frame;
}
};
test.py
import example
interpreter = example.PixyInterpreter()
frame = interpreter.get_frame()
print(frame.tostring())
示例调用:
$ swig -c++ -python -py3 test.i
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/python3.6/ -fPIC -shared test_wrap.cxx -o _example.so -lpython3.6m
$ python3 test.py
b'Hello World!'
答案 1 :(得分:0)
非常感谢您提供详尽的答案!
我尝试了您的示例,并且效果很好。在经历了尝试使您的想法重现/适应我的代码的麻烦之后,现在它可以编译了,但是无法正常工作...我在接口文件中做了一些更改,我的完整接口文件是:
pixy.i
%module pixy
%{
#define SWIG_FILE_WITH_INIT
#include "pixy.h"
%}
%include "stdint.i"
%include "carrays.i"
%include "numpy.i"
%init%{
import_array();
%}
%apply (unsigned char** ARGOUTVIEW_ARRAY1, uint16_t* DIM1) {(unsigned char** current_frame, uint16_t* nbPixels)}
//%include "pixy.h" //<--- Error : Unable to find 'pixy.h'
%array_class(struct Block, BlockArray);
int pixy_init();
void pixy_close();
void pixy_error(int error_code);
int pixy_blocks_are_new();
int pixy_get_blocks(uint16_t max_blocks, BlockArray *blocks);
int pixy_rcs_set_position(uint8_t channel, uint16_t position);
struct Block
{
uint16_t type;
uint16_t signature;
uint16_t x;
uint16_t y;
uint16_t width;
uint16_t height;
int16_t angle;
};
// Inspired from the block struct above
struct PixyFrame // if not written
{ // >>> interpreter = pixy.PixyFrame()
// >>> Error : PixyFrame not defined
int get_frame (unsigned char** current_frame, uint16_t* nbPixels);
// if not added
// >>> interpreter = PixyFrame()
// >>> frame = interpreter.get_frame()
// AttributeError: type object 'object' has no attribute '__getattr__'
};
我看过很多类似的例子:
foo.i
%module foo
%{
#include "foo.h"
%}
%include "foo.h"
但是最后一个 include 在我的文件中返回错误:错误:找不到'pixy.h'
我几乎可以用我的界面文件重现您的示例,但是,当我调用get_frame()时:
>>> interpreter = PixyFrame()
>>> frame = interpreter.get_frame()
TypeError: get_frame() takes exactly 3 arguments (1 given)
所需的3个参数是(self,unsigned char **,uint16_t)
在构建时收到此警告:
pixy.i:17: Warning 453: Can't apply (unsigned char **ARGOUTVIEW_ARRAY1,uint16_t *DIM1). No typemaps are defined.