首页 > 解决方案 > Ctypes 扩展 Python 并返回数组 (2D)

问题描述

我目前正在尝试编写一个可以由我拥有的 python 文件调用的 C++ 方法。我正在使用 python 的ctypes库,并且在此过程中遇到了无数错误。到目前为止,我想要我的方法做的就是将一个数组作为其参数并返回一个数组作为结果。我已经阅读了 StackOverflow 上的无数线程,但找不到运行时没有错误的解决方案。这是我一直在尝试的。

import ctypes
import numpy as np

c_lib = ctypes.cdll.LoadLibrary(r"C:\Users\~user~\Desktop\~folder~\file.dll")

c_lib.method.restype = ctypes.POINTER(ctypes.c_int64) # result should be integer array (in this case it should be a 2D array with an unknown shape)
c_lib.method.argtypes = [np.ctypeslib.ndpointer(dtype=ctypes.c_int64), ctypes.c_int64] # the arguments should be an array (also with an unknown shape)

x = [[0,1], [2,3], [1,1], [86,100], [87,99], [100,100]] 
array = np.array(x, dtype = ctypes.c_int64) # make python list a numpy array

y = c_lib.method(array, 5)

目前我收到此错误:

OSError: exception: access violation reading 0x0000000000000000

这是我的 C++ 代码:

#include <vector>
#include <tuple>
#include <math.h>

using namespace std;

extern "C"{

vector<tuple<int, int>> group_coordinates(int** array, int number);

vector<tuple<int, int>> group_coordinates(int** coordinates, int max_deviation){
    vector<tuple<int, int>> group_coordinates(int** coordinates, float max_deviation){
    vector<vector<int>> mean_x_groups = {{coordinates[0][0]}};
    vector<vector<int>> mean_y_groups = {{coordinates[0][0]}};
    vector<tuple<int, int>> grouped_coordinates;
    
    int mean_x, mean_y;
    // this goes through the points, finds the if a point is within the max deviation of the mean coordinate (this mean updates as coordinates are added)
    // if a point doesn't fit the deviation than it will be added to a new group of its own
    for(int i = 0; i < sizeof(coordinates); i++)
    {
        int* point = coordinates[i];
        int len_mxg = mean_x_groups.size();
        int len_myg = mean_y_groups.size();
        tie(mean_x, mean_y) = mean_xy(mean_x_groups[len_mxg-1], mean_y_groups[len_myg-1]);
        if ((point[0] <= mean_x + max_deviation && point[0] >= mean_x - max_deviation) && (point[1] <= mean_y + max_deviation && point[1] >= mean_y - max_deviation)){
            // before a new group is added, the point will be checked to see if it can within any of the old groups
                    // if it is it will be added, if it isn't, a new group of its own ill be made
            for(int index = 0; index < len_mxg; index++){
                int mean_x, mean_y;
                tie(mean_x, mean_y) = mean_xy(mean_x_groups[index], mean_y_groups[index]);
                if ((point[0] <= mean_x + max_deviation && point[0] >= mean_x - max_deviation) && (point[1] <= mean_y + max_deviation && point[1] >= mean_y - max_deviation)){
                                mean_x_groups[index].push_back(point[0]);
                                mean_y_groups[index].push_back(point[1]);
                }
                else{
                    mean_x_groups.push_back({point[0]});
                    mean_y_groups.push_back({point[1]});
                    break;
                }
            }
        }
        else{
            mean_x_groups[len_mxg-1].push_back(point[0]);
            mean_y_groups[len_myg-1].push_back(point[1]);   
        }
    }
    // this uses the sorting above an retruns the mean (x, y) pair of each group
    // the enumerate here was me playing with the fuction, seemed to orgainze the sorting cleaner looking
    int len_mxg = mean_x_groups.size();
    int len_myg = mean_y_groups.size();
    for (int index = 0; index < len_mxg; index++){
            tie(mean_x, mean_y) = mean_xy(mean_x_groups[index], mean_y_groups[index]);
            grouped_coordinates.push_back(make_tuple(mean_x, mean_y));
    }
    return grouped_coordinates; 
}
}

我发现这个线程ctypes, python3.5, OSError: exception: access violation writing 0x00000000但无法使用答案做出适用的解决方案。

在这一点上,我不确定我是否应该继续阅读ctypes(虽然我不想放弃......但我已经尝试了无数不成功的解决方案)或者我是否应该尝试不同的 C++ Python 链接器。

如果有人知道我可以如何修复访问冲突错误,可以帮助我修复我的代码,或者可以为我提供更多资源来研究,那将不胜感激!

以下是我研究过的其他线程/资源:

如何使用 ctypes 将数组从 C++ 函数返回到 Python

https://www.semicolonworld.com/question/59536/how-to-return-array-from-c-function-to-python-using-ctypes

https://doc.sagemath.org/html/en/thematic_tutorials/numerical_sage/ctypes.html

https://nesi.github.io/perf-training/python-scatter/ctypes

Python 2D 数组 i C 使用 ctypes

标签: pythonc++arraysnumpyctypes

解决方案


推荐阅读