- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要从我的模块调用 Python 函数并为其设置两个参数:int 和 array。
现在我在调用这个函数的过程中遇到了段错误,我不知道我做错了什么。有人可以指定我的错误在哪里吗?
我的 Python 模块 app.py 中的函数。如果我从 Python 代码中调用它,它会起作用:
def get_model(rate, signal):
mfcc_train = MFCC().compute(rate, signal)
with open('mfcc_test', 'wb') as f:
pickle.dump(mfcc_train, f)
return clf()._fit(mfcc_train)
调用上述函数的 C 代码。最后一个 Spring 是“打电话之前”
#include <Python.h>
#include <stdio.h>
#include "wav.h"
#include <numpy/arrayobject.h>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;
uint8_t *samples = NULL;
wavread("test.wav", &samples);
printf("No. of channels: %d\n", header->num_channels);
printf("Sample rate: %d\n", header->sample_rate);
printf("Bit rate: %dkbps\n", header->byte_rate*8 / 1000);
printf("Bits per sample: %d\n\n", header->bps);
printf("Sample 0: %d\n", samples[0]);
printf("Sample 1: %d\n", samples[1]);
// Initialize the Python Interpreter
printf("Before init\n");
Py_Initialize();
PyObject *sysPath = PySys_GetObject("path");
const char *scriptDirectoryName = ".";
PyObject *path = PyUnicode_FromString(scriptDirectoryName);
int result = PyList_Insert(sysPath, 0, path);
printf("after init\n");
// Build the name object
pName = PyUnicode_DecodeFSDefault(argv[1]);
printf("after pname %s %d\n", argv[1], pName == NULL ? 1 : 0);
// Load the module object
pModule = PyImport_Import(pName);
printf("after pmodule %d\n", pModule == NULL ? 1 : 0);
// pFunc is also a borrowed reference
pFunc = PyObject_GetAttrString(pModule, "get_model");
printf("after pfunc\n");
if (PyCallable_Check(pFunc))
{
pArgs = PyTuple_New(2);
printf("after pytuple\n");
PyTuple_SetItem(pArgs, 0, PyLong_FromLong(header->sample_rate));
printf("after set item\n");
uint8_t* array = malloc(header->datachunk_size);
int dims[1];
dims[0] = header->datachunk_size;
printf("alloc\n");
import_array();
PyObject* pSamples = PyArray_SimpleNewFromData(1, dims, NPY_INT8, (void*)samples);
printf("pSamples\n");
PyArray_ENABLEFLAGS((PyArrayObject*)pSamples, NPY_ARRAY_OWNDATA);
PyTuple_SetItem(pArgs, 1, pSamples);
printf("Before calling\n");
pValue = PyObject_CallObject(pFunc, pArgs);
printf("After calling\n");
} else
{
PyErr_Print();
}
printf("pValue: %d\n", pValue);
// Clean up
Py_DECREF(pModule);
Py_DECREF(pFunc);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
free(header);
free(samples);
}
UPD:更新了代码,修复了一个问题。但另一个问题仍然存在。它在 PyObject* pSamples = PyArray_SimpleNewFromData(1, dims, NPY_INT8, (void*)samples);
行中。而且我找不到它有什么问题。
还有 wav.h 以防万一:
#include <inttypes.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
typedef struct {
char chunk_id[4];
uint32_t chunk_size;
char format[4];
char fmtchunk_id[4];
uint32_t fmtchunk_size;
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bps;
char datachunk_id[4];
uint32_t datachunk_size;
}WavHeader;
WavHeader *header;
void wavread(char *file_name, int16_t **samples)
{
int fd;
if (!file_name)
errx(1, "Filename not specified");
if ((fd = open(file_name, O_RDONLY)) < 1)
errx(1, "Error opening file");
if (!header)
header = (WavHeader*)malloc(sizeof(WavHeader));
if (read(fd, header, sizeof(WavHeader)) < sizeof(WavHeader))
errx(1, "File broken: header");
if (strncmp(header->chunk_id, "RIFF", 4) ||
strncmp(header->format, "WAVE", 4))
errx(1, "Not a wav file");
if (header->audio_format != 1)
errx(1, "Only PCM encoding supported");
if (*samples) free(*samples);
*samples = (int16_t*)malloc(header->datachunk_size);
if (!*samples)
errx(1, "Error allocating memory");
if (read(fd, *samples, header->datachunk_size) < header->datachunk_size)
errx(1, "File broken: samples");
close(fd);
}
最佳答案
如果没有 header
的定义很难判断,但我相信问题出在行上
PyTuple_SetItem(pArgs, 0, header->sample_rate);
PyTuple_SetItem
需要一个 Python 对象,而您将我认为是整数的东西传递给它,它被误解为 PyObject*
。
我怀疑你想要
PyTuple_SetItem(pArgs, 0, PyInt_FromLong(header->sample_rate));
(Python3 中的PyLong_FromLong
)
第二个问题:您 free
samples
两次。首先你将它传递给 numpy 并告诉 numpy 它拥有数据:
PyObject* pSamples = PyArray_SimpleNewFromData(1, dims, NPY_INT8, (void*)samples);
PyArray_ENABLEFLAGS((PyArrayObject*)pSamples, NPY_ARRAY_OWNDATA);
然后在代码的末尾释放它
free(samples);
我怀疑您打算将新分配的 array
传递给 numpy 而不是 samples
。 (如果是这种情况,您仍然需要在它们之间复制数据)
UPD:评论中的一个更正确的解决方案是将 dims 类型从 int 更改为 npy_intp
关于python - 将两个参数(int 和数组)传递给嵌入式 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45737189/
这个问题困扰了我几天。 这是我的相关 Storyboard布局: 我已经将阳光下的每个布局都设置为所有三个 View Controller ,并且仍然得到一个在横幅 View 上方有一个“间隙”的结果
我正在我的 C++ 程序中嵌入一个网页。我遇到的问题是,在嵌入式页面的 javascript 中,我可以捕获 onkeypress,但不会触发 onkeydown 和 onkeyup。 如果我在非嵌入
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
我有一个 java web 应用程序。我想创建一个嵌入式 LDAP 服务器,当 web 应用程序运行时,我将向 LDAP 插入一些记录,并且有另一个 web 应用程序将访问此 LDAP 以获取信息。可
我正在尝试通过 tomcat maven 插件将 war 部署到嵌入式 tomcat 服务器。控制台显示服务器启动正常。 看来 war 还没有展开。当我访问 http://localhost:9090
假设我有如下函数: bigrams=[(k,v) for (k,v) in dict_bigrams.items() if k[:pos_qu]==selection[:pos_qu
我读过一些关于 python 嵌入式 C++ 的教程。我曾引用过 python 对象。 https://docs.python.org/3/c-api/function.html Python 脚本:
我正在使用嵌入式应用程序,在调试期间,调试器无法解析宏符号(我的理论:因为宏在预处理中丢失了)。我最终不得不先在源代码中找到宏,然后使用定义来监视变量。 我的问题是:有没有办法将宏定义合并到 elf
首先我要说的是我开发的是基于cortex m4的嵌入式设备应用。 我有引导加载程序和主应用程序通用的功能。现在我为引导加载程序和应用程序编译源文件 2 次。但是我的双库 dfu 空间不足,我想在 RO
作为嵌入式 C 编程的初学者,我很好奇每个(根据我的经验)程序执行是如何从 main() 函数开始的?这就像链接器识别 main() 并将那个“特殊” 函数的地址放入重置 vector 指向的地址。
在我的实时嵌入式处理器固件中,我需要十进制数字的格式化打印。标准 printf/sprintf 在工具链中不可用,所以我需要自己实现它。 我使用了除以十并取余的天真方法。但是我的目标处理器本身不支持除
我有编程经验,但在软件开发方面了解不多。我目前正在为我工作的公司编写一个软件,我开始挑战自己代码的可读性。 我想知道这是否是嵌入式 if 语句的“有效”替代方案,或者我是否可以使用更好的方法。 假
我有一个运行嵌入式 Linux 的嵌入式目标,我想计算以下时间: 1) 高速缓存读/写时序2) uncache 内存读/写时序 Linux 中是否有任何标准测试来计算上述时间? 我已经编写了自己的测试
大多数嵌入式设备都是为了在通常资源受限或低规格的设备上执行特定任务而构建的。 因此,大多数嵌入式开发人员需要去除不必要的库和模块,并为其特定设备和用例创建自定义分发。我们先来了解一下嵌入式 Linu
我正在嵌入式处理器上编写一个简单的裸机应用程序。作为此应用程序的一部分,它必须使用 malloc 在大约 256kB 的堆上分配一些内存。注意:最初这是在 main 中静态分配的,但在一定的大小限制下
我正在尝试为我 friend 的婚礼建立一个网站。我使用的是 Bootstrap 5,嵌入的视频没有填满屏幕大小。这是一个 live test page HTML: 您还需要代码吗?我想让视频的全宽
我有一个项目,我尝试为微 Controller 构建固件并尝试更好地控制所使用的优化标志。我想,而不是使用 -O flag 分别指定不同的优化标志。不幸的是,-O 似乎发生了一些优化魔法。我无法使用单
我正在使用双核设备,并且要求核心 A 创建一个数据结构,其中包含在核心 B 上运行的函数列表的参数,定期更新它并通知核心 B。参数和类型的数量可以改变在运行期间。 我的计划如下.. 创建一个 Para
我们有一个 Microsoft.Phone.Controls.WebBrowser内嵌控件 StackPanel , 在 PivotItem 内在 Windows Phone 8 上。以简化的形式,它
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 2 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!