gpt4 book ai didi

python - 痛饮 : function that modifies its argument from c++ to python

转载 作者:太空狗 更新时间:2023-10-29 22:55:31 28 4
gpt4 key购买 nike

我是 SWIG 的新手,我使用的相机使用 SWIG 在 Python 中封装 C++。这台相机很特别,因为我无法直接获取原始图像。然后我找到/添加一个可以制作快照的 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(&current_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 中使用一个char 数组,它将包含像素值,并且可以通过将其作为参数传递到函数 get_frame 中进行修改。 C++ 函数看起来像:

int PixyInterpreter::get_frame(unsigned char *current_frame)

和接口(interface)文件“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...]

它返回 * buffer overflow detected *: python terminated 这似乎发生在 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 类型一起使用 * :/

最佳答案

如果您想从 C++ 接收一个数组并将您在 Python 级别所做的更改反射(reflect)在 C++ 中,我建议您使用 NumPy SWIG 绑定(bind) numpy.i .

我在这里使用 ARGOUTVIEW_ARRAY1 这样您在 Python 中所做的更改会立即反射(reflect)在底层 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!'

关于python - 痛饮 : function that modifies its argument from c++ to python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51598137/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com