I am trying to use Mathematica as tcp server for computer vison, but I run into problem with speed. Mathematica recieves images from python and it takes very long time to accept the image, if I use C# to Python (full HD) everything works just fine. This is the code from Mathematica:
Port = 5005; Ip = "127.0.0.1"; message = 0; listener = "0"; outSocket = 0; startSession[] := ( listener = SocketListen[{Ip, Port}, recieveMessage, HandlerFunctionsKeys -> {"DataByteArray"}]; ) getMessageLength[msg_] := ( ImportByteArray[Part[msg, 1 ;; 4], "Integer32"][[1]] ) t = 0; messageBuffer = {}; msgLenght = 0; recieveMessage[x_] := Module[{sameLength}, If[Length[messageBuffer] == 0, messageBuffer = x["DataByteArray"]; msgLenght = getMessageLength[x["DataByteArray"]]; t = AbsoluteTime[]; Echo[msgLenght]; , messageBuffer = Join[messageBuffer, x["DataByteArray"]]; ]; sameLength = msgLenght == Length[messageBuffer]; If[sameLength, messageBuffer = {}; Echo[AbsoluteTime[] - t]; , Return[0]; ]; ] startSession[]
and here Python:
import socket import time import struct TCP_IP = '127.0.0.1' TCP_PORT = 5005 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TCP_IP, TCP_PORT)) message = "x" * (1920*1080*3) mess = struct.pack('i', (1920*1080*3 + 4) ) mess = mess + message s.send(mess) s.close()
The main problem is not join, even if I do not store recieved data it does not change the time much and is still around 9 seconds.
Do you have any idea how to speed it up? I need to get to times below 0.1 seconds per image. Python and Mathematica run on the same computer.
Thank you for any ideas.