I have been working on a small machine learning project of classification of objects in a video. I am also implementing a Tkinter GUI for showing the frames of the video in a frame in window. But i have a problem that i have to close the window and then the window again opens to show the next frame. here is the code
global img1
temp_up_list = []
temp_down_list = []
up_list = [0, 0, 0, 0, 0]
down_list = [0, 0, 0, 0, 0]
cap = cv2.VideoCapture('video.mp4')
while True:
success, img = cap.read()
img = cv2.resize(img,(0,0),None,0.5,0.5)
ih, iw, channels = img.shape
blob = cv2.dnn.blobFromImage(img, 1 / 255, (input_size, input_size), [0, 0, 0], 1, crop=False)
# Set the input of the network
net.setInput(blob)
layersNames = net.getLayerNames()
outputNames = [(layersNames[i - 1]) for i in net.getUnconnectedOutLayers()]
# Feed data to the network
outputs = net.forward(outputNames)
# Find the objects from the network output
postProcess(outputs,img)
# Draw the crossing lines
cv2.line(img, (0, middle_line_position), (iw, middle_line_position), (255, 0, 255), 2)
cv2.line(img, (0, up_line_position), (iw, up_line_position), (0, 0, 255), 2)
cv2.line(img, (0, down_line_position), (iw, down_line_position), (0, 0, 255), 2)
# # Draw counting texts in the frame
# cv2.putText(img, "Up", (110, 20), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# cv2.putText(img, "Down", (160, 20), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# cv2.putText(img, "Car: "+str(up_list[0])+" "+ str(down_list[0]), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# cv2.putText(img, "Motorbike: "+str(up_list[1])+" "+ str(down_list[1]), (20, 60), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# cv2.putText(img, "Bus: "+str(up_list[2])+" "+ str(down_list[2]), (20, 80), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# cv2.putText(img, "Truck: "+str(up_list[3])+" "+ str(down_list[3]), (20, 100), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# cv2.putText(img, "Person: "+str(up_list[4])+" "+ str(down_list[4]), (20, 120), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# Show the frames
# cv2.imshow('Output', img)
root = Tk()
root.title("Identification of vehicles")
root.geometry("1250x800")
root.configure(background='white')
titleWidget = Frame(root, width = 600, height = 60, bd=1, bg='white', relief=SUNKEN)
titleWidget.grid_propagate(False)
buttonWidget = Frame(root, width = 350, height = 400, bd=1, bg='white', relief=SUNKEN)
buttonWidget.grid_propagate(False)
labelWidget = Canvas(root, width = 600, height = 400, bd=1, bg='white', relief=SUNKEN)
labelWidget.grid_propagate(False)
label2Widget = Frame(root, width = 600, height = 400, bd=1, bg='white', relief=SUNKEN)
# label2Widget.grid_propagate(False)
lbl1 = Label(titleWidget, text="Heavy vehicle identification using AI & deep learning",bg="white",fg="black",font='Helvetica 20 bold')
lbl1.pack()
top0 = Label(buttonWidget, text="Count details",bg="white",fg="black",font='Helvetica 15 bold')
top0.grid(row=0,column=1,padx=5,pady=5)
top1 = Label(buttonWidget, text="up ",bg="white",fg="black",font='Helvetica 15 bold')
top1.grid(row=2,column=1,padx=5,pady=5)
top2 = Label(buttonWidget, text="down ",bg="white",fg="black",font='Helvetica 15 bold')
top2.grid(row=2,column=2,padx=5,pady=5)
lbl1 = Label(buttonWidget, text="Car :",bg="white",fg="black",font='Helvetica 15 bold')
lbl1.grid(row=4,column=0,padx=5,pady=5)
lbl2 = Label(buttonWidget, text="Truck :",bg="white",fg="black",font='Helvetica 15 bold')
lbl2.grid(row=5,column=0,padx=5,pady=5)
lbl3 = Label(buttonWidget, text="MotorBike :",bg="white",fg="black",font='Helvetica 15 bold')
lbl3.grid(row=6,column=0,padx=5,pady=5)
lbl4 = Label(buttonWidget, text="Person :",bg="white",fg="black",font='Helvetica 15 bold')
lbl4.grid(row=7,column=0,padx=5,pady=5)
lbl5 = Label(buttonWidget, text="Bus :",bg="white",fg="black",font='Helvetica 15 bold')
lbl5.grid(row=8,column=0,padx=5,pady=5)
lbl6 = Label(buttonWidget, text=str(up_list[0]),bg="white",fg="black",font='Helvetica 15 bold')
lbl6.grid(row=4,column=1,padx=5,pady=5)
lbl7 = Label(buttonWidget, text=str(up_list[3]),bg="white",fg="black",font='Helvetica 15 bold')
lbl7.grid(row=5,column=1,padx=5,pady=5)
lbl8 = Label(buttonWidget, text=str(up_list[1]),bg="white",fg="black",font='Helvetica 15 bold')
lbl8.grid(row=6,column=1,padx=5,pady=5)
lbl9 = Label(buttonWidget, text=str(up_list[4]),bg="white",fg="black",font='Helvetica 15 bold')
lbl9.grid(row=7,column=1,padx=5,pady=5)
lbl11 = Label(buttonWidget, text=str(up_list[2]),bg="white",fg="black",font='Helvetica 15 bold')
lbl11.grid(row=8,column=1,padx=5,pady=5)
lbl12 = Label(buttonWidget, text=str(down_list[0]),bg="white",fg="black",font='Helvetica 15 bold')
lbl12.grid(row=4,column=2,padx=5,pady=5)
lbl13 = Label(buttonWidget, text=str(down_list[3]),bg="white",fg="black",font='Helvetica 15 bold')
lbl13.grid(row=5,column=2,padx=5,pady=5)
lbl14 = Label(buttonWidget, text=str(down_list[1]),bg="white",fg="black",font='Helvetica 15 bold')
lbl14.grid(row=6,column=2,padx=5,pady=5)
lbl15 = Label(buttonWidget, text=str(down_list[4]),bg="white",fg="black",font='Helvetica 15 bold')
lbl15.grid(row=7,column=2,padx=5,pady=5)
lbl16 = Label(buttonWidget, text=str(down_list[2]),bg="white",fg="black",font='Helvetica 15 bold')
lbl16.grid(row=8,column=2,padx=5,pady=5)
playbtn = Button(label2Widget, text='Start',height= 1, width=13,bg="white",fg="red",font='Helvetica 15 bold')
playbtn.grid(row=0,column=0,padx=40,pady=10)
stopbtn = Button(label2Widget, text='Stop',command=lambda: close(),height= 1, width=13,bg="white",fg="red",font='Helvetica 15 bold')
stopbtn.grid(row=0,column=2,padx=40,pady=10)
titleWidget.grid(row = 0, column = 0,padx=10,pady=10)
labelWidget.grid(row = 1, column = 0,padx=0,pady=30)
label2Widget.grid(row = 2, column = 0,padx=0,pady=30)
buttonWidget.grid(row = 1, column = 1,padx=0,pady=30)
color_coverted = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pil_image=Image.fromarray(color_coverted)
img1 = ImageTk.PhotoImage(pil_image)
labelWidget.create_image(0,0,anchor=NW, image=img1)
root.mainloop()
if cv2.waitKey(1) == ord('q'):
break
# Write the vehicle counting information in a file and save it
with open("data.csv", 'w') as f1:
cwriter = csv.writer(f1)
cwriter.writerow(['Direction', 'car', 'motorbike', 'bus', 'truck', 'person'])
up_list.insert(0, "Up")
down_list.insert(0, "Down")
cwriter.writerow(up_list)
cwriter.writerow(down_list)
f1.close()
# print("Data saved at 'data.csv'")
# Finally realese the capture object and destroy all active windows
cap.release()
cv2.destroyAllWindows()```
[enter image description here][1]
[1]: https://i.stack.imgur.com/NKc9q.png