Python Forum
tkinter camera threads
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter camera threads
#1
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.
Reply
#2
There are some examples of using threads with tkinter in the following link
https://python-forum.io/Thread-Tkinter-H...ng-the-gui
Reply
#3
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.
Reply
#4
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.
Reply
#5
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?
Reply
#6
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.
Reply
#7
Hello again. I don't understand what you want me to do. Can you write it?
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter frame camera opencv Nick_tkinter 9 1,203 Mar-21-2021, 06:41 PM
Last Post: Nick_tkinter
  Raspberry Pi camera live feed in tkinter robgar2001 6 7,648 Sep-30-2018, 10:49 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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