Luca Angioloni Luca Angioloni - 8 days ago 4x
iOS Question

Way to transfer OpenCV Mat to iPhone

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.

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.

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

I would separate the problem this way:

  1. Best way to transfer information: TCP Server Socket connection or Web APIs to download somehow the new images?

  2. 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)?

What are your opinions on this?

Thanks in advance.


I solved the problem this way:

  1. Encoded the image Mat with cv2.imencode(extension, mat) function which returns a byte array with the encoded image.
  2. Use TCP Socket to send the generated byte array.


The function used for conversion is

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

On iOS I used SwiftSocket Library to implement the TCP Client.

It all works fine and smoothly now.

Thanks anyway.