LIVE_STREAM_VIDEO_APP_OPEN_CV (3)

AnujGupta
4 min readJun 6, 2021

--

Image is just the array of information that stores the rgb value in a structured format that represents a object .

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

There are two functions in this program 1 for sending data 1 for rercieving data .

Data here is the array of an image .

Socket is created on the system to connect .

Send function make connection with other system and using pickle module we are dumping data in to other system.

Capturing image frame using opencv moule .
And sending these array data via socket , using pickle module.

def send():

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
my_ip = '192.168.43.131'
my_port = 9999
socket_address = (my_ip,my_port)

server_socket.bind(socket_address)

server_socket.listen(5)
print("System listening to form CONNECTION on : ",socket_address , " <<<<<<< your system")


while True:
client_socket,addr = server_socket.accept()
print('CONNECTED WITH >>>>> ',addr, " <<<<< other systems address")
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('>>>>>>>>>>> ANUJ GUPTA <<<<<<<<<<<<',frame)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'):
client_socket.close()

Recieve function loads the packates from sender and using cv module we are displaying the data into image format continously ,
So that it looks as video .

def recieve():
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '192.168.43.26'
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(4096)
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(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow(">>>>>>>>>>>>> NITESH GUPTA CALL ( ( ( CONNECTED ) ) ) <<<<<<<<<<<<",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
client_socket.close()

# implementing multi threading for both side connection (( sending and recieving at the same time ))

send123=threading.Thread(target=send)
recieve123=threading.Thread(target=recieve)

send123.start()
recieve123.start()

Using multi threading to run both the functions simultaneously on each server.

Multithreading is a model of program execution that allows for multiple threads to be created within a process, executing independently but concurrently sharing process resources. Depending on the hardware, threads can run fully parallel if they are distributed to their own CPU core.

# implementing multi threading for both side connection (( sending and recieving at the same time ))

send123=threading.Thread(target=send)
recieve123=threading.Thread(target=recieve)

send123.start()
recieve123.start()

Complete code :

# These program transfer and recieve data simultaneously (client as well as server)
# in the send function put details of your system ip port to create socket

import socket,cv2,pickle,struct,threading # importing necessary libraries

def send():

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
my_ip = '192.168.43.131'
my_port = 9999
socket_address = (my_ip,my_port)

server_socket.bind(socket_address)

server_socket.listen(5)
print("System listening to form CONNECTION on : ",socket_address , " <<<<<<< your system")


while True:
client_socket,addr = server_socket.accept()
print('CONNECTED WITH >>>>> ',addr, " <<<<< other systems address")
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('>>>>>>>>>>> ANUJ GUPTA <<<<<<<<<<<<',frame)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'):
client_socket.close()


#-----------------------------------------------------------------------------------------------------------------------------------------------------------


def recieve():
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '192.168.43.26'
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(4096)
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(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow(">>>>>>>>>>>>> NITESH GUPTA CALL ( ( ( CONNECTED ) ) ) <<<<<<<<<<<<",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
client_socket.close()

# implementing multi threading for both side connection (( sending and recieving at the same time ))

send123=threading.Thread(target=send)
recieve123=threading.Thread(target=recieve)

send123.start()
recieve123.start()

i have integrated multi threading in live stream project where a person can transmit and recieve data simultaneously

send() function used to send data

recieve() to recieve

Thanks

--

--