Creating Video Chat App using Python

In a Pandemic situation, the only way to stay connected is through the internet. But due to such a huge activity in Advertisement department, data leak and data privacy is a big issue. To overcome the data privacy issues, let us create our own video chat app in order to stay connected with our friend's ones and avoid tracking by any of the advertising companies. Previously we created a text chat app now let’s take a step forward and create a video chat app.

So I use OpenCV, Image Processing:

For creating live streaming video chat we have to install the library that is mentioned below:

1.pip install OpenCV-python
2.pip install sockets
3.pip install pickle-mixin
4.pip install imutils

Server.py

import socket, cv2, pickle,struct
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 = 9999
socket_address = (host_ip,port)
#Socket Bind
server_socket.bind(socket_address)
#Socket Listen
server_socket.listen(5)
print("LISTENING AT:",socket_address)
#Socket Accept----
while True:
client_socket,addr = server_socket.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
vid = cv2.VideoCapture(0)
while(vid.isOpened()):
img,frame = vid.read()
frame = imutils.resize(frame,width=320)
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)

cv2.imshow('TRANSMITTING VIDEO',frame)
key = cv2.waitKey()
if key ==ord('return'):
client_socket.close()

Client-side program:

#lets mke the client code
import socket,cv2, pickle,struct
#lets mke the client code
#create socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = ‘192.168.43.35’
# paste your server ip address here
port = 9999
client_socket.connect((host_ip,port))
# a tuple
data = b””
payload_size = struct.calcsize(“Q”)
while True:
while len(data) < payload_size:
packet = client_socket.recv(4*1024) # 4K
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 += client_socket.recv(4*1024)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
client_socket.close()

We run the server.py and after it, we try to run client.py, pictures are transferred blazingly fast hence creating video streaming.

Converting this image to bytes format

import pickle
photo_serialize=pickle.dumps(photo)

--

--

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