1

I am connecting to client-2 from client-1 and client-2 to server. I am sending frames from the client-1 to client-2 and on client-2, I am performing prediction and sending the result to the server.I have the following code.

Client-1 code:

  context = zmq.Context()
  footage_socket = context.socket(zmq.PUB)
  footage_socket.connect('tcp://172.168.1.2:5555')
  videoFile = 'data.mp4'
  camera = cv2.VideoCapture(videoFile) 
  length=int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
  print(length)
  count=0
  #time.sleep(2)
  while True:        
    grabbed, frame = camera.read()
    count+=1
    print(count)
    try:
       frame = cv2.resize(frame, (224, 224))
    except cv2.error:
        break
    encoded, buffer = cv2.imencode('.jpg', frame)
    jpg_as_text = base64.b64encode(buffer)
    footage_socket.send(jpg_as_text)

Client-2 code:

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://0.0.0.0:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
while True:    
    frame = footage_socket.recv_string()
    img = base64.b64decode(frame)
    npimg = np.fromstring(img, dtype=np.uint8)
    source = cv2.imdecode( npimg, 1 )
    frame=cv2.resize(source,(224,224)).astype("float32")
    image = img_to_array( source)
    image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2]) )
    image = preprocess_input( image )
    preds = model.predict(image)
    ##connecting to server##        
    context1=zmq.Context()
    footage_socket=context1.socket(zmq.PUB)
    footage_socket.connect('tcp://192.168.56.103:9999')
    footage_socket.send(preds)
    print('sending to server')

Server code:

   context = zmq.Context()
   footage_socket = context.socket(zmq.SUB)
   footage_socket.bind('tcp://0.0.0.0:9999')
   footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
   while True:
      frame = footage_socket.recv_string()
      img = base64.b64decode(frame)
      #print(img)

On client-2 I am receiving the below error

    frame = footage_socket.recv_string()
  File "/usr/local/lib/python3.5/dist-packages/zmq/sugar/socket.py", line 583, in recv_string
    msg = self.recv(flags=flags)
  File "zmq/backend/cython/socket.pyx", line 790, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 826, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 193, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/socket.pyx", line 188, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Operation not supported
halfer
  • 19,824
  • 17
  • 99
  • 186
Mazia
  • 63
  • 1
  • 10

1 Answers1

1

Several sins, let's demask one after another:

Client-1 could be improved, yet the Client-2 has most of the problems:

################################################################### FOOTAGE ~ <SUB>-Socket
# SUB
footage_socket = context.socket( zmq.SUB )
...
PUB_TARGET = 'tcp://192.168.56.103:9999'
while True:    
    frame  = footage_socket.recv_string()                         # <SUB>.recv()-ed
    source = cv2.imdecode( np.fromstring( base64.b64decode( frame ),
                                          dtype = np.uint8
                                          ),
                           1 )
    frame  = cv2.resize( source,
                         (224,224)
                         ).astype( "float32" )
    image = img_to_array( source )
    image = image.reshape( ( 1,
                             image.shape[0],
                             image.shape[1],
                             image.shape[2]
                             )
                           )
    preds = model.predict( preprocess_input( image ) )
    ################################################################## PER-LOOP INFty-times
    ## connecting to server ###########################
    context1=zmq.Context()                           ## INSTANTIATED new Context()-instance
    footage_socket = context1.socket( zmq.PUB )      ## ASSOCIATED a new  Socket()-instance
    footage_socket.connect( PUB_TARGET )             ## .CONNECT( PUB_TARGET )
    footage_socket.send( preds )                     ## <PUB>.send()
    ################################################### LOOP AGAIN
    ###################################################      yet now the <PUB>.recv()

a) while True:-code-block creates as many Context()-instances, having all allocations and at least 1-I/O-thread, as many times the loop runs through

b) trailing part of the loop assigns footage_socket with a new object (another new instance of a socket-class), rendering the original object-reference to a SUB-type socket-instance an orphan, yet having all associated resources' allocations unterminated

c) a newly re-assigned footage_socket-socket, now bearing a reference of a PUB-type socket-instance indeed cannot process a .send()-method, as scripted in the head of the while True:-code-block and an (unhandled) exception gets thrown, as has been demonstrated above.


Solution?

Remove these conceptual errors - avoiding colliding names for both an intended SUB and a new PUB and avoid making the code repeatedly (ad infinitum) generating new and new and new Context()-instances (these operations are resources-wise and latency-wise expensive) and its infinitely many socket(PUB)-instances and infinitely many .connect()-s thereof & the code shall otherwise work as expected.


Would you like to read more help on ZeroMQ?

Then feel free to read this answer.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92
  • The [advice here](https://stackoverflow.com/a/64623700) is still very pertinent: (_Please don't use an answer for Meta commentary. If you have any issues with how the community dealt with your answer, please post them on Meta.SO)._ Others users may chip in with their own feedback, but I am also happy to give a fulsome explanation, which builds on the various justifications I have thus far provided. – halfer Nov 05 '20 at 23:27
  • 1
    Hi, thank you very much for your wonderful help,@user3666197, I am still facing the same issue? – Mazia Nov 06 '20 at 10:38
  • 1
    @Mazia How did you change the faulty code? Would you mind to post a new Question with a reproducible MCVE-code that demonstrates the remaining issues? – user3666197 Nov 06 '20 at 11:48