The image taken from the camera is displayed on the GUI screen. It also adds the ability to take a snapshot when you press the shutter on the camera.
Use VideoCapture () to set up a camera instance vcap. video source = 0 Built-in camera video_source = 1 USB camera
At that time, the width and height of the Video image are also acquired.
self.vcap = cv2.VideoCapture( video_source )
self.width = self.vcap.get( cv2.CAP_PROP_FRAME_WIDTH )
self.height = self.vcap.get( cv2.CAP_PROP_FRAME_HEIGHT )
Use Tkinter to set up a Canvas instance.
self.canvas1 = tk.Canvas(self.frame_cam)
self.canvas1.configure( width= self.width, height=self.height)
self.canvas1.grid(column= 0, row=0,padx = 10, pady=10)
Use OpenCV read () to get the vcap image. Store the acquired one in frame. Convert what you got with BGR to RGB and store it in frame again.
_, frame = self.vcap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Once you get the frame, use Pillow to convert it to photo. The reason is that Canvas can only display images processed by Pillow. (Perhaps) Then, the photo is displayed on Canvas.
#OpenCV frame -> Pillow Photo
self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
#self.photo -> Canvas
self.canvas1.create_image(0,0, image= self.photo, anchor = tk.NW)
Set the update cycle with delay. The unit is milliseconds. This time, set it to 15 milliseconds. ** See how the FPS of the video changes when you change the value of this delay. ** ** Insert update () into the main part and after () into the part that displays the image on the Canvas.
self.delay = 15 #[mili seconds]
self.update()
#...............
def update(self):
#Get a frame from the video source
_, frame = self.vcap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
#self.photo -> Canvas
self.canvas1.create_image(0,0, image= self.photo, anchor = tk.NW)
self.master.after(self.delay, self.update)
The snapshot is processed by converting the frame learned by read () of OpenCV to RGB and then saving it as a file with cv2.imwrite.
def press_snapshot_button(self):
# Get a frame from the video source
_, frame = self.vcap.read()
frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imwrite( "frame-" + time.strftime( "%Y-%d-%m-%H-%M-%S" ) + ".jpg ",
cv2.cvtColor( frame1, cv2.COLOR_BGR2RGB ) )
1.Python OpenCV - show a video in a Tkinter window 2. [Raspberry Pi] How to display the image of the camera module on Tkinter 3. Load video with Python, OpenCV (file / camera image) 4. Modified version: I made a breakout with tkinter in Python.
import tkinter as tk
from tkinter import ttk
import cv2
import PIL.Image, PIL.ImageTk
from tkinter import font
import time
class Application(tk.Frame):
def __init__(self,master, video_source=0):
super().__init__(master)
self.master.geometry("700x700")
self.master.title("Tkinter with Video Streaming and Capture")
# ---------------------------------------------------------
# Font
# ---------------------------------------------------------
self.font_frame = font.Font( family="Meiryo UI", size=15, weight="normal" )
self.font_btn_big = font.Font( family="Meiryo UI", size=20, weight="bold" )
self.font_btn_small = font.Font( family="Meiryo UI", size=15, weight="bold" )
self.font_lbl_bigger = font.Font( family="Meiryo UI", size=45, weight="bold" )
self.font_lbl_big = font.Font( family="Meiryo UI", size=30, weight="bold" )
self.font_lbl_middle = font.Font( family="Meiryo UI", size=15, weight="bold" )
self.font_lbl_small = font.Font( family="Meiryo UI", size=12, weight="normal" )
# ---------------------------------------------------------
# Open the video source
# ---------------------------------------------------------
self.vcap = cv2.VideoCapture( video_source )
self.width = self.vcap.get( cv2.CAP_PROP_FRAME_WIDTH )
self.height = self.vcap.get( cv2.CAP_PROP_FRAME_HEIGHT )
# ---------------------------------------------------------
# Widget
# ---------------------------------------------------------
self.create_widgets()
# ---------------------------------------------------------
# Canvas Update
# ---------------------------------------------------------
self.delay = 15 #[mili seconds]
self.update()
def create_widgets(self):
#Frame_Camera
self.frame_cam = tk.LabelFrame(self.master, text = 'Camera', font=self.font_frame)
self.frame_cam.place(x = 10, y = 10)
self.frame_cam.configure(width = self.width+30, height = self.height+50)
self.frame_cam.grid_propagate(0)
#Canvas
self.canvas1 = tk.Canvas(self.frame_cam)
self.canvas1.configure( width= self.width, height=self.height)
self.canvas1.grid(column= 0, row=0,padx = 10, pady=10)
# Frame_Button
self.frame_btn = tk.LabelFrame( self.master, text='Control', font=self.font_frame )
self.frame_btn.place( x=10, y=550 )
self.frame_btn.configure( width=self.width + 30, height=120 )
self.frame_btn.grid_propagate( 0 )
#Snapshot Button
self.btn_snapshot = tk.Button( self.frame_btn, text='Snapshot', font=self.font_btn_big)
self.btn_snapshot.configure(width = 15, height = 1, command=self.press_snapshot_button)
self.btn_snapshot.grid(column=0, row=0, padx=30, pady= 10)
# Close
self.btn_close = tk.Button( self.frame_btn, text='Close', font=self.font_btn_big )
self.btn_close.configure( width=15, height=1, command=self.press_close_button )
self.btn_close.grid( column=1, row=0, padx=20, pady=10 )
def update(self):
#Get a frame from the video source
_, frame = self.vcap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
#self.photo -> Canvas
self.canvas1.create_image(0,0, image= self.photo, anchor = tk.NW)
self.master.after(self.delay, self.update)
def press_snapshot_button(self):
# Get a frame from the video source
_, frame = self.vcap.read()
frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imwrite( "frame-" + time.strftime( "%Y-%d-%m-%H-%M-%S" ) + ".jpg ",
cv2.cvtColor( frame1, cv2.COLOR_BGR2RGB ) )
def press_close_button(self):
self.master.destroy()
self.vcap.release()
def main():
root = tk.Tk()
app = Application(master=root)#Inherit
app.mainloop()
if __name__ == "__main__":
main()
Recommended Posts