Python Forum

Full Version: tkinter frame camera opencv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.I have a main window using tkinter.I call a function that has:
root.after(1 , camera_status , ....) and other things
So,in camera_status which is inside in another file has:

from tkinter import *
#from GUI_camera import video_stream
import cv2 , PIL
from PIL import Image,ImageTk
cap = cv2.VideoCapture(0)

def camera_status(root , FOREGROUND_COLOR , ACTIVE_FOREGROUND_COLOR , BACKGROUND_COLOR , ACTIVE_BACKGROUND_COLOR , mode):  # control camera
    camera_frame = LabelFrame(root , width = 663 , height = 528)
    camera_frame.place(x = 844 , y = 300)
    
    if(mode):
        ret , frame = cap.read()
        if(ret):
            frame = cv2.flip(frame , 1)
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
            img = PIL.Image.fromarray(cv2image)
            imgtk = ImageTk.PhotoImage(image = img)
            camera_frame.imgtk = imgtk
            camera_frame.configure(image = imgtk)
    
        enable_camera_logo = Image.open("logo/enable_camera.png")
        
    else:
        enable_camera_logo = Image.open("logo/disable_camera.png")
        

    enable_camera_logo = enable_camera_logo.resize( (75 , 30) , resample = 0)
    enable_camera_image = ImageTk.PhotoImage(enable_camera_logo)

    camera_button = Button(root , highlightbackground = "black" , bd = 0 , image = enable_camera_image , activeforeground = "black", activebackground = "black" , bg = "black" , relief = SUNKEN)
    camera_button.config(command = lambda: camera_status(root , FOREGROUND_COLOR , ACTIVE_FOREGROUND_COLOR , BACKGROUND_COLOR , ACTIVE_BACKGROUND_COLOR , mode))
    camera_button.image = enable_camera_image
    camera_button.place(x = 840 , y = 250)

    mode = not mode
I just want to push a button and inside my frame(camera_frame) to enable camera.So,when I push the button ,the light from my camera (laptop) is enable ,but it doesn't show anything isnide frame.
Doyou have any idea what is going on and how can I fix it?

Don't run this code ,because I have icons for the button.
Thanks.
You have a function that makes a button that calls the same function. What is the thinking behind that? What are you trying to do. If you can describe that maybe the code will make some sense to me.
OK.I will describe it simply.I want to push a button and enable camera inside a frame ,when I push it again I want to disable camera.
So ,I have a file(eg: file b.py) and I call function camera_status(...) which is inside (eg: file a.py),the button is inside camera_status(...) and when I push it in order to enable/disable the camera, I have to call the same function,because inside this function I enable/disable camera (generally control camera).
Am I clear?
The problem is that the light of my laptop's camera is enabled ,but the frame doesn't show anything.

My code is big and have a lot of files.I made 2 files (file a and b),just to see what I mean and what I want in order to help you understand.

file a.py:
from tkinter import *
#from GUI_camera import video_stream
import cv2 , PIL
from PIL import Image,ImageTk
cap = cv2.VideoCapture(0)

def camera_status(root , mode):  # control camera
    camera_frame = LabelFrame(root , width = 663 , height = 528)
    camera_frame.place(x = 844 , y = 300)
    
    if(mode):
        ret , frame = cap.read()
        if(ret):
            frame = cv2.flip(frame , 1)
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
            img = PIL.Image.fromarray(cv2image)
            imgtk = ImageTk.PhotoImage(image = img)
            camera_frame.imgtk = imgtk
            camera_frame.configure(image = imgtk)
        print("Camera is enabled")
        #enable_camera_logo = Image.open("logo/enable_camera.png")
        
    else:
        print("camera is disabled")
        #enable_camera_logo = Image.open("logo/disable_camera.png")
        pass

    #enable_camera_logo = enable_camera_logo.resize( (75 , 30) , resample = 0)
    #enable_camera_image = ImageTk.PhotoImage(enable_camera_logo)

    camera_button = Button(root , text = "Control camera")  # see in the terminal the output message
    camera_button.config(command = lambda: camera_status(root , mode) )
    #camera_button.image = enable_camera_image
    camera_button.place(x = 840 , y = 250)

    mode = not mode
file b.py:
from tkinter import *
from a import camera_status

root = Tk()
root.geometry("1800x1100")
root.after(1 , camera_status(root , False) )  
root.mainloop()
Can you help me?
Thanks in advance.
What does "enable the camera" mean?

I don't understand why you are making a new frame and button each time instead of laying out the controls once and reusing them.
turn on camera
And do what? It looks like you are taking a picture and displaying a image. Do you want video?
yes I want real time video
Looks like you tried a bunch of different things and now the code is littered with the leftovers of unsuccessful attempts. For example, this adds an attribute to a LabelFrame. It doesn't do anything, but now you have a frame with an attribute named imgtk.
camera_frame.imgtk = imgtk
I also don't see how this is not causing an error.
camera_frame.configure(image = imgtk)
image is not an attribute of LabelFrame. You cannot display an image in a LabelFrame. You can display an image in a Label. The Label in a LabelFrame is text only. Maybe you never get to where this code is executed.

When you run your code do you see that mode is being toggled each time you press the button? Does it toggle between printing "Camera is enabled" and "camera is disabled"?

What is the value of ret? Is the problem that the read is failing?
I found the error.I had a labelframe instead of a label.That was the error.Anyway thanks for your time.
Hello again.I changed a little bit file a.py and it is like this:
from tkinter import *
import cv2 , PIL
from PIL import Image,ImageTk
cap = cv2.VideoCapture(0)
 
def camera_status(root , mode):  # control camera
    camera_frame = Label(root , width = 663 , height = 528)
    camera_frame.place(x = 844 , y = 300)
     
    def yyy():
        ret , frame = cap.read()
        
        print("ret = " + str(ret) + "  cap = " + str(cap.isOpened() ))
        if(ret):
            frame = cv2.flip(frame , 1)
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
            img = PIL.Image.fromarray(cv2image)
            imgtk = ImageTk.PhotoImage(image = img)
            camera_frame.imgtk = imgtk
            camera_frame.configure(image = imgtk)
            camera_frame.after(1 , yyy)

    if(mode):
        yyy()

    else:
        pass # HERE I WANT TO TURN OFF CAMERA

    camera_button = Button(root , text = "Control camera")  # see in the terminal the output message
    camera_button.config(command = lambda: camera_status(root , mode) )
    camera_button.place(x = 840 , y = 250)
 
    mode = not mode
When I push the button ,camera is turned on (OK) , but when I push it again camera doesn't turn off.
How can I fix it?
from tkinter import *
from a import camera_status
 
root = Tk()
root.geometry("1800x1100")
root.after(1 , camera_status(root , False) )  
root.mainloop()
Run b.py