gpt4 book ai didi

python - 从 Raspberry Pi 捕获 jpeg 图像并将其发送到 PC socket python?

转载 作者:可可西里 更新时间:2023-11-01 02:53:27 24 4
gpt4 key购买 nike

我想将从 Rasberry pi 相机捕获的图像像流一样实时发送到 PC 但不是流,因为我没有那么多带宽。我现在的代码是。

客户端

import io
import cv2
import socket
import struct
from PIL import Image
import numpy

# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
cv2.namedWindow('Network Image')
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8200))
server_socket.listen(0)

# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')
try:
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)
image = Image.open(image_stream).convert('RGB')
open_cv_image = numpy.array(image)
open_cv_image = open_cv_image[:, :, ::-1].copy()
cv2.imshow('Network Image',open_cv_image)
cv2.waitKey(0)
print('Image is %dx%d' % image.size)
image.verify()
print('Image is verified')
finally:
connection.close()
server_socket.close()

服务器

import io
import socket
import struct
import time
import picamera

# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
client_socket = socket.socket()
client_socket.connect(('192.168.2.225', 8200))

# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
with picamera.PiCamera() as camera:
camera.framerate=15
camera.resolution = (480, 480)
# Start a preview and let the camera warm up for 2 seconds
camera.start_preview()
time.sleep(2)

# Note the start time and construct a stream to hold image data
# temporarily (we could write it directly to connection but in this
# case we want to find out the size of each capture first to keep
# our protocol simple)
start = time.time()
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())
# If we've been capturing for more than 30 seconds, quit
if time.time() - start > 30:
break
# 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))
finally:
connection.close()
client_socket.close()

我可以通过某种方式提高效率吗?理想情况下,我想捕获一个帧并另存为 jpeg,然后以 30 FPS 的速度传输到客户端。现在我得到大约 8 FPS。

最佳答案

使用

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for foo in camera.capture_continuous(stream, 'jpeg', use_video_port = True):

关于python - 从 Raspberry Pi 捕获 jpeg 图像并将其发送到 PC socket python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46726757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com