Real-Time Video Chatting without Voice: How to Build an App with OpenCV and Python

Abhinav Shreyash
3 min readAug 3, 2023

--

In today’s rapidly evolving digital era, the power of technology continues to amaze us. One such fascinating application is a live video chat app that we can create using Python’s OpenCV2 module. This app allows users to communicate through live video streaming without the need for voice communication. In this blog, we will explore the code behind this ingenious application, demonstrating how it enables real-time video transmission and reception between server and client.

Server-Side Code:

# Server
import socket
import cv2
import pickle
import 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)

server_socket.bind(socket_address)
server_socket.listen(5)

print("Listening at:", socket_address)

while True:
client_socket, addr = server_socket.accept()
print("Got Connection from:", addr)

if client_socket:
vid = cv2.VideoCapture(0)

while vid.isOpened():
ret, frame = vid.read()
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 == ord('q'):
client_socket.close()
break

cv2.destroyAllWindows()

Explanation of Server-Side Code: The server-side code initiates by creating a socket object using socket.socket() with the AF_INET family and the SOCK_STREAM socket type, using the TCP protocol and IPv4. It then captures the video stream from the internal webcam using cv2.VideoCapture(0). The server's IP address and port are retrieved using socket.gethostbyname() and set to socket_address. The server binds the socket to this address and starts listening for incoming connections.

When a client connects, the server begins sending live video frames to the client using pickle for serialization and struct for packing the data. The video frames are continuously sent until the client presses the ‘q’ key, at which point the connection is closed and the windows are destroyed.

Client-Side Code:

# Client
import socket
import cv2
import pickle
import struct

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host_ip = 'YOUR_SERVER_IP' # Replace with the server's IP address
port = 9999

client_socket.connect((host_ip, port))
data = b""
payload_size = struct.calcsize("Q")

while True:
while len(data) < payload_size:
packet = client_socket.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 += 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()
cv2.destroyAllWindows()

Explanation of Client-Side Code: The client-side code is similar to the server-side code, with the main difference being that the client connects to the server’s IP and port using client_socket.connect((host_ip, port)). Once the connection is established, the client receives and displays the live video frames sent by the server.

Just like the server, the client will stop receiving frames upon pressing the ‘q’ key.

Conclusion:

In this blog, we explored how to create a live streaming video chat app without voice using Python’s OpenCV2 module. You saw how to establish a connection between the server and the client, allowing real-time video transmission and reception. The application opens up exciting possibilities for interactive communication in situations where voice communication may not be feasible or required.

Thanky you very much for giving your precious time and going through my blog , even a little word read by you in my blog is very appriciating.

--

--