Live Streaming Video Chat App with OpenCV and Socker Programming

In this, we will see how to create a live video chat application with the help of OpenCV and socket programming in python.

Server Side Scripting:

Step 1: Imported the socket programming header files, after we created a socket and assigned a host IP address and port number.

import socket, cv2, pickle, struct, imutils

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
port = 9999
socket_address = (host_ip,port)

Step 2: Now, we bind the created socket with the host IP address and port number.

server_socket.bind(socket_address)

Step 3: Finally, we will make the socket to listen from the clients in a continuous manner.

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=680)

a = pickle.dumps(frame) message = struct.pack(“Q”,len(a))+a

client_socket.sendall(message)

cv2.imshow(‘TRANSMITTING VIDEO’,frame)

key = cv2.waitKey(1) & 0xFF

if key ==13 or key ==113 or key==81: msg=”q”

client_socket.send(msg.encode())

client_socket.close()

cv2.destroyAllWindows()

Client-side scripting:

Step 1: Imported the socket programming header files, after we created a socket and assigned a host IP address and port number.

client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = ‘192.168.56.1’
port = 9999

Step 2: Now, make a connection for connecting the client with the created server.

client_socket.connect((host_ip,port))

Step 3: Finally, we will run a loop to send and receive data from the server in a continuous manner.

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 ==13 or key ==113 or key==81:

cv2.destroyAllWindows()
break
client_socket.close()

Thank you for reading the article.

--

--

--

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

Recommended from Medium

BUILDING KNOWLEDGE: 15 parts, 37 screws, 6 beta tests yields 1 desk

tools and office supplies arranged in a circular pattern

Create your first ”Hello World” app using stateless widget in flutter

Big Picture Event Storming

Paytm Plugin Flutter — paytm

Keycloak — Identity and Access Management tool

Ruby on Rails Agencies: How to Vet and Hire One

Ant vs Maven vs Gradle

Over the past 48 hours I have proposed this idea to the CEOs

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
Abhishek Kumar

Abhishek Kumar

More from Medium

VITTY — From an Idea to a Reality

Custom Keras model running on OpenCV AI Kit (OAK-1)

Fun With AI: Create Figures ‘Magically’ on Screen Using MediaPipe and OpenCV

Face Detection in Videos