Simple Live Video Streaming App with Python and OpenCV

In this exciting project, we're diving into the world of real-time video streaming using Python and the powerful OpenCV library. By the end of this guide, you'll have two computers communicating seamlessly, sharing live video streams. Let's gear up for this immersive experience!

Before We Begin

Before we jump into the code, here's what you need to get started:

  1. Two Computers: You'll require two computers (or virtual machines) running Python.

  2. OpenCV Library: Ensure that you have OpenCV installed. You can quickly do this by running pip install opencv-python.

Setting Up the Server (Computer A)

Our journey begins with configuring Computer A as the server. This computer should have a publicly accessible IP address or domain name if you plan to connect from a different network.

import cv2
import threading
import socket
import numpy as np

# Initialize video capture from the server's webcam
cap = cv2.VideoCapture(0)

# Create a socket for server-side communication
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 8080))
server_socket.listen(10)

# Accept a client connection
client_socket, client_address = server_socket.accept()
connection = client_socket.makefile('wb')

# Function to send video frames to the client
def send():
    while True:
        ret, frame = cap.read()
        frame = cv2.resize(frame, (640, 480))
        data = cv2.imencode('.jpg', frame)[1].tobytes()
        try:
            connection.write(data)
        except:
            break

# Function to receive video frames from the client
def receive():
    while True:
        data = connection.read()
        if not data:
            break
        nparr = np.fromstring(data, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        cv2.imshow('Server', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

# Create and start threads for sending and receiving
send_thread = threading.Thread(target=send)
receive_thread = threading.Thread(target=receive)
send_thread.start()
receive_thread.start()

# Main loop
send_thread.join()
receive_thread.join()

# Release resources
cap.release()
cv2.destroyAllWindows()

Setting Up the Client (Computer B)

Now, let's configure Computer B as the client. This code captures the video stream from Computer B's webcam and sends it to the server (Computer A).

import cv2
import threading
import socket
import numpy as np

# Create a socket for client-side communication
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('server_ip_here', 8080))  # Replace 'server_ip_here' with the server's IP or domain name

# Function to send video frames to the server
def send():
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        frame = cv2.resize(frame, (640, 480))
        data = cv2.imencode('.jpg', frame)[1].tobytes()
        try:
            client_socket.sendall(data)
        except:
            break

# Function to receive video frames from the server
def receive():
    while True:
        data = client_socket.recv(1024)
        if not data:
            break
        nparr = np.fromstring(data, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        cv2.imshow('Client', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

# Create and start threads for sending and receiving
send_thread = threading.Thread(target=send)
receive_thread = threading.Thread(target=receive)
send_thread.start()
receive_thread.start()

# Main loop
send_thread.join()
receive_thread.join()

# Release resources
cap.release()
cv2.destroyAllWindows()

Running the Application

  1. Run the server code on Computer A.

  2. Replace 'server_ip_here' in the client code with the actual IP address or domain name of Computer A.

  3. Run the client code on Computer B.

Voilà! You should now witness a live video stream from Computer B displayed on the screen of Computer A. It's like having your own video conferencing setup!

In this exhilarating project, you've ventured into the world of real-time video streaming. Python and OpenCV have empowered you to create a video communication bridge between two computers. Whether for remote collaboration or exploration, this experience showcases the boundless possibilities of technology. Enjoy your video streaming adventure! 🎥