(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()