Live Streaming Video Chat App without voice using cv2 module of Python And Socket Programming

Utkarsh Pande
3 min readJun 12, 2021

--

This Article Is Mainly About Transferring Video using Socket Programming :

To Perform This Practical We Have to First Setup The Connection Between The Client and Server in this Practical I AM using Single Computer as a Client And Server. You Can use other systems as well by just changing the client IP but One Necessary Condition is that they Must be in the same network.

### FOR SERVER SIDE ###

STEP 1: CHOOSE PROTOCOL AND ADDRESS FAMILY

As we Know transfer of any type of Data on the network Use some type Of Protocol Like TCP, UDP, etc. and to identify the Target there must be address generally we use IP and Port no but we can use different thing also like ip6 UNIX, etc and lastly we have to bind make socket using this two

myp =  s.SOCK_STREAM
afn = s.AF_INET
add_pro = s.socket(afn , myp)

STEP 2: NOW WE HAVE TO BIND THE IP ADDRESS AND PORT NO OF THE SERVER

#Binding IP Address and Port No 
myip = "192.168.43.198" #server ip
port = 1234 #server port
bind = add_pro.bind((myip , port)) #Binding ip and port
add_pro.listen() #Starting Listen means start connection

STEP 3: NOW WE HAVE TO ACCEPT THE REQUEST FROM THE CLIENT AND ESTABLISH THE CONNECTION BETWEEN THE CLIENT AND THE SERVER

while True:
c , addr = add_pro.accept()
if c:
cv = cv2.VideoCapture(0)
while(cv.isOpened()):
ret , photo = cv.read()
a = pickle.dumps(photo)
message = struct.pack("Q",len(a))+a
c.sendall(message)
cv2.imshow("Server Side" ,photo)
key = cv2.waitKey(1) & 0xFF
if cv2.waitKey(1) == 13:
cv2.destroyAllWindows()
c.close()
break
cv.release()

Here the use of the different type of function is shown here:-

1) add_pro.accept() : - This is  used to accept the request from the Client
2) cv2.VideoCapture(0) :- This is generally use to use the internal camera for the laptop
3) cv.isOpened() :- Check whether our camera is working fine of not
4) cv.read() :- Capture the photo as we Know Image is genrally the Matrix 2D for Grey and 3D for the Color Image and here it is like reading the image
5) pickle.dump() :- It is used to save the photo
6) struct.package() :- This is the function that converts a given list of values into their corresponding string representation
7) c.sendall() :- Used to send data to the client
8) cv2.imshow() :- This is used to show the image whatever it click
9) waitKey(1) :- Used to hold the image for the particular imagae
10) c.close() :- Close the connection
11) cv.release() :- Close the Camera
12) cv2.destroyAllWindows() :- Close the Displaying Image
13) struct.unpack() :- used to unpack the data come from the server

### FOR CLIENT SIDE ###

NOTE:- FIRST STEP IS THE SAME AS THE SERVER

STEP 2:- USING SERVER IP AND CLIENT FOR CONNECTION

#Binding IP Address and Port No 
serverip = "192.168.43.198" #server ip
port = 1234 #server port
add_pro.connect((myip,port))

STEP 3:- RECEIVING THE DATA FROM THE SERVER

while True:
while len(data) < payload_size:
packet = add_pro.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 += add_pro.recv(4*1024)
frame_data = data[:msg_size]
data ​= data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("Client Side",frame)
if cv2.waitKey(1) == 13:
cv2.destroyAllWindows()
break
add_pro.close()

USE OF FUNCTION ARE listed above.

Here we are receiving data from the client and unpacking is done using struct.unpacks and lastly we are showing the image as here the image is the matrix so we are using the array.

STEP 4:

The Final Output of the program is shown below:

--

--