Create Live Streaming Video Chat App without voice using cv2 module of Python

WL Date 10–06–2021 Task -03

Sandeep Kumar Patel
4 min readJun 6, 2021

Create Live Streaming Video Chat App without voice using cv2 module of Python

This task is completed by Sandeep Kumar Patel

GitHub Link-

https://github.com/Ersandeep977/LW-Date-10-06-2021-Task-03

Socket Programming in Python

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

Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest.

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 listen 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

Simple Client

Let us write a very simple client program which opens a connection to a given port 12345 and given host. This is very simple to create a socket client using Python’s socket module function.

The socket.connect(hosname, port ) opens a TCP connection to hostname on the port. Once you have a socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file.

Server Program-

#!/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, frame= 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…”,frame)
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

try:
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(“Socket successfully created Now Show …”)
except socket.error as err:
print(“Socket creation failed with error {}”.formatat(err))

port = 1234
server_ip = “192.168.56.1”
skt.connect((server_ip,port))
data = b””
payload_size = struct.calcsize(“Q”)
try:
while True:
while len(data) < payload_size:
packet = skt.recv(4*1024)
if not packet: break
data += packet
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack(“Q”,packed_msg_size)[0]

while len(data) < msg_size:
data+= skt.recv(4*1024)
img_data = data[:msg_size]
data = data[msg_size:]
frame= pickle.loads(img_data)
cv2.imshow(“Recieving video”, frame)
if cv2.waitKey(10) == 13:
cv2.destroyAllWindows()
break
skt.close()
except:
cv2.destroyAllWindows()
skt.close()

OutPut-

Thank YOU…….!!!

--

--

Sandeep Kumar Patel
Sandeep Kumar Patel

Written by Sandeep Kumar Patel

Passionate about AI and ML, I see research as purposeful curiosity. Eager for feedback, email -" patelsandeep88@gmail.com"

Responses (1)