title: Python frame data optimization before sending through socket
body: <p>Following step 6 of <a href="https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/" rel="nofollow noreferrer">Adrian's guide</a> and some others, I managed to stream 320x240 frames with a speed of 10 fps and 0.1 s latency from my raspberry pi to my laptop. The problem is, when I test this system in my lab (which is equipped with an antique router), it can only stream 1-2 fps with a 1-1.5 second latency, which is totally unacceptable for what I intend to do with those frames. </p>
  
  <p>Right now, my method is simple and straight forward: the server on the raspberry pi capture a frame and store it as a 320x240x3 matrix like the guide mentioned above, then pickle that matrix and keep pumping it over a TCP socket. The client on the laptop keep receiving these frames, do some processing on them, and finally show the result with imshow. My code is rather long for a post (around 200 lines) so I would rather avoid showing it if I can.</p>
  
  <p>Is there any way to reduce the size of each frame's data (the pickled 320x240x3 matrix, its length is 230 kB) or is there a better way to transmit that data? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Okay guys, the exact presented length of the pickled array is 230563 bytes, and the payload data should be at least 230400 bytes so overhead should be no more than 0.07% of the total package size. I think this narrows the problem down to wireless connection quality and the method for encoding the data to bytes (pickling seems to be slow). The wireless problem can be solved by creating ad-hoc network (sounds interesting but I have not tried this yet) or simply buying a better router, and the encoding problem can be solved with Aaron's solution. Hope that this will help future readers :)</p>