简体   繁体   English

python通过套接字接收图像

[英]python receive image over socket

I'm trying to send an image over a socket - I'm capturing an image on my raspberry pi using pycam, sending to a remote machine for processing, and sending a response back. 我正在尝试通过套接字发送图像-我正在使用pycam在树莓派上捕获图像,将其发送到远程计算机进行处理,然后将响应发送回去。

On the server (the pi), I'm capturing the image, transforming to an array, rearranging to a 1D array and using the tostring() method. 在服务器(pi)上,我正在捕获图像,转换为数组,重新排列为一维数组,并使用tostring()方法。

On the server, the string received is not the same length. 在服务器上,收到的字符串长度不同。 Any thoughts on what is going wrong here? 对这里出什么问题有任何想法吗? Attached is the code I'm running, as well as the output on both the server and the client 附带的是我正在运行的代码,以及服务器和客户端上的输出

SERVER CODE: 服务器代码:

from picamera.array import PiRGBArray
from picamera import PiCamera
import socket
import numpy as np
from time import sleep
import sys

camera = PiCamera()
camera.resolution = (640,480)

rawCapture = PiRGBArray(camera)

s = socket.socket()

host = 'myHost'
port = 12345
s.bind((host,port))
s.listen(1)
while True:
    c,addr = s.accept()

    signal = c.recv(1024)
    print 'received signal: ' + signal

    if signal == '1':
        camera.start_preview()
        sleep(2)

        camera.capture(rawCapture, format = 'bgr')
        image = rawCapture.array
        print np.shape(image)
        out = np.reshape(image,640*480*3)

        print out.dtype
        print 'sending file length: ' + str(len(out))

        c.send(str(len(out)))
        print 'sending file'
        c.send(out.tostring())


        print 'sent'

        c.close()
        break

CLIENT CODE: 客户代码:

import socket, pickle
import cv2
import numpy as np

host = '192.168.1.87'
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

s.send('1')

#while true:
x = long(s.recv(1024))
rawPic = s.recv(x)

print 'Received'
print x

print len(rawPic)
type(rawPic)

#EDITED TO INCLUDE DTYPE
image = np.fromstring(rawPic,np.uint8)
s.close()

SERVER OUTPUT: 服务器输出:

received signal: 1
(480, 640, 3)
uint8
sending file length: 921600
sending file

CLIENT OUTPUT: 客户输出:

Received
921600
27740
str

ValueError                                Traceback (most recent call last)
<ipython-input-15-9c39eaa92454> in <module>()
----> 1 image = np.fromstring(rawPic)

ValueError: string size must be a multiple of element size

I'm wondering if the issue is i'm calling tostring() on a uint8, and if the fromstring() is assuming it's a uint32? 我想知道问题是否是我在uint8上调用tostring(),并且fromstring()是否假设它是uint32? I can't figure out why the received string is so much smaller than what is sent. 我不知道为什么收到的字符串比发送的字符串小得多。

EDIT It seems for some reason the server is not fully sending the file. 编辑似乎由于某种原因,服务器未完全发送文件。 It never prints 'sent', which it should do at completion. 它从不打印“已发送”,它应该在完成时执行。 If I change the send line to: 如果我将发送行更改为:

c.send(str(len(out[0:100])))
print 'sending file'
c.send(out[0:100].tostring())

Everything works fine. 一切正常。 Thoughts on what could be cutting off my sent file midway through? 关于中途中断发送文件的想法?

Decoding to the Proper Type 解码为正确的类型

When you call tostring() , datatype (and shape) information is lost. 当您调用tostring() ,数据类型(和形状)信息会丢失。 You must supply numpy with the datatype you expect. 您必须为numpy提供所需的数据类型。

Ex: 例如:

import numpy as np

image = np.random.random((50, 50)).astype(np.uint8)
image_str = image.tostring()

# Works
image_decoded = np.fromstring(image_str, np.uint8)

# Error (dtype defaults to float)
image_decoded = np.fromstring(image_str)

Recovering Shape 恢复形状

If shape is always fixed, you can do 如果形状总是固定的,则可以

image_with_proper_shape = np.reshape(image_decoded, (480, 640, 3))

In the client. 在客户端。

Otherwise, you'll have to include shape information in your message, to be decoded in the client. 否则,您将必须在邮件中包含形状信息,以便在客户端中进行解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM