Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live video from IP address #2675

Open
sp5677 opened this issue Jan 13, 2025 · 5 comments
Open

Live video from IP address #2675

sp5677 opened this issue Jan 13, 2025 · 5 comments

Comments

@sp5677
Copy link

sp5677 commented Jan 13, 2025

I was wondering if there's a way of showing a live video in customtkinter, I know there is "tkvideoplayer" but that's only for normal video.

In ChatGPT it said to do this - it wouldn't be wise to use this code I think.

import cv2
import customtkinter as ctk
from tkinter import Label
from PIL import Image, ImageTk

# Function to capture and display frames
def update_frame():
    # Read a frame from the camera stream
    ret, frame = cap.read()
    if ret:
        # Convert the frame from BGR to RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Convert the frame to a PIL Image
        img = Image.fromarray(frame_rgb)
        img = img.resize((640, 480))  # Resize to fit the window

        # Convert the PIL Image to an ImageTk object
        img_tk = ImageTk.PhotoImage(img)

        # Update the label with the new frame
        label.config(image=img_tk)
        label.image = img_tk  # Keep a reference to the image to prevent garbage collection

    # Call the update_frame function after 10 milliseconds
    window.after(10, update_frame)

# Set up the IP camera stream (replace the URL with your camera's stream URL)
camera_url = "rtsp://username:password@ip_address:port"
cap = cv2.VideoCapture(camera_url)

# Set up the main window
window = ctk.CTk()
window.title("Live Video Stream")

# Set up the label to display the video
label = Label(window)
label.pack()

# Start updating the frames
update_frame()

# Start the Tkinter main loop
window.mainloop()

# Release the camera when done
cap.release()

@dipeshSam
Copy link

dipeshSam commented Jan 13, 2025

@sp5677
You can use it. Including OpenCV library with customtkinter package is not ideal.

@sp5677
Copy link
Author

sp5677 commented Jan 13, 2025

@sp5677 You can use it. Including OpenCV library with customtkinter package is not ideal.

What's a better way?

@dipeshSam
Copy link

@sp5677
Use class oriented approach with proper modularization ensuring safety and it is fine.

@Akascape
Copy link

@sp5677 There is no video player like widget in tkinter by default, what tkVideoPlayer does is update a tk label image with the extracted frame from video (in a loop).
So, the method you are using is fine, you are just updating the image of the ctk label with the captured frame from webcan using opencv.

@sp5677
Copy link
Author

sp5677 commented Jan 18, 2025

@sp5677 There is no video player like widget in tkinter by default, what tkVideoPlayer does is update a tk label image with the extracted frame from video (in a loop). So, the method you are using is fine, you are just updating the image of the ctk label with the captured frame from webcan using opencv.

Tkinker doesn't use the GPU which makes it very slow, I decided it would be best if I used a different GUI that has GPU acceleration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants