1

I want to send and receive image from cv2.Videocapture using WebSocket.

It could get json, but it couldn't decoded.

We need result that can be opened using cv2.imshow().

Somebody help me...

This is Client

ret, image_np = cap.read()
        IMAGE_SHAPE = image_np.shape
        encoded_image = base64.b64encode(image_np)
        print(type(encoded_image))
        payload = {
            'from': 'rasp',
            'image': str(encoded_image),
            'shape': IMAGE_SHAPE,
        }

        data = json.dumps(payload)

        try:
            # Send encoded image data.
            await websocket.send(data)

            # receive server message
            received_data = await websocket.recv()
            print('< {}'.format(received_data))

            # image = base64.b64decode(received_data)
            # np_image = np.fromstring(image, dtype=np.uint8)
            # source = np_image.reshape(IMAGE_SHAPE)
            return websocket
        except Exception:
            print('WebSocket send or receive error.')
            exit(1)

This is Server

async def server_on(websocket, path):
payload = {
    'from': 'image-server',
    # 'result': {
    #     # Default is null.
    #     'isPerson': <bool>,
    #     'centerPoint': <(x, y)>,
    # },
}

data = json.dumps(payload)

try:
    payload = await websocket.recv()
    receive_data = json.loads(payload)
    # At this line doesnt work... 
    decoded_image = base64.b64decode(receive_data['image'])
    image_np = np.fromstring(decoded_image, dtype=np.uint8)
    source = image_np.reshape(receive_data['shape'])


    await websocket.send(data)
except Exception:
    websocket.close()
    return
2
  • Is the payload encoded exactly the same as the one received? Commented May 10, 2018 at 10:39
  • I found that str(encoded_image) increase 3bytes compared with encoded_image. But, i don't know how to correctly change bytes to string to send with JSON Commented May 10, 2018 at 13:05

1 Answer 1

1

In your Client I would say that you have an extra operation not needed. Based on your latest comment, you might not need to use: str(encoded_image).

You could try to use: base64.encodestring(image_np) that will send you back a string container.

ret, image_np = cap.read()
        IMAGE_SHAPE = image_np.shape
        encoded_image = base64.encodestring(image_np)
        print(type(encoded_image))
        payload = {
            'from': 'rasp',
            'image': encoded_image.decode('utf-8'),
            'shape': IMAGE_SHAPE,
        }
Sign up to request clarification or add additional context in comments.

4 Comments

I'm really really sorry. Too late. I did that but had log: 'TypeError: Object of type 'bytes' is not JSON serializable'.. Maybe because it is bytes. I'm trying to fixing it about .... 1 and half weeks
I understand it better. You have bytes and not string. Can you try to use base64.encodestring()?
It doesn't work too. Exactly what i want to know is that why it increase 3byte .. bytes -> string. I think there is answer.. I'm very appreciate for your help
Fix it! Thank for your help Like this way, encoded_image = base64.b64encode(image_np) payload = { 'from': 'rasp', 'image': encoded_image.decode('utf-8'), 'shape': IMAGE_SHAPE, }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.