-1

I created a GUI using Tkinter. It has three buttons. one to capture image. second for capturing continuous images until i press third button i.e stop button.

When I press the first button it works fine i.e it runs a function which captures an image and save that image to a particular location.

But when I press the second button for continuous capturing of images then it starts capturing images but button gets stuck or GUI stopped working. because of this I am not able to stop this continuous capturing of images because I cannot press the third button i.e stop button.

I am running a while loop for continuous capturing of images and I can only break this loop if a Global variable STOP will have "0" in it and I can only make it zero by pressing stop button of GUI. But I cannot press stop button to make this STOP variable "0" so that loop can break. I think while loop is stopping the GUI main-loop and creating this problem. if you some have alternative or solution please share it.this is the code

Second button just call this function

def capture_video():
stop = '1'
l=Lepton()
l.enter()
path="/home/ubuntu/Desktop/IR_videos/vid_"
file=open("/home/ubuntu/Desktop/IR_videos/vid_no.txt",'r')
    no=file.read()
    file.close()
folder_name=path+no
os.mkdir(folder_name)
i=0
print("Image capturing started")
    print("____Press Stop button to stop____")
while True:
    a=l.capture()
    cv2.normalize(a,a,0,65535,cv2.NORM_MINMAX)
    np.right_shift(a,8,a)
    img=np.uint8(a)
    img_name=folder_name+"/"+str(i)+".jpg"
    cv2.imwrite(img_name,img)
    i=i+1
    if stop == "0":
        print("Image capturing stoped\n")
        print("Press video button to capture again")
        break
no=int(no)+1
file=open("/home/ubuntu/Desktop/IR_videos/vid_no.txt",'w')
file.write(str(no))
file.close()
cv2.destroyAllWindows()
l.exit()

here lepton is a class and capture() is function to capture image from flir lepton camera

and this is code of GUI stop button function :

def stop_it():
   lep.stop='0'
   time.sleep(1)
   lep.stop='1'
Rai Singh
  • 11
  • 3
  • 2
    Yes, the while loop is blocking code execution, but if you don't show your code it's going to be tricky to help you. – ChatterOne May 07 '18 at 07:07
  • Please provide code which reproduces the problem. – Peter May 07 '18 at 07:27
  • Please copy your code into the question using code formatting, do not post an image. The easiest way to do this is to copy and paste your code, the highlight it and press CTRL+K. – Artemis May 07 '18 at 07:30
  • Seems you can't use html in comments... I meant use `CTRL`+`K`. – Artemis May 07 '18 at 07:33

1 Answers1

0

You should run capture_video() on another thread than the main so that the main does not freeze. This way, you will be able to click on your Stop button. Also, make sure your stop variable inside capture_video is accessible by your stop_it function. In your capture_video, your stop variable is local and thus cannot be changed.

You can find an excellent answer on how to run a function on another thread here: https://stackoverflow.com/a/14331755/9729313

Here another example with threading and Tkinter: https://stackoverflow.com/a/50175727/9729313

Kefeng91
  • 802
  • 6
  • 10
  • 1
    It doesn't have to be another thread, one possible solution is to yield to the event loop after each frame captured, i.e. by rescheduling next frame event immediately after this one. – bipll May 07 '18 at 09:53