[Tkinter] Modify Class on Button Click - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Modify Class on Button Click (/thread-33587.html) |
Modify Class on Button Click - KDog - May-08-2021 I have a program that shows the live webcam feed from my laptop. It uses Tkinter and OpenCV. I would like to flip the live video when a button is pressed. Any help is appreciated as I am a complete beginner with Classes! RE: Modify Class on Button Click Tkinter - Yoriz - May-09-2021 Create a bind on a button and in the event handler add the code to flip the video How to rotate a video with OpenCV RE: Modify Class on Button Click Tkinter - KDog - May-09-2021 (May-09-2021, 10:04 AM)Yoriz Wrote: Create a bind on a button and in the event handler add the code to flip the video Thanks for the quick response. Please see code below, I have created a button and function but don't know how to code the function: from tkinter import * import cv2 from PIL import Image, ImageTk import time from tkinter import filedialog class App: def __init__(self, video_source=0): self.appName = "Kamera" self.window = Tk() self.window.title(self.appName) self.window.resizable(0,0) #self.window.wm_iconbitmap("cam.ico") self.window['bg'] = 'black' self.video_source = video_source self.vid = MyVideoCapture(self.video_source) #self.label = Label(self.window, text=self.appName, font=15, bg='blue', fg='white').pack(side=TOP, fill=BOTH) self.canvas = Canvas(self.window, width=self.vid.width, height=self.vid.height, bg='red') self.canvas.pack() self.btn_snapshot = Button(self.window, text="Snapshot", width=5, command=self.snapshot) self.btn_snapshot.pack(anchor=CENTER) self.btn_flip = Button(self.window, text="Flip Image", width=7, command=self.flip_img) self.btn_flip.pack(anchor=CENTER) self.update() self.window.mainloop() def flip_img(self): #need code here def snapshot(self): check, frame = self.vid.getFrame() if check: filename = filedialog.asksaveasfilename( defaultextension='jpg', title="Choose Filename", filetypes=[("JPEG Files", '*.jpg' )]) #image = "IMG-" + time.strftime("%H-%M-%S-%d-%m") + ".jpg" cv2.imwrite(filename, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) #msg = Label(self.window, text='Image Saved' + image, bg='black', fg='green').place(x=430, y=510) else: messagebox.showerror("paint says", "unable to save image ,\n something went wrong") def update(self): isTrue, frame = self.vid.getFrame() if isTrue: self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame)) self.canvas.create_image(0,0,image = self.photo, anchor = NW) self.window.after(15, self.update) class MyVideoCapture: def __init__(self, video_source=0): self.vid = cv2.VideoCapture(video_source) if not self.vid.isOpened(): raise ValueError("Unable to open this camera \n select another video source", video_source) self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT) def getFrame(self): if self.vid.isOpened(): isTrue, frame = self.vid.read() #frame = cv2.flip(frame, 1) if isTrue: return (isTrue, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) else: return (isTrue, None) else: return (isTrue, None) def __del__(self): if self.vid.isOpened(): self.vid.release() if __name__ == "__main__": App() RE: Modify Class on Button Click Tkinter - Yoriz - May-09-2021 I have never used cv2 so I'm not quite sure how flip works, you can try adding the following lines and see what happens. class App: def __init__(self, video_source=0): self.appName = "Kamera" ... ... def flip_img(self): #need code here self.vid.flipped = not self.vid.flipped # New Line class MyVideoCapture: def __init__(self, video_source=0): self.vid = cv2.VideoCapture(video_source) ... ... self.flipped = False # New Line def getFrame(self): if self.vid.isOpened(): isTrue, frame = self.vid.read() if isTrue and self.flipped: # New Line frame = cv2.flip(frame, 1) # Uncommented Line and indented into an if statement if isTrue: return (isTrue, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) ... ... RE: Modify Class on Button Click Tkinter - KDog - May-11-2021 That worked thank you very much! |