简体   繁体   English

将字符串转换回numpy数组

[英]Convert string back to numpy array

I'am working on a video chat in python. 我正在用python进行视频聊天。 I'am using CV2 to capture the current image(this gives me a numpy array), then I send the numpy over to my server. 我正在使用CV2捕获当前图像(这给了我一个numpy数组),然后将numpy发送到我的服务器。 Now I have a string at the server and i need to decode it back to a numpy array. 现在我在服务器上有一个字符串,我需要将其解码回numpy数组。

I'am using Python 3.7 and i dindn't come up with somethink yet. 我正在使用Python 3.7,但我还没有想到。

    #client 
    #capture 500 frames
    while i < 500:
        i = i + 1
        # Capture frame-by-frame
        ret, frame = cap.read()
        #send data                           
        client_socket.send(bytes(str(ret) + "###" + str(frame), "utf-8"))


    #server
    #split ret and frame
    ret, frame = str(conn.recv(16000)).split("###")
    gray = cv2.cvtColor(frame.toNumpyArray #PseudoMethod  <-- Here
    ,cv2.COLOR_BGR2GRAY)

I only need a method to convert the string back to a numpy array. 我只需要一种将字符串转换回numpy数组的方法。 If i print it out,the string looks like this: 如果我打印出来,字符串看起来像这样:

b'[[[128 255 255]\\n [125 255 255]\\n [107 255 255]\\n ...\\n [102 130 167]\\n [102 128 172]\\n [102 128 172]]\\n\\n [[128 255 255]\\n [127 255 255]\\n [108 255 255]\\n ...\\n [102 130 167]\\n [102 128 172]\\n [102 128 172]]\\n\\n [[111 255 255]\\n [111 255 255]\\n [109 255 255]\\n ...\\n [ 99 131 169]\\n [ 99 131 169]\\n [ 99 131 169]]\\n\\n ...\\n\\n [[ 27 64 95]\\n [ 29 67 97]\\n [ 24 66 98]\\n ...\\n [ 73 117 160]\\n [ 70 119 161]\\n [ 70 119 161]]\\n\\n [[ 18 71 81]\\n [ 20 74 83]\\n [ 30 67 93]\\n ...\\n [ 77 117 159]\\n [ 74 118 163]\\n [ 74 118 163]]\\n\\n [[ 14 68 77]\\n [ 19 73 82]\\n [ 30 67 93]\\n ...\\n [ 77 117 159]\\n [ 74 118 163]\\n [ 74 118 163]]]' b'[[[128255255] \\ n [125255255] \\ n [107255255] \\ n ... \\ n [102130167] \\ n [102128172] \\ n [102128172]] \\ n \\ n [[128 255 255] \\ n [127255255]] \\ n [108255255] \\ n ... \\ n [102130167] \\ n [102128172] \\ n [102128172] ] \\ n \\ n [[111255255] \\ n [111255255] \\ n [109255255] \\ n ... \\ n [99131169] \\ n [99131169] \\ n [99131169 ]] \\ n \\ n ... \\ n \\ n [[27 64 95] \\ n [29 67 97] \\ n [24 66 98] \\ n ... \\ n [73117160] \\ n [70119 161] \\ n [70119161]] \\ n \\ n [[18 71 81] \\ n [20 74 83] \\ n [30 67 93] \\ n ... \\ n [77117159] \\ n [74 118 163] \\ n [74 118 163]] \\ n \\ n [[14 68 77] \\ n [19 73 82] \\ n [30 67 93] \\ n ... \\ n [77117159] \\ n [ 74 118 163] \\ n [74 118 163]]]'

Sorry for my bad english, I'am a german student. 对不起,我的英语不好,我是德国学生。

The following pair of programs demonstrates one way to commmunicate a numpy ndarray object across a network socket. 以下一对程序演示了一种在网络套接字上通信numpy ndarray对象的方法。 The client converts the array to a byte stream using the save method, writing the stream to a BytesIO object that is then sent across the socket to the server: 客户端使用save方法将数组转换为字节流,将流写入BytesIO对象,然后通过套接字将其发送到服务器:

import numpy as np
import socket
from io import BytesIO

# Create an output socket connected to server
sout = socket.create_connection(('127.0.0.1', 6543))

# Create data and write binary stream out to socket

a = np.array([[1.1, 2.2, 3.3],
              [4.4, 5.5, 6.6],
              [7.7, 8.8, 9.9]])

b = BytesIO()
np.save(b, a)

sout.send(b.getvalue())
sout.close()

The server listens on the appropriate network address, receiving data until the ending socket closes. 服务器侦听适当的网络地址,接收数据,直到结束套接字关闭。 It then converts the received data into a BytesIO object, from which numpy's load function recovers the structure: 然后,它将接收到的数据转换为BytesIO对象,numpy的load函数从该对象恢复结构:

import numpy as np
import socket
from io import BytesIO

# Create socket listening on correct port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 6543))
s.listen()

# Accept a connection and receive data
sock, address = s.accept()
data = b''
while True:
    indt = sock.recv(1024)
    if not indt:
        break
    data += indt

# Take data and recast to np.ndarray
data = BytesIO(data)

b = np.load(data)
print(type(b), b, sep='\n')

The output from running the server is as follows: 运行服务器的输出如下:

<class 'numpy.ndarray'>
[[1.1 2.2 3.3]
 [4.4 5.5 6.6]
 [7.7 8.8 9.9]]

There are various ways in which this code could be optimised, but this should give you enough to get going. 有多种方法可以优化此代码,但这应该给您足够的动力。

I think that the numpy.fromstring function does exactly what you want. 我认为numpy.fromstring函数正是您想要的。

For example the np.fromstring('1 2', dtype=int, sep=' ') call returns this array: array([1, 2]) . 例如, np.fromstring('1 2', dtype=int, sep=' ')调用返回此数组: array([1, 2])

Hope it helps. 希望能帮助到你。

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

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