Live Streaming Video Chat App using cv2 module of Python
Task 03 👨🏻💻 Summer Program 2021
Task Description 📄
📌 Create Live Streaming Video Chat App without voice using cv2 module of Python:
Team : Summer_6_14
Mohammed Adnan
Prattipati Sri Raviteja
Saami Abdul Samad
Mohammed Awais Ahmed
GitHub Repo URL: https://github.com/AwaisJunaid/Summer2021/tree/main/Task%203
To create a Live Streaming Video Chat App using the cv2 module of Python, we have used the socket programming to connect the two ends of the network to connect to each other. There are two key nodes here:
- The Sender(Server)
- The Receiver(Client)
What is socket programming?
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. A socket is created with no name. A remote process has no way to refer to a socket until an address is bound to the socket.
We’re also going to use an important module called OpenCV where it is a library of Python bindings designed to solve computer vision problems. All the OpenCV array structures are converted to and from Numpy arrays.
Now let’s begin!
The Sender
Librararies:
import socket
import cv2
import pickle
import struct
Socket:
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)
Output: Host IP: <IP Address>
Binding & Listening the Socket:
server_socket.bind(socket_address)
server_socket.listen(5)
print("LISTENING AT:",socket_address)
Output: Listening @ <Host IP>, <Port>
Running and Streaming the Video:
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()
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)
cv2.imshow('VIDEO FROM SERVER',frame)
key = cv2.waitKey(10)
if key ==13:
client_socket.close()
cv2.destroyAllWindows()
The Receiver
Libraries:
import socket
import cv2
import pickle
import struct
Connecting to the Socket:
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '<HOST_IP>' # paste your HOST IP
port = <PORT_NO>
client_socket.connect((host_ip,port))
data = b""
payload_size = struct.calcsize("Q")
Streaming the Server Side Video:
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(10)
if key == 13:
break
client_socket.close()
cv2.destroyAllWindows()
Output:
In the given output, you can see that there’s a window from sender’s server and the other window is for the client i.e., from the receiver’s end.