我正在用Python编写TCP客户端-服务器系统的代码。服务器是带有Pi相机的Raspberry PI 3,客户端是远程PC,可通过Tkinter GUI和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)
到目前为止,一切正常。
当我想通过TCP套接字发送RAW图像时出现问题。 我使用以下代码获取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方法打包(服务器端)和解包(客户端)“输出”?是否还有另一种通过TCP套接字发送该数据的方法?
谢谢!