Python Forum
tkinter frame camera opencv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter frame camera opencv
#1
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.
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
turn on camera
Reply
#6
And do what? It looks like you are taking a picture and displaying a image. Do you want video?
Reply
#7
yes I want real time video
Reply
#8
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?
Reply
#9
I found the error.I had a labelframe instead of a label.That was the error.Anyway thanks for your time.
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 820 Jan-16-2024, 06:44 PM
Last Post: rob101
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,451 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  tkinter mapview in a frame janeik 2 1,246 Jun-22-2023, 02:53 PM
Last Post: deanhystad
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,012 Sep-30-2021, 05:57 PM
Last Post: menator01
  tkinter camera threads Nick_tkinter 6 3,327 Apr-16-2021, 12:23 PM
Last Post: Nick_tkinter
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,651 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  How to disable focus on Frame in Tkinter? szafranji 1 2,974 May-13-2020, 10:45 PM
Last Post: DT2000
  [Tkinter] Tkinter bringing variables from 1 frame to another zukochew 6 12,418 Dec-26-2019, 05:27 AM
Last Post: skm
  Unable to put background image on Tkinter Frame jenkins43 2 8,706 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 8,944 Sep-30-2019, 07:30 AM
Last Post: Maksim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020