我使用cv2.destroyAllWindows破坏了由Tkinter按钮调用的函数内的imshow()
窗口。
但是它不起作用,imshow()
窗口仍然保持打开状态
我正在制作一个运动检测器,当按下tkinter按钮时它会被激活。
如果我不使用tkinter按钮将imshow()
窗口正确关闭。
但是在我将功能与tkinter按钮一起使用后,它没有
import cv2,pandas
from datetime import datetime
def motion_detection():
CSV_path='Files\\'
first_frame=None
status_list=[None,None]
times=[]
df=pandas.DataFrame(columns=["Start","End"])
video=cv2.VideoCapture(0)
while True:
check, frame = video.read()
status=0
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if first_frame is None:
first_frame=gray
continue
delta_frame=cv2.absdiff(first_frame,gray)
thresh_frame=cv2.threshold(delta_frame,30,255,cv2.THRESH_BINARY)[1]
thresh_frame=cv2.dilate(thresh_frame,None,iterations=2)
(cnts,_)=cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 3000:
continue
status=1
(x,y,w,h)=cv2.boundingRect(contour)
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
status_list.append(status)
status_list=status_list[-2:]
if status_list[-1]==1 and status_list[-2]==0:
times.append(datetime.now())
if status_list[-1]==0 and status_list[-2]==1:
times.append(datetime.now())
cv2.imshow("Gray Frame",gray)
cv2.imshow("Delta Frame",delta_frame)
cv2.imshow("Threshold Frame",thresh_frame)
cv2.imshow("color Frame",frame)
key=cv2.waitKey(1)
if key==ord('q'):
if status==1:
times.append(datetime.now())
break
for i in range(0,len(times),2):
df=df.append({"Start":times[i],"End":times[i+1]},ignore_index=True)
df.to_csv(CSV_path+"Times.csv")
video.release()
cv2.destroyAllWindows
from tkinter import *
import motion_detector
main_window = Tk()
start_camera = Button(main_window, text="Start Camera", command=motion_detector.motion_detection)
start_camera.grid(row = 2, column = 0)
view_database = Button(main_window, text="View Database")
view_database.grid(row = 2, column = 2)
main_window.mainloop()
当我从键盘上按q时,我希望所有imshow()
窗口都关闭,但是即使函数停止执行也不会关闭