开发者

Passing a numpy array to Delphi dll

开发者 https://www.devze.com 2023-03-03 16:44 出处:网络
I am trying to implement an image classification algorithm in Python. The problem is that python takes very long with looping through the array. That\'s why I decided to write a Delphi dll which perfo

I am trying to implement an image classification algorithm in Python. The problem is that python takes very long with looping through the array. That's why I decided to write a Delphi dll which performs the 开发者_如何学JAVAarray-processing. My problem is that I don't know how to pass the multidimensional python-array to my dll-function.

Delphi dll extract: (I use this function only for testing)

type
    TImgArray = array of array of Integer;

function count(a: TImgArray): Integer; cdecl;
begin
    result:= high(a);
end;

relevant Python code:

arraydll = cdll.LoadLibrary("C:\\ArrayFunctions.dll")

c_int_p = ctypes.POINTER(ctypes.c_int32)
data = valBD.ReadAsArray()
data = data.astype(np.int32)
data_p = data.ctypes.data_as(c_int_p)

print arraydll.count(data_p)

The value returned by the dll-function is not the right one (it is 2816 instead of 7339). That's why I guess that there's somethin wrong with my type-conversion :( Thanks in advance, Mario


What you're doing won't work, and is likely to corrupt memory too.

A Delphi dynamic array is implemented under the hood as a data structure that holds some metadata about the array, including the length. But what you're passing to it is a C-style pointer-as-array, which is a pointer, not a Delphi dynamic array data structure. That structure is specific to Delphi and you can't use it in other languages. (Even if you did manage to implement the exact same structure in another language, you still couldn't pass it to a Delphi DLL and have it work right, because Delphi's memory manager is involved under the hood. Doing it this way is just asking for heap corruption and/or exceptions being raised by the memory manager.)

If you want to pass a C-style array into a DLL, you have to do the C way, by passing a second parameter that includes the length of the array. Your Python code should already know the length, and it shouldn't take any time to calculate it. You can definitely use Delphi to speed up image processing. But the array dimensions have to come from the Python side. There's no shortcut you can take here.

Your Delphi function declaration should look something like this:

type
  TImgArray = array[0..MAXINT] of Integer;
  PImgArray = ^TImgArray;

function ProcessSomething(a: PImgArray; size: integer): Integer; cdecl;


Normal Python arrays are normally called "Lists". A numpy.array type in Python is a special type that is more memory efficient than a normal Python list of normal Python floating point objects. Numpy.array wraps a standard block of memory that is accessed as a native C array type. This in turn, does NOT map cleanly to Delphi array types.

As David says, if you're willing to use C, this will all be easier. If you want to use Delphi and access Numpy.array, I suspect that the easiest way to do it would be to find a way to export some simple C functions that access the Numpy.array type. In C I would import the numpy headers, and then write functions that I can call from Pascal. Then I would import these functions from a DLL:

function GetNumpyArrayValue( arrayObj:Pointer;  index:Integer):Double;

I haven't written any CPython wrapper code in a while. This would be easier if you wanted to simply access CORE PYTHON types from Delphi. The existing Python-for-delphi wrappers will help you. Using numpy with Delphi is just a lot more work.

Since you're only writing a DLL and not a whole application, I would seriously advise you forget about Delphi and write this puppy in plain C, which is what Python extensions (which is what you're writing) really should be written in.

In short, since you're writing a DLL, in Pascal, you're going to need at least another small DLL in C, just to bridge the types between the Python extension types (numpy.array) and the Python floating point values. And even then, you're not going to easily (quickly) get an array value you could read in Delphi as a native delphi array type.

The very fastest access mechanism I can think of is this:

type
   PDouble = ^Double;

function GetNumpyArrayValue( arrayObj:Pointer; var doubleVector:PDouble; var doubleVectorLen:Integer):Boolean;

You could then use doubleVector (pointer) type math to access the underlying C array memory type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号