Python Forum
[Tkinter] Modify Class on Button Click
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Modify Class on Button Click
#1
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!
Reply
#2
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
Reply
#3
(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
How to rotate a video with OpenCV

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()
Yoriz write May-09-2021, 09:13 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
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))
                ...
                ...
Reply
#5
That worked Smile thank you very much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Kivy] Acces atributes from a button defined in a class inside the .kv Tomli 2 282 Jun-10-2021, 01:05 AM
Last Post: Tomli
  [Tkinter] Button click problem using OOP JohnB 5 812 Oct-21-2020, 12:43 PM
Last Post: JohnB
  tkinter | Button color text on Click Maryan 2 879 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Class function does not create command button Heyjoe 2 655 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Closing window on button click not working kenwatts275 4 867 May-03-2020, 01:59 PM
Last Post: deanhystad
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 4,775 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [PyQt] Problem how to click a button inside a group box? mart79 2 1,298 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 2,228 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Button in one class, methods in another one alan9979 4 1,028 Jul-11-2019, 01:57 AM
Last Post: joe_momma
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 1,715 May-12-2019, 08:17 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020