Python Forum
tkinter camera threads - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: tkinter camera threads (/thread-33324.html)



tkinter camera threads - Nick_tkinter - Apr-15-2021

Hello everyone.
I have a big code in tkinter with 14 files.In one of my files I control my camera and show the output inside a frame (in tkinter).
So ,I will show you the code below ,it is implemented with threads. I want to use threads, because other parts of my program sticks a little bit.
But the problem is that the frame for output of camera is flashing a little bit.I don't know if my code is correct,
because I create and start a thread every time.
Using threads in video_stream(.) (see below) , other parts of program doesn't stick ,but my frame for the camera output is a little bit flashing.

Code if file a.py:
This is a function of (for example a.py)
import PIL , cv2
from PIL import Image , ImageTk
from tkinter import *
from datetime import datetime
import threading

cap = cv2.VideoCapture(2)

def video_stream(label_frame):
   
    ret , frame = cap.read()
    if(not cap.isOpened() ):
        return -1
        
    elif(ret):
        frame = cv2.flip(frame , 1)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = PIL.Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image = img)
        label_frame.imgtk = imgtk
        label_frame.configure(image = imgtk)
        thread_call_camera = threading.Thread(target=video_stream,args=(label_frame,))
        thread_call_camera.start()
        #label_frame.after(20 , video_stream , label_frame)
In another file ,for example b.py ,I call: << value = video_stream(a frame I designed here) >>
I can't show you the whole code ,is big and maybe you will get confused.
I just want to tell me if I create threads normally.

Thanks a lot.


RE: tkinter camera threads - Yoriz - Apr-15-2021

There are some examples of using threads with tkinter in the following link
https://python-forum.io/Thread-Tkinter-How-to-deal-with-code-that-blocks-the-mainloop-freezing-the-gui


RE: tkinter camera threads - Nick_tkinter - Apr-15-2021

I want to know if I implement the threads normal, because if you see, inside video_stream(.) ,thread calls the same function.
What do you think about it?
I know how to implement threads,but what about that case?
Thanks.


RE: tkinter camera threads - Yoriz - Apr-15-2021

You will only get a return of -1 when it first travels through the function and the following evaluates to true
if(not cap.isOpened() ):
        return -1
otherwise, it travels right through the function and will return None.
Directly calling the GUI from inside another thread will cause problems.
Depending on how fast it travels through the function and starts another thread looping back on itself, it might be overwhelming the GUI with changes.


RE: tkinter camera threads - Nick_tkinter - Apr-15-2021

No,it doesn't go through the first if statement.It goes to elif and all works fine ,but as I said the frame for camera output is a little bit flashing.What do you suggest me to do?


RE: tkinter camera threads - Yoriz - Apr-15-2021

Try removing the threading use the after call you have commented out instead.
Use another after call on label_frame.configure with a duration of 0 so it gets called in the GUI thread.


RE: tkinter camera threads - Nick_tkinter - Apr-16-2021

Hello again. I don't understand what you want me to do. Can you write it?
Thank you!