Skip to content

Send Python Numpy Array over TCP/IP socket with simple codes.

License

Notifications You must be signed in to change notification settings

ekbanasolutions/numpy-using-socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NpSocket

About

This python module was built to transfer the Numpy array over a TCP/IP socket. There is Numpy Socket package orginally written by @sabjorn to stream video frames from OpenCV which only works for python2. But I couldn't find the decent solution for python3. So based on a thread in stackoverflow, this module was written to send the numpy array over the TCP/IP sokcet. This module make use of the code sample of the answer given by @nareddyt in the same stackoverflow thread.

I'm actually using this module to send the video frame captured through opencv, make a prediction on the image, and send back the modified image to the Pi client.

Installation

  1. First clone this repository.
  2. cd into the cloned directory.
  3. Run python3 setup.py install in the terminal.
  4. You should have a working NpSocket module.
  5. Varify this by running the following commands in terminal:
>>> import npsocket
>>> from npsocket import SocketNumpyArray
>>> exit() 

Examples:

  1. Initalize the Receiver:
from npsocket import SocketNumpyArray
sock_receiver = SocketNumpyArray()
sock_receiver.initalize_receiver(9999) # the 9999 is the port you can change it with your own. 
while True:
    frame = sock_receiver.receive_array()  # Receiving the image as numpy array. 
    # Display
    cv2.imshow('frame', frame)
    cv2.waitKey(1)
  1. Intialize the Sender:
from npsocket import SocketNumpyArray
import cv2

cap = cv2.VideoCapture(0)
sock_sender = SocketNumpyArray()

sock_sender.initialize_sender('localhost', 9999)

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, (620, 480))
    sock_sender.send_numpy_array(frame)
  1. Run the 'reciver' for and the the 'sender'. You should be able to see the frame being sent by the sender and displayed by the receiver in the CV2 window.

In the same way, you should be able to transfer any Numpy Array. There is an example files in the videostream directory, where the above example is shown. If you simply want to run and see it, then you can first run python3 receiver.py and then python3 sender.py and see it working.

Contributors

  1. Bibek
  2. Vaghawan

About

Send Python Numpy Array over TCP/IP socket with simple codes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages