Unveiling the Magic of Live Video: Step-by-Step Guide to IP Cam Streaming with Python!

Pankaj Yadav
2 min readApr 17, 2024

🎥💻 Are you ready to be a part of the most engaging live video streaming environment? Let’s start this interesting and adventurous trip together to know how the Python code captures and displays live video from the mobile camera to a tkinter window using an IP camera app. In this guide, we will divide the process into bite-sized bits, making it easy to follow and learn one step at a time. Therefore, with your programming accessories, let’s get coding!💻🎥

Step 1: Setting the Stage

Before we dive into coding, let’s ensure we have all the necessary tools at our disposal:

Tools Required:

  1. Python: Our programming language of choice.
  2. OpenCV Library: For capturing and processing video frames.
  3. Tkinter Library: For building our graphical user interface.
  4. PIL (Python Imaging Library): For handling images.
  5. An IP Camera App: Installed on your mobile device.

Step 2: Capturing Live Video and Building the Tkinter Interface

Now, let’s enhance our project by creating a tkinter window where we’ll display the live video stream. Here’s the code snap

import cv2
import tkinter as tk
from PIL import Image, ImageTk
import numpy as np

class IPCamViewer:
def __init__(self, root, IP_ADDRESS, PORT):
self.root = root
self.ip_address = IP_ADDRESS
self.port = PORT
self.url = "http://192.168.1.81:4747/video" # Corrected URL format
self.cap = cv2.VideoCapture(self.url)

self.width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

self.canvas = tk.Canvas(root, width=self.width, height=self.height)
self.canvas.pack()

self.update()

def update(self):
ret, frame = self.cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame)
self.photo = ImageTk.PhotoImage(image=image)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
self.root.after(20, self.update) # Update every 20 milliseconds

if __name__ == "__main__":
# Replace 'YOUR_IP_ADDRESS' and 'YOUR_PORT' with your Linux IP camera's IP address and port
IP_ADDRESS = 'http://192.168.1.81:4747'
PORT = 'http://192.168.1.81:4747/video'

root = tk.Tk()
root.title("IP Camera Viewer")

app = IPCamViewer(root, IP_ADDRESS, PORT)

root.mainloop()

Step 4: Testing and Enjoying the Result

Run the code, sit back, and behold the magic as your tkinter window comes to life with the live video stream from your mobile camera! Marvel at the seamless integration of Python, OpenCV, and tkinter, and relish in the satisfaction of a job well done.

Conclusion:

Congratulations on completing this exhilarating journey into the realm of live video streaming with Python! Armed with your newfound knowledge and skills, the possibilities are endless. So, go forth, explore, and continue to push the boundaries of what’s possible with technology!

--

--

Pankaj Yadav

Pankaj Yadav: Proactive BTech Computer Science Engineering Student at KMCLU, Lucknow.