【发布时间】:2018-02-12 15:33:42
【问题描述】:
我正在尝试使用用户界面制作对象跟踪示例。我需要几个按钮来改变代码的方式。但是下面的代码没有按我的预期工作。布尔变量必须更改,因为当我按下按钮时我可以看到按钮功能分叉,但它不会影响连续工作的循环。如果有人知道我错过了什么,你能告诉我吗?感谢您的帮助。
import PIL
from PIL import Image, ImageTk
import Tkinter as tk
import argparse
import datetime
import cv2
import serial
import time
import os
import sys
#Start_Tracking = tk.BooleanVar
#Start_Tracking.set(False)
p1 = (310,230)
p2 = (330,250)
global Start_Tracking
Start_Tracking = False
tracker = cv2.TrackerKCF_create()
class Application:
def __init__(self, output_path = "./"):
self.vs = cv2.VideoCapture(0)
self.vs.set(3,640)
self.vs.set(4,320)
self.output_path = output_path
self.current_image = None
self.root = tk.Tk() # initialize root window
self.root.title("Object Tracking GUI") # set window title
# self.destructor function gets fired when the window is closed
self.root.protocol('WM_DELETE_WINDOW', self.destructor)
self.panel = tk.Label(self.root) # initialize image panel
self.panel.pack(padx=10, pady=10)
self.root.config(cursor="arrow")
# create a button, that when pressed, tracking will start
sel_btn = tk.Button(self.root, text='Select A Target', command=self.select_target)
sel_btn.pack(fill="both", expand=True, padx=10, pady=10)
# create a button, that when pressed, tracking will end
rel_btn = tk.Button(self.root, text="Release Selected Target", command=self.release_target)
rel_btn.pack(fill="both", expand=True, padx=10, pady=10)
# start a self.video_loop that constantly pools the video sensor
# for the most recently read frame
self.video_loop()
def video_loop(self):
if not Start_Tracking:
ok, frame = self.vs.read() # read frame from video stream
cv2.rectangle(frame, p1, p2, (0,0,200))
# frame = cv2.resize(frame, (1500,1000))
if ok: # frame captured without any errors
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) # convert colors from BGR to RGBA
self.current_image = Image.fromarray(cv2image) # convert image for PIL
#self.current_image= self.current_image.resize([1280,1024],PIL.Image.ANTIALIAS)
imgtk = ImageTk.PhotoImage(image=self.current_image) # convert image for tkinter
self.panel.imgtk = imgtk # anchor imgtk so it does not be deleted by garbage-collector
self.panel.config(image=imgtk) # show the image
#self.root.attributes("-fullscreen",True)
else:
ok, frame = self.vs.read() # do nothing for now
self.root.after(1, self.video_loop)
def select_target(self):
""" Take snapshot and save it to the file """
Start_Tracking=True
tracker = cv2.TrackerKCF_create()
print("Start Pressed")
def release_target(self):
""" Take snapshot and save it to the file """
Start_Tracking=False
tracker.clear()
print("Release Pressed")
def destructor(self):
""" Destroy the root object and release all resources """
print("[INFO] closing...")
self.root.destroy()
self.vs.release() # release web camera
cv2.destroyAllWindows() # it is not mandatory in this application
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", default="./",
help="path to output directory to store snapshots (default: current folder")
args = vars(ap.parse_args())
# start the app
print("[INFO] starting...")
pba = Application(args["output"])
pba.root.mainloop()
【问题讨论】:
-
欢迎来到Stack Overflow!请针对您的问题提供minimal reproducible example,而不是包含许多不相关部分的代码。
标签: python object tkinter tracking