将字符串转换回numpy数组 [英] Convert string back to numpy array
问题描述
我正在使用python进行视频聊天.我正在使用CV2捕获当前图像(这给了我一个numpy数组),然后将numpy发送到我的服务器.现在我在服务器上有一个字符串,我需要将其解码回一个numpy数组.
I'am working on a video chat in 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. Now I have a string at the server and i need to decode it back to a numpy array.
我正在使用Python 3.7,但还没有想到.
I'am using Python 3.7 and i dindn't come up with somethink yet.
#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)
我只需要一种将字符串转换回numpy数组的方法. 如果我将其打印出来,则字符串看起来像这样:
I only need a method to convert the string back to a numpy array. If i print it out,the string looks like this:
b'[[[[128255255]] \ n [125255255] \ n [107255255] \ n ... \ n [102130167] \ n [102128172] \ n [102128 172]] \ n \ n [[128 255 255] \ n [127255255] \ n [108255255] \ n ... \ n [102130167] \ n [102128172] \ n [102 128 172]] \ n \ n [[111 255 255] \ n [111 255 255] \ n [109255255] \ n ... \ n [99131169] \ n [99131169] \ n [ 99 131 169]] \ n \ n ... \ n \ n [[27 64 95] \ n [29 67 97] \ n [24 66 98] \ n ... \ n [73117160] \ n [70 119 161] \ n [70 119 161]] \ 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]]]'
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]]]'
对不起,我的英语不好,我是德国学生.
Sorry for my bad english, I'am a german student.
推荐答案
以下一对程序演示了一种通过网络套接字通信numpy ndarray
对象的方法.客户端使用save
方法将数组转换为字节流,将流写入BytesIO对象,然后通过套接字将其发送到服务器:
The following pair of programs demonstrates one way to commmunicate a numpy ndarray
object across a network socket. 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:
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()
服务器在适当的网络地址上侦听,接收数据,直到结束套接字关闭为止.然后,它将接收到的数据转换为BytesIO
对象,numpy的load
函数从该对象恢复结构:
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:
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.
这篇关于将字符串转换回numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!