Video Streaming Using Python

In this article, we are going to Create a Video Chat App Using Python OpenCV and Socket Programming with an easy step-by-step tutorial.

For this practical, I am going to use Python OpenCV and Socket Programming so let’s first understand Python OpenCV and Socket Programming.

Python OpenCV

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation.

Socket Programming

Socket programming is a way of connecting two or more nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other sockets reach out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. They are the real backbones behind web browsing. In simpler terms, there is a server and a client.

Server

A server has a bind() method which binds it to a specific IP and port so that it can listen to incoming requests on that IP and port. A server has a listen() method which puts the server into listening mode. This allows the server to listen to incoming connections. And last a server has an accept() and close() method. The accept method initiates a connection with the client and the close method closes the connection with the client.

Prerequisite

  • Python and Necessary Libraries Installed in your Favourite OS (Numpy and OpenCV).

Python Code for Video Chat Application:

Here we are going to write the python code for Video Chat Server Side.

First, we have to create two python programming files one Client.py and another Server.py

Before that please make sure your IP address putting given below IP address won’t work for your local device, If you don’t have your IP address just open CMD(command prompt) and write the highlighted part:

Client.py:

Before running this code lets us set the server-side so that client can make end-to-end connections.

Server.py

Video-Server.py file

  • We have to import Some Python Libraries to make our chat app those Libraries are cv2, socket, NumPy, and pickle.
import cv2, socket, numpy, pickle

This is a Part where we are creating a network socket using the python socket library to make our work easier.

  1. We just need to specify some parameters inside predefined functions. Our Chat app will use UDP protocol due to some reasons
  2. UDP is commonly used for applications that are “lossy” (can handle some packet loss), such as streaming audio and video. It is also used for query-response applications, such as DNS queries.
  3. That’s why I used a socket.sock_DGRAM parameter as it means we need UDP protocol.
  4. After this, we are required to give the IP address and Port Number on which we need our Chat App to make a Connection. Here I have used My Windows Private IP because I am testing it locally
  5. At last, we need to use the bind function to bind our IP address and Port. So a Connection can be established
  6. The bind() method is used when a socket needs to be made a server socket. As server programs listen on published ports, it is required that a port and the IP address be assigned explicitly to a server socket. For client programs, it is not required to bind the socket explicitly to a port.
s=socket.socket(socket.AF_INET , socket.SOCK_DGRAM)  
ip="192.168.43.149"
port=2323
s.bind((ip,port))

This is the final block of code.

  1. It says that we have to receive data from the client and get the basic information like the data and IP address of the client.
  2. The data received from the client will be a NumPy array encoded to streaming data within the client-side. The received data will be decoded.
  3. imdecode() function reads data from specified memory cache and converts (decodes) data into image format; it is mainly used to recover images from network transmission data.
  4. Then the data is decoded and the Screen is Show with the Webcam on and If we press Enter Key the program is terminated.

Here is the complete code for the server code

Video-Client.py file

Here we are going to write the python code for the Video Chat Client Side.

  • We have to import Some Python Libraries to make our chat app those Libraries are cv2, socket, NumPy, and pickle.
import cv2, socket, numpy, pickle
  • There is one Extra function that is used called setsockopt()

The setsockopt() function provides an application program with the means to control socket behavior. An application program can use setsockopt() to allocate buffer space, control timeouts, or permit socket data broadcasts.

s=socket.socket(socket.AF_INET , socket.SOCK_DGRAM)  
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 10000000)
serverip="192.168.43.149"
serverport=2323
  • Now the Final Block of Code is Some What Similar to the Server File Code.
  1. Here, In the first line, we are selecting the webcam to use for the video chat
  2. Then we are using a while loop that will capture our video feed
  3. Then imshow() function is used to display the webcam window
  4. The data is encoded using imencode()
  5. cv2. imencode() function converts (encode) the image format into streaming data and assigns it to the memory cache. It is mainly used for compressing image data format to facilitate network transmission.
  6. The encoded data is dumped using the pickle library
  7. sendto() function is used to send a message on a socket (the encoded data). It shall send a message through a connection-mode or connectionless-mode socket.
  8. If the Enter Key is Pressed then the program is terminated

Here is the complete code for the Client code

When we execute these two files we will get two windows with webcam on like this

GitHub Project Link:

Thanks for visiting so far, if you like the blog don’t forget to clap 50 :)

ARTH-School of technology, BCA graduate