0

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.

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)

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'[[[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.

5
  • 1
    Can you provide an example of the string you're getting? Commented Jan 2, 2019 at 9:33
  • I have posted an example Commented Jan 2, 2019 at 9:43
  • It seems like you're looking for ways to serialize Commented Jan 2, 2019 at 9:43
  • See the '...'? That's missing data. You can't recover the original from this. str is the wrong way to serialize an array. Commented Jan 2, 2019 at 11:54
  • See a related question and its answers here stackoverflow.com/questions/38886641/… Commented Sep 22, 2022 at 4:12

2 Answers 2

2

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()

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I think that the numpy.fromstring function does exactly what you want.

For example the np.fromstring('1 2', dtype=int, sep=' ') call returns this array: array([1, 2]).

Hope it helps.

1 Comment

That's not what his string looks like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.