본 글은 기록용 글 입니다.
Tkinter를 이용해 OpenCV 영상을 띄우는 코드입니다.
import cv2
import PIL.Image, PIL.ImageTk
from tkinter import *
class App:
def __init__(self, window):
self.width, self.height = 640,480
self.window = window
self.window.geometry("640x480")
self.window.title("Tkinter + OpenCV")
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
self.canvas = Canvas(window, width = self.width, height = self.height)
self.canvas.pack()
self.canvas.bind("<Button-1>", self.mouse_down)
self.canvas.bind("<B1-Motion>", self.mouse_move)
self.canvas.bind("<ButtonRelease-1>", self.mouse_up)
self.canvas_on_down = False
self.delay = 33
self.update()
self.window.mainloop()
def update(self):
ret, frame = self.cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if self.canvas_on_down == True:
frame = cv2.rectangle(frame, (self.canvas_start_x, self.canvas_start_y), (self.canvas_move_x, self.canvas_move_y), (0, 0, 255), 2)
self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
self.canvas.create_image(0, 0, image = self.photo, anchor = NW)
self.window.after(self.delay, self.update)
def mouse_down(self, evt):
self.canvas_on_down = True
self.canvas_start_x, self.canvas_start_y = int(evt.x), int(evt.y)
self.canvas_move_x, self.canvas_move_y = int(evt.x), int(evt.y)
def mouse_move(self, evt):
self.canvas_move_x, self.canvas_move_y = int(evt.x), int(evt.y)
def mouse_up(self, evt):
self.canvas_on_down = False
App(Tk())
728x90
'소프트웨어 > 파이썬' 카테고리의 다른 글
[파이썬] Pyinstaller dll 포함한 빌드 - 실패공유 (7) | 2022.01.13 |
---|---|
[파이썬] ESP32-CAM 스트리밍 영상 불러오기(Tkinter, OpenCV) (8) | 2021.07.02 |
[파이썬] ESP CAM의 스트리밍 영상 opencv로 불러오기(수정) (0) | 2021.06.29 |
[파이썬] Object Tracking Tkinter로 만들기 (0) | 2021.06.27 |
[파이썬] ESP CAM의 스트리밍 영상 opencv로 불러오기 (0) | 2021.02.03 |
댓글