отправлять данные через TCP-сокет - PullRequest
0 голосов
/ 23 февраля 2019

Я кодирую систему клиент-сервер TCP на Python.Сервер - Raspberry PI 3 с камерой Pi, а клиент - удаленный ПК, который управляет всем с помощью графического интерфейса Tkinter и сокета zmq.Сервер непрерывно получает поток и отправляет его через сокет в соответствии с процедурами, перечисленными здесь: https://picamera.readthedocs.io/en/release-1.13/recipes1.html#capturing-to-a-stream

На стороне сервера:

stream = io.BytesIO()
for foo in camera.capture_continuous(stream, 'jpeg'):
    # Write the length of the capture to the stream and flush to
    # ensure it actually gets sent
    connection.write(struct.pack('<L', stream.tell()))
    connection.flush()
    # Rewind the stream and send the image data over the wire
    stream.seek(0)
    connection.write(stream.read())
    # Reset the stream for the next capture
    stream.seek(0)
    stream.truncate()
    # Write a length of zero to the stream to signal we're done
connection.write(struct.pack('<L', 0))

На стороне клиента:

while True:
    # Read the length of the image as a 32-bit unsigned int. If the
    # length is zero, quit the loop
    image_len = struct.unpack('<L', 
    connection.read(struct.calcsize('<L')))[0]
    if not image_len:
        break
    # Construct a stream to hold the image data and read the image
    # data from the connection
    image_stream = io.BytesIO()
    image_stream.write(connection.read(image_len))
    # Rewind the stream, open it as an image with PIL and do some
    # processing on it
    image_stream.seek(0)

Здесь все работает нормально.

Проблема возникла, когда я захотел отправить изображение RAW через сокет TCP.Я получаю данные RAW, используя этот код:

with picamera.PiCamera() as camera:
with picamera.array.PiBayerArray(camera) as stream:
    camera.capture(stream, 'jpeg', bayer=True)
    # Demosaic data and write to output (just use stream. array if you
    # want to skip the demosaic step)
    output = (stream.demosaic() >> 2).astype(np.uint8)
    with open('image.data', 'wb') as f:
        output.tofile(f)

полученный из: https://picamera.readthedocs.io/en/release-1.13/recipes2.html#raw-bayer-data-captures

Я хочу использовать перечисленную выше схему клиент-сервер, чтобы отправить переменную с именем'output', который, я думаю, содержит байтовый массив (или массив numpy?).Как я могу использовать метод struct.pack для упаковки (на стороне сервера) и распаковки (на стороне клиента) 'output'?Есть ли другой способ отправить эти данные через сокет TCP?

1019 * Спасибо!
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...