Analytics Vidhya
Published in

Analytics Vidhya

Video Chat using OpenCV and Socket

Photo by visuals on Unsplash

In this blog, we’re going to build the Video Chat without voice channel using OpenCV and Sockets in Python.

Let’s start with a plan:

  • Create a TCP socket for one to one communication.
  • Take the live stream from the camera.
  • Use multi-threading for a duplex channel.

OpenCV is the library used for image and video processing using Python. We can do anything related to images and videos using it. We crop, edit, create images and make videos.

Socket is the low-level networking interface that is used to connect two nodes on a network to communicate with each other.

Threading is the library used to create a multi-threaded program in python.

Let’s start implementing the plan:

Firstly we will create the TCP socket using the socket library using the following code:

  • Setting the protocol to TCP and address family to IPv4 address family.
# tcp and ipv4 address familytcp = socket.SOCK_STREAMafm = socket.AF_INET
  • Creating a socket using the TCP and AFM variables.
# creating socketsa = socket.socket(afm,tcp)sb = socket.socket(afm,tcp)

sa and sb are the sockets created to communicate between the two nodes. We can use a single socket to achieve the same too.

  • Binding the IP address with port number.
sa.bind((usera_ip,usera_port))
  • Start listening at the socket.
sa.listen()
  • Start accepting the connection from the node.
session, addr = sa.accept()

sa.accept() is used to accept the connection request from other nodes. session stores the session data and uses it to send and receive the request while addr is used to store the address of the receiver’s node.

  • Connect this node to the other node.
sb.connect((usera_ip,2001))

Then, we need to design the sophisticated receive and send for receiving and sending the video.

receive() function includes the infinite loop of receiving the video frame using the following block of code.

session.recv(buff_size) is used to receive the byte array from the node. Then, np.frombuffer() is used to store the byte back into the numpy arraay with np.uint8 data type which stores the value in the range of 0 to 255. Then, cv2.imdecode(image_arr, cv2.IMREAD_COLOR) is used to decode the image back to the colored image with cv2.IMREAD_COLOR arguement.

And if the image is empty with data type of Nonetype, the program will pass or else show the frames continuously using cv2.imshow(image_name, image) and cv2.waitKey(10) is used to make the frame wait for 10 milliseconds and exit if the keyword input is Enter(13) key.

cv2.destroyAllWindows() is used to destroy the image window completely and exit the thread using os._exit(0).

send() function includes the infinite loop of sending the video frame using the following block of code.

cv2.VideoCapture(0) is used to access the camera and the camera can be changed using the number passed as an arguement. capture stores the camera access methods like read() the input from the camera or release() is the camera.

capture.read() returns two variable, first as a bool value that either the picture is taken or not as true or false and second as the photo.

Then, if the ret is True encode the image using imencode(‘.jpg’,photo) and returns two variable and second variable as the encoded array and converted to the bytes using .tobytes which can be sent over the socket.

sendall(byte_encoded_array) sends all the data over the socket and data is sent to the destination node untill all the data is sent successfully to the destination node.

We need to create the threads for simultaneously receiving and sending the video using thread.Thread(target=func) with the target as send() and receive() giving functions to separate threads.

Then, we should run the programs on both the nodes. Here, I used the same IP address and different ports.

Whoa!! we did it together. Successfully created and executd the video chat program. You can check out the code using the below Github link.

If you find this interesting, give a 👏. Thanks for reading.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store