Way to transfer OpenCV Mat to iPhone
<p>I'm doing a computer vision project on my RaspberryPi 2.
The computer vision is done through the well known OpenCV library with Python bindings.
I want to show a live stream of what the Raspberry Pi is doing with an iOS app.</p>

<p>The images being elaborated by the Raspberry Pi are OpenCV Mats that in Python are no less then Numpy Arrays.
The iPhone app on the other end doesn't have OpenCV elaboration capabilities and it can only work with images in my logic.</p>

<p>Now, while i'm designing this thing, I can't figure out the best way to do so.</p>

<p>I would separate the problem this way:</p>

<ol>
<li>Best way to transfer information: TCP Server Socket connection or Web APIs to download somehow the new images?</li>
<li>Format of the information I'm sending: Multidimentional array of values (taken directly from the Mat), and in this case how to decode the information on the iPhone, or convert the mat to an image with OpenCV on the Raspberry Pi and then store it somewhere and send it (It seems messy and slow to me)?</li>
</ol>

<p>What are your opinions on this?</p>

<p>Thanks in advance.</p>

<p>I solved the problem this way:</p>

<ol>
<li>Encoded the image Mat with <code>cv2.imencode(extension, mat)</code> function which returns a byte array with the encoded image.</li>
<li>Use TCP Socket to send the generated byte array.</li>
</ol>

<p>Specifically:</p>

<p>The function used for conversion is</p>

<pre><code>def mat_to_byte_array(mat, ext):
    success, img = cv2.imencode(ext, mat)
    bytedata = img.tostring() # numpy function used for casting to bytearray
    return success, bytedata
</code></pre>

<p>On iOS I used <a href="https://github.com/swiftsocket/SwiftSocket" rel="nofollow">SwiftSocket</a> Library to implement the TCP Client.</p>

<p>It all works fine and smoothly now.</p>

<p>Thanks anyway.</p>

python ios swift opencv raspberry-pi