Live Streaming using OpenCV

Abhishek Prasad Kesare
Nerd For Tech
Published in
4 min readJun 5, 2021

At least once we have used live streaming but ever wondered how it can be done by using programming.

Hello Guys, I am here for you to make some fun using OpenCV and socket programming with python

Today we are going to create a live streaming app with help of OpenCV and socket programming we are going to develop code from scratch for both server and client.

What is Opencv?

Opencv is a computer vision library. This library has multiple functionalities so that we can process our images and videos using the webcam and programming.

What is Socket programming?

In simple terms, socket programming means communication between two or more servers on a port using internet protocols like TCP or UDP. More about socket and OpenCV we will discuss in code

Let’s Develop a server

On the server The app will be running with port number and IP address when the client connects to the server then the server receives the data and converts it into streaming.

before Running the code make sure you have downloaded the following libraries in python

pip install packagename

opencv-python,numpy,pickle

import cv2, socket, numpy, pickles=socket.socket(socket.AF_INET , socket.SOCK_DGRAM)ip="192.168.1.5"port=6666s.bind((ip,port))

In the above code, we have imported the required libraries for task. ‘s’ variable will store the address of socket and socket.AF_INET is for representing we are using ipv4 for socket. socket.sock_DGRAM is for UDP protocol which stands for user datagram protocol. Sometimes it is risky to use because suppose our packet doesn’t go to the user it will not tell us so we have the risk of data loss instead of that it is easy to use. We can use TCP also but it will make our code a little bit complex so for basic we are using the UDP

“ip” variable holds IP address of your computer and “port” number has the port on which you are running the process. you can give any port number according to your choice because it is just a process running and exposed to world

s.bind() function will bind our “ip” and “port” number and convert into a socket. in simple words tuple. Now we have done socket.

while True:    x=s.recvfrom(1000000)    clientip = x[1][0]    data=x[0]    print(data)    data=pickle.loads(data)    print(type(data))    data = cv2.imdecode(data, cv2.IMREAD_COLOR)    cv2.imshow('server', data) #to open image    if cv2.waitKey(10) == 13:        breakcv2.destroyAllWindows()

The above function will receive data from the client temporarily store into the ‘x’ variable. In the ‘clientip’ we have stored the IP address of the client using retrieved with slicing array and the data variable will hold the data. Now we will decode the data received in the data variable using function imdecode and imshow function will show a streaming window. cv2.waitkey(10) will click/collect data every 10 milliseconds until you press enter and enter has number 13. destroyAllWindows() will close the application when you press enter.

Let’s Configure the client

Now when we connect the client at the given server IP and port number communication starts between them.

import cv2, socket, pickle, os  s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 10000000)serverip="192.168.1.5"serverport=6666

Here first we have imported required libraries for socket and OpenCV and ‘s’ stores the address of the socket.AF_INET represents the IP family v4 and socket.SOCK_DGRAM this keyword is for UDP protocol, And the third line will create a buffer size so that we can store a buffer of data to transmit it continuously. “serveip” and “serverport” hold the IP address and port number of the server.

cap = cv2.VideoCapture(0)
while True:
ret,photo = cap.read()

cv2.imshow('streaming', photo)

ret, buffer = cv2.imencode(".jpg", photo,[int(cv2.IMWRITE_JPEG_QUALITY),30])
x_as_bytes = pickle.dumps(buffer)

s.sendto(x_as_bytes,(serverip , serverport))

if cv2.waitKey(10) == 13:

break
cv2.destroyAllWindows()cap.release()

VedioCapture(0) stores the function address and ‘0’ for storing the webcam. and ret stores the return value and photo and store output of cap.read() this function will read the images. imshow will show the streaming window. buffer variable will store you data of your streaming and provide it to the server. pickle.dumps(buffer) this function will dump the data into a variable. send to a function will bind the data with IP and port number so that we can send it. waitkey will collect data in every 10 milliseconds until we press enter and the code of entering is 13. destroyAllWindows() will destroy windows and cap.release() will close our camera.

Here you can clearly see the data received and streaming happening to both sides.

Here I am using the second machine as a virtual machine so that you are seeing both same images.

You will find complete code GITHUB

--

--

Abhishek Prasad Kesare
Nerd For Tech

Data science, , cloud computing, Artificial Intelligence, Cybersecurity,tech-blogger