0

I need one help in my code what should i do to close test 1 when i click on test 2 to start it again when click on stop.

i have declared two video in two button in my code in Test 1,Test 2 when i click test1 it plays it when i click on 2nd it do same but.. the thing is is when i play test 1 and click on test 2 after that it plays both one and top on other. and when i click stop and click again on test 1 it dont start.

import cv2
from tkinter import *
from PIL import Image, ImageTk
import threading
from tkinter.filedialog import askopenfilename


def resize(image):
    im = image
    new_siz = siz
    im.thumbnail(new_siz, Image.ANTIALIAS)
    return im

def size(event):
    global siz
    if siz == screenWH:
        siz = (600, 200)
    else:
        siz = screenWH
        win.state('zoomed')
    print ("size is:"), siz

def view_frame_video():
    vc = cv2.VideoCapture('C:\\Users\\Devendra\\Desktop\\VID_20190125_172553.mp4')
    if vc.isOpened():
        rval , frame = vc.read()
    else:
        rval = False

    while rval:
        rval, frame = vc.read()
        img =Image.fromarray(frame)
        img = resize(img)
        imgtk = ImageTk.PhotoImage(img)
        lbl.config(image=imgtk)
        lbl.img = imgtk
        if stop == True:
            vc.release()
            break      #stop the loop thus stops updating the label and reading imagge frames
        cv2.waitKey(1)
    vc.release()

def view_frame_video2():
    vc = cv2.VideoCapture('C:\\Users\\Devendra\\Desktop\\VID_20190129_162657.mp4')
    if vc.isOpened():
        rval , frame = vc.read()
    else:
        rval = False

    while rval:
        rval, frame = vc.read()
        img =Image.fromarray(frame)
        img = resize(img)
        imgtk = ImageTk.PhotoImage(img)
        lbl.config(image=imgtk)
        lbl.img = imgtk
        if stop == True:
            vc.release()
            break      #stop the loop thus stops updating the label and reading imagge frames
        cv2.waitKey(1)
    vc.release()

def stop_():
    global stop
    stop = True

def play():
    stop = False
    t = threading.Thread(target=view_frame_video)
    t.start()
def play2():
    stop = False
    t = threading.Thread(target=view_frame_video2)
    t.start()



win = Tk()
win.title('Data Logger')
win.geometry('1300x650')

stop = None
screenWH = (win.winfo_screenwidth(), win.winfo_screenheight())
siz = (600, 600)

Label(text='Prosthetic Hand Testing Software',font = ('times',22), fg = 'Red',width = 104, height = 2).pack()



def import_csv_data():
    global v
    csv_file_path = askopenfilename()
    print(csv_file_path)
    v.set(csv_file_path)
Label(win, text='File Path',font = ('arial' ,10), fg = 'black', width = 104, height = 2).pack()
v = StringVar()
entry = Entry(textvariable = v)
entry.pack()
Button(text='Browse Data Set',command=import_csv_data).pack()


frm_ = Frame(bg='black')
frm_.pack()
lbl = Label(frm_, bg='black')
lbl.pack(expand=True)
lbl.bind('<Double-Button-1>', size)


buttonframe = Frame(win)
buttonframe.pack(side=BOTTOM)
Button(text='Test 1', command = play,fg="Green", height=2, width=20).pack(side=LEFT)
Button(text='Test 2', command = play2,fg="Green", height=2, width=20).pack(side=LEFT)#,padx=21, pady=18)
Button(buttonframe,text='Stop', command = stop_,fg="Orange", height=2, width=20).pack(side=LEFT,padx=20, pady=18)
Button(buttonframe,text='Exit',fg="Red",command=win.destroy,height=2, width=20).pack(side=LEFT,padx=20, pady=18)
win.mainloop()

should close test 1 and play test 2 when i click on test 2 after test 1 should restart playing test 1,2 when i stop it and click again on any button

  • You need to declare `stop` as global in `play()` and `play2()` functions. – acw1668 Jan 30 '19 at 11:00
  • thank you it worked :) one more.. when i play test 1 and click test 2 while its running test 1 donot stop and test 2 play over any idea – Devendra Kullarkar Jan 30 '19 at 11:44
  • It is because the thread of test 1 is still running as the value of `stop` is still False when you start test 2 thread. You need to find a way to stop test 1 thread. – acw1668 Jan 30 '19 at 13:26

1 Answers1

1

The main issue is that your code will create new thread whenever you click the Test 1 or Test 2 button. However the old threads are still running and so there are more than one thread updating the lbl which causes the overlap of images. To overcome it, one thread is created for playing video. The Test 1 and Test 2 buttons are used to select which video file as the source. Below is modified (and simplified) code based on your code:

import cv2
from tkinter import *
from PIL import Image, ImageTk
import threading

def play_video():
    while True:
        try:
            if vc.isOpened():
                rval, frame = vc.read()
                if rval:
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    img = Image.fromarray(frame)
                    img.thumbnail(siz, Image.ANTIALIAS)
                    imgtk = ImageTk.PhotoImage(img)
                    lbl.config(image=imgtk)
                    lbl.img = imgtk
        except:
            pass

def play():
    try:
        vc.open('C:\\Users\\Devendra\\Desktop\\VID_20190125_172553.mp4')
    except:
        pass

def play2():
    try:
        vc.open('C:\\Users\\Devendra\\Desktop\\VID_20190129_162657.mp4')
    except:
        pass

def stop_():
    try:
        vc.release()
    except:
        pass


win = Tk()
win.title('Data Logger')
win.geometry('1300x650')

siz = (600, 400)

Label(text='Prosthetic Hand Testing Software', font=('times',22), fg='Red', width=104, height=2).pack()

lbl = Label(bg='black', image=ImageTk.PhotoImage(Image.new('RGB', siz)))
lbl.pack(expand=True)

buttonframe = Frame(win)
buttonframe.pack(side=BOTTOM)
Button(buttonframe, text='Test 1', command=play, fg="Green", height=2, width=20).pack(side=LEFT)
Button(buttonframe, text='Test 2', command=play2, fg="Green", height=2, width=20).pack(side=LEFT)
Button(buttonframe, text='Stop', command=stop_, fg="Orange", height=2, width=20).pack(side=LEFT, padx=20, pady=18)
Button(buttonframe, text='Exit', command=win.destroy, fg="red", height=2, width=20).pack(side=LEFT, padx=20, pady=18)

vc = cv2.VideoCapture()

# create and start the video playing thread
t = threading.Thread(target=play_video)
t.setDaemon(True)
t.start()

win.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34