Live Streaming Video Call App without voice using cv2 module of Python
Introduction:
In today's fast-paced world, real-time communication is essential, and building a live video chat application can be an exciting project. In this article, we will explore how to create a simple live video chat app using Python, OpenCV (Open Source Computer Vision Library), and socket programming. This application allows two users to connect over a network and engage in a live video conversation. Let's dive into the code and learn how to set up the server and client sides of the application.
Prerequisites:
Before we get started, ensure that you have Python installed, along with the necessary libraries, including NumPy and OpenCV.
Server-Side Code:
import os
import cv2
import numpy as np
import socket
# Create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define server IP and port
ip = "192.168.43.147"
port = 8888
# Bind the socket
s.bind((ip, port))
# Listen for incoming connections
s.listen(5)
# Accept a connection
conn, addr = s.accept()
# Create a video capture object
cap = cv2.VideoCapture(1)
while True:
data = conn.recv(90456)
# Decode the image
arry = np.fromstring(data, np.uint8)
photo = cv2.imdecode(arry, cv2.IMREAD_COLOR)
if type(photo) is type(None):
pass
else:
cv2.imshow("SERVER-SCREEN", photo)
if cv2.waitKey(10) == 13:
break
stat, photo = cap.read()
# Encode the image and send it via the network
photo_data = cv2.imencode('.jpg', photo)[1].tobytes()
conn.sendall(photo_data)
cv2.destroyAllWindows()
cap.release()
领英推荐
os.system("cls")
Client-Side Code:
import os
import cv2
import numpy as np
import socket
# Create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define server IP and port
ip = "192.168.43.147"
port = 8888
# Connect to the server
s.connect((ip, port))
# Create a video capture object
cap = cv2.VideoCapture(0)
while True:
stat, photo = cap.read()
# Encode and send data via the network
photo_data = cv2.imencode('.jpg', photo)[1].tobytes()
s.sendall(photo_data)
data = s.recv(90456)
# Decode the image
arry = np.fromstring(data, np.uint8)
photo = cv2.imdecode(arry, cv2.IMREAD_COLOR)
if type(photo) is type(None):
pass
else:
cv2.imshow("CLIENT-SCREEN", photo)
if cv2.waitKey(10) == 13:
break
cv2.destroyAllWindows()
cap.release()
os.system("cls")
Conclusion:
By following this guide, you can create a basic live video chat application using Python, OpenCV, and socket programming. Remember to replace the IP address in the code with your system's IP address and run the server and client sides in two different command prompt terminals. This project is an excellent starting point for further enhancements and customization, making it a fun and educational programming exercise.
Thank you!!!!!!!!!!!!!