SWIG Python C++ output array giving 'unknown type' error
<p>I am trying to use swig to wrap some c++ code to pass a numpy array back to python. I am following some examples I have seen online to use numpy.i. Here is what my code looks like.</p>

<p>I am using this as the function definition in my class header file:</p>

<pre><code>bool grabFrame(int buf_size, unsigned char *buf);
</code></pre>

<p>In my interface file I have:</p>

<pre><code>/* File : OV4682Interface.i */
%module OV4682Interface
%include "std_string.i"

%{
    #define SWIG_FILE_WITH_INIT
    #include "OV4682FrameGrabber.h"
%}

%include "numpy.i"
%init %{
    import_array();
%}

%apply (int DIM1, unsigned char* ARGOUT_ARRAY1) {(int buf_size, unsigned char *buf)};

%include "../inc/OV4682FrameGrabber.h"
</code></pre>

<p>My Python code looks like this:</p>

<pre><code>import numpy as np
import OV4682Interface as ov

width = 672
height = 380
buf_size = width*height*2

buf = np.zeros(buf_size, dtype=np.uint8)

grab = ov.OV4682FrameGrabber()
grab.grabFrame(buf)
</code></pre>

<p>When I run this I get the following error:</p>

<p>Traceback (most recent call last):
  File "OV4682FrameGrabberTest.py", line 44, in 
    grab.grabFrame(buf)
  File "/home/ubuntu/rgb_ir_frame_grabber/build/lib/OV4682Interface.py", line 117, in grabFrame
    def grabFrame(self, *args): return _OV4682Interface.OV4682FrameGrabber_grabFrame(self, *args)
TypeError: Int dimension expected.  'unknown type' given.</p>

<p>For some reason I am getting an error saying the type is unknown for the passed in array, but I have explicitly set the dtype to be np.uint8. I was wondering if anyone can point me to what I am doing incorrectly here as I am a bit stumped.</p>

<p>Looks like I did not look closely enough at the answer for this post (<a href="https://stackoverflow.com/questions/36222455/swigcpython-passing-and-receiving-c-arrays">SWIG+c+Python: Passing and receiving c arrays</a>). I did not notice that swig adds the output array to the return list and just wants the size of the array passed in. </p>

<p>I changed the method definition to this:</p>

<pre><code>void grabFrame(int buf_size, unsigned char *buf);
</code></pre>

<p>and I changed the python call to be this:</p>

<pre><code>buf = grab.grabFrame(np.shape(buf)[0])
</code></pre>

<p>This now works and returns the array of data I wanted.</p>

python c++ numpy swig