0
votes

OK I'm a little new to python so I'm looking for a way to deactivate my camera when I close the current page frame down. So far all I can find are examples of destroying the entire window which is not what I need. It took some playing around with it but so far I've manages to get the page to close and go back to my start page but the camera still remain on, if I reload my camera page the camera image is frozen until I activate the camera button again, it throws errors but seems to work outside of it leaving the camera on when I close down the page frame and return to my start page. Is there a fix for this?

# forth window frame page6 --------- Video
class Page6(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, height=600, width=800, bg="#010205")
        #label = Label(self, text ="VIDEO", font = JupiterReg50, fg="grey", bg="#010205")
        #label.grid(row = 0, column = 2, padx = 10, pady = 10, columnspan=2)

        # Is Camera On Or Off (Off By Default)
        global vidCam_Off
        vidCam_Off = True
        

        # Create a Frame To Display Our Video Feed Into
        camappFrm = Frame(self, bg="white", border=0, width=640, height=480)
        camappFrm.grid(row=1, column=0, columnspan=2, padx=70)
        camappFrm.grid_propagate(0)
        # Create a label in the frame
        lmain = Label(camappFrm)
        lmain.grid(row=1, column=0)
        

        # Capture from camera
        global cap
        cap = cv2.VideoCapture(0)
        global img_counter
        img_counter = 0

        def make_480p():
            cap.set(3, 640)
            cap.set(4, 480)

        # function for video streaming
        def video_stream():
            global cv2image
            global vidCam_Off
            global cap
            if (vidCam_Off == False):
                # Get the latest frame and convert into Image
                cv2image= cv2.cvtColor(cap.read()[1],cv2.COLOR_BGR2RGB)
                img = Image.fromarray(cv2image)
                # Convert image to PhotoImage
                imgtk = ImageTk.PhotoImage(image = img)
                lmain.imgtk = imgtk
                lmain.configure(image=imgtk)
                # Repeat after an interval to capture continiously
                lmain.after(1, video_stream) # Was Set At 20
            elif (vidCam_Off == True):
                cap.stop(1)
                cv2.waitKey(1)
                cap.release()