CREATE LIVE STREAMING VIDEO CHAT APP WITHOUT VOICE USING CV2 MODULE OF PYTHON

NETWORK SOCKET

* A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network.

  • The structure and properties of a socket are defined by an application programming interface (API) for the networking architecture.

|| IMPLEMENTATION OF SOCKETS

In Standard Internet Protocols like TCP and UDP :

* socket address is the combination of
socket address = ( IP address , port number)

|| CLIENT-SERVER MODEL

  • Server creates socket on startup
  • May serve several clients concurrently
  • A client should know the server IP and port

PRE-REQUITIES :

-> Install following modules in cmd ::* pip install OpenCV-python
* pip install sockets
* pip install pickle-mixin.
* pip install supyr-struct

|| PYTHON SERVER MODULE

  1. Socket creation
SYNTAX : server_socket = socket.socket(socket.AF_INET , socket.SOCK_DGRAM)CODE :
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print('HOST IP:',host_ip)
port = 10050
socket_address = (host_ip,port)

2. Socket bind

SYNTAX : server_socket.bind((host_ip , port))CODE :
server_socket.bind(socket_address)

3. Socket listen

SYNTAX : server_socket.listen(5) #5 is backlogCODE :
server_socket.listen(5)
print("LISTENING AT:",socket_address)

4. Socket accept

SYNTAX : client_socket , addr = server_socket.accept()CODE : 
while True:
client_socket,addr = server_socket.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
cap = cv2.VideoCapture(0)

while(cap.isOpened()):
ret,vdo = cap.read()
vdo = imutils.resize(vdo,width=320)
a = pickle.dumps(vdo)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)

cv2.imshow('TRANSMITTING VIDEO',vdo)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'):
client_socket.close()

5. Socket client

client_socket.recv() or client_socket.send() or client_socket.sendall()

6. Close client

client_socket.close

| VIDEO DATA TRANSMISSION : At Server Side ::

  • With Open CV get video frames of webcam
  • With PICKLE serialize frame to byte data
  • Pack each frame data using STRUCT module
  • Send data to client and display frame

FINAL SERVER CODE :

OUTPUT :

|| PYTHON CLIENT MODULE

  1. Socket creation
SYNTAX : client_socket = socket.socket(socket.AF_INET , socket.SOCK_STREAM)CODE :
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '169.254.72.59' # paste your HOST IP
port = 10050

2. Socket connect

SYNTAX : client_socket.connect(( host_ip , port ))CODE : 
client_socket.connect((host_ip,port)) # tuple
data = b"" # Initialise data variable as a string , b - bytes
# Payload size is defined with "Q" i.e.,unsigned long long integer that takes 8 bytes
payload_size = struct.calcsize("Q")

3. Socket receive

SYNTAX : packet = client_socket.recv(1024) #1024 is buffer sizeCODE :
while True:
while len(data) < payload_size:

# when we recieve packet we use 4k buffer
packet = client_socket.recv(4*1024)

if not packet: break
data+=packet

# First 8 bytes contain size of packed msg so we use data from 0 to payload_size(i.e., 8)
packed_msg_size = data[:payload_size]

# Rest of the data contains video so we set new data which we have to frame to display
data = data[payload_size:]
msg_size = struct.unpack("Q",packed_msg_size)[0]

while len(data) < msg_size:
data += client_socket.recv(4*1024)

# We will get the packed msg size and run the while loop until we receive all data from the client socket for frame
frame_data = data[:msg_size]
data = data[msg_size:]

# Finally, frame data is recovered
frame = pickle.loads(frame_data)

cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # q is used to quit the video
break
client_socket.close()

4. Socket send

client_socket.send(packet)

5. Close socket

client_socket.close()

| VIDEO DATA TRANSMISSION : At Client Side ::

  • Receive packets and append them to data
  • Unpack the data using STRUCT module
  • Load the frame using PICKLE
  • Display the frame at client side

FINAL CLIENT CODE :

OUTPUT :

FINAL OUTPUT ::

  • Note: since I done in same pc I get same image

DONE !!!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Python vs. R

stvke.info, stvke.app available on Main Net, Kovan, Goerli and Rinkeby

Customize your iorad Widget

Learning and trying to reverse web assembly for fun!

Image Uploads to Active Storage: “How to AJAX”

Synthesizing Programs with Deep Learning

Implementing the Jumping Animation

Introducing the ASO Stack 2019

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
Saranya. S

Saranya. S

More from Medium

Automating Open VPN Login [Ubuntu]

Pwning the Pwnkit with Confluera

Using Flask with MongoDB and SQLite database(33)

Introducing Tailpages (Tailwind + Github Pages)