Live Streaming Video App Without voice using cv2 module of Python.

Yash Dwivedi
3 min readJun 10, 2021

Python is a general purpose programming language started by Guido van Rossum that became very popular very quickly, mainly because of its simplicity and code readability. It enables the programmer to express ideas in fewer lines of code without reducing readability.

OpenCV is a free open source library used in real-time image processing. It’s used to process images, videos, and even live streams, but in this tutorial, we will process images only as a first step. Before getting started, let’s install OpenCV.

Install OpenCV

pip install opencv-python

OpenCV module at the backend uses numpy array as computer never understand Images and all the images are in array format.

Here we used some of the libraries like OpenCV, socket, pickle, struct

Socket Module:

we are using socket library for TCP or UDP communication between Devices using Any of address families.

Pickle Module:

Python pickle module is used for serializing and de-serializing python object structures. The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) is called pickling or serialization or flattening or marshalling.

Struct Module:

This module performs conversions between Python values and C structs represented as Python bytes objects. This can be used in handling binary data stored in files or from network connections, among other sources.

Now It’s time to create the Live Streaming code:

We have to create two programs one is for server side and the other one is client side program. client always go first to the server program.

I’m Also Sharing my GitHub Link:

https://github.com/yashdwi05/tcp-live-streaming.git

Server Program using TCP:

#!/usr/bin/python3
import socket
import cv2
import pickle
import struct

#TCP Socket Creation
tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

tcp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcp_sock.bind((“0.0.0.0”, 2323))

#Listening To Connections
tcp_sock.listen()
print(“Accepting Connections…”)

while True:
s, addr = tcp_sock.accept()
print(f”Connected to {addr}!!!”)
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
ret, photo = cap.read()

#Serialise/flattening Data
data = pickle.dumps(photo)

#Bytes Conversion
packet = struct.pack(“Q”, len(data))+data
s.sendall(packet)
cv2.imshow(“Server Side Streaming…”,photo)
if cv2.waitKey(10) == 13:
cv2.destroyAllWindows()
cap.release()
break
tcp_sock.close()

Client Program:

#!/usr/bin/python3
import cv2
import socket
import pickle
import struct

#TCP Socket creation
tcp_client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_port = 2323
server_ip = input(“Enter Server IP:”)

#Establish Connection
tcp_client_sock.connect((server_ip,server_port))
print(f”Conneted to {server_ip}”)
data = b””
payload = struct.calcsize(“Q”)

while True:
while len(data) < payload:
recv_packet = tcp_client_sock.recv(1024)
if not recv_packet: break
data += recv_packet
packed_data = data[:payload]
data = data[payload:]
packet_size = struct.unpack(“Q”,packed_data)[0]

while len(data) < packet_size:
data+= tcp_client_sock.recv(1024)
photo_data = data[:packet_size]
data = data[packet_size:]

#Deserialise Data
photo = pickle.loads(photo_data)
cv2.imshow(“Client Side Streaming…”, photo)
if cv2.waitKey(10) == 27:
break

cv2.destroyAllWindows()
tcp_client_sock.close()

Here we have both side codes and as soon as we connect to client it will start transmitting video to the client.

Thanks for Reading!!

--

--