python dlib实现小程序(3)实现图像跟踪

# coding=utf-8 import sys import dlib import cv2class mycorrelationTracker(object): def __init__(self,windowname = 'default window',cameranum = 0): self.STATUS_RUN_WITHOUT_TRACKER = 0 # 不跟踪目标,但是实时显示 self.STATUS_RUN_WITH_TRACKER =1# 跟踪目标,实时显示 self.STATUS_PAUSE = 2 # 暂停,卡在当前帧 self.STATUS_BREAK = 3 # 退出 self.status = self.STATUS_RUN_WITHOUT_TRACKERself.track_window = None# 实时跟踪鼠标的跟踪区域 self.selection = None self.drag_start = None # 要检测的物体所在区域 self.start_flag = None# 标记,是否开始拖动鼠标cv2.namedWindow(windowname,cv2.WINDOW_AUTOSIZE) cv2.setMouseCallback(windowname,self.onmouseckicked) self.windowname = windownameself.cap = cv2.VideoCapture(cameranum)self.tracker = dlib.correlation_tracker()self.fame = Nonedef keyeventhandler(self): keyvalue = https://www.it610.com/article/cv2.waitKey(1) if keyvalue == 27:#27是ESC的ASIC码 self.status = self.STATUS_BREAK if keyvalue == 32: #空格 if self.status != self.STATUS_PAUSE: self.status = self.STATUS_PAUSE else: if self.track_window: self.status = self.STATUS_RUN_WITH_TRACKER self.start_flag = True else: self.status = self.STATUS_RUN_WITHOUT_TRACKER if keyvalue ==13: #回车 if self.status == self.STATUS_PAUSE: if self.track_window: self.status = self.STATUS_RUN_WITH_TRACKER self.start_flag = Truedef processhandler(self):if self.status == self.STATUS_RUN_WITHOUT_TRACKER: ret ,self.fame = self.cap.read() cv2.imshow(self.windowname,self.fame)elif self.status==self.STATUS_PAUSE: img_first =self.fame.copy() if self.track_window: cv2.rectangle(img_first,(self.track_window[0],self.track_window[1]),(self.track_window[2],self.track_window[3]),(0,0,255),1) elif self.selection: cv2.rectangle(img_first,(self.selection[0],self.selection[1]),(self.selection[2],self.selection[3]),(0,0,255),1)cv2.imshow(self.windowname,img_first)elif self.status == self.STATUS_BREAK: self.cap.release() cv2.destroyAllWindows() sys.exit()elif self.status == self.STATUS_RUN_WITH_TRACKER: ret,self.fame = self.cap.read() if self.start_flag: self.tracker.start_track(self.fame,dlib.rectangle(self.track_window[0],self.track_window[1],self.track_window[2],self.track_window[3])) self.start_flag = False else: self.tracker.update(self.fame)box_predict = self.tracker.get_position() cv2.rectangle(self.fame,(int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom())),(0,0,255),1) cv2.imshow(self.windowname,self.fame)def onmouseckicked(self,event,x,y,flags,param): if event == cv2.EVENT_LBUTTONDOWN and self.status != self.STATUS_PAUSE: print("please press the block") return if event == cv2.EVENT_LBUTTONDOWN: self.drag_start = (x,y) self.track_window = None if self.drag_start: print(x,y,'s') print(self.drag_start) xMin = min(x, self.drag_start[0]) yMin = min(y, self.drag_start[1]) xMax = max(x, self.drag_start[0]) yMax = max(y, self.drag_start[1]) self.selection = (xMin, yMin, xMax, yMax) if event == cv2.EVENT_LBUTTONUP:# 鼠标左键松开 self.drag_start = None self.track_window = self.selection self.selection = Nonedef run(self): while(1): self.keyeventhandler() self.processhandler()if __name__ == '__main__': tesetracker = mycorrelationTracker(windowname='image',cameranum=0) tesetracker.run()

【python dlib实现小程序(3)实现图像跟踪】其中cv2.setMouseCallback(windowname,self.onmouseckicked) 是一直在检测你的鼠标在窗口中的活动的,后面onmouseckicked 是回调函数,意思就是说这个函数会返回五个值到这个回调函数中,其中有第一个值是EVENT 事件,后面两个是当前鼠标的坐标值。因为这边用了类值,如果没有用类直接写函数的时候一定要定义全局变量,比如像这样:
def onMouseClicked(event,x,y,flags,param): global selection,track_window,drag_start if event == cv2.EVENT_LBUTTONDOWN: drag_start = (x,y) track_window = None if drag_start: print(x,y,'s') print(drag_start) xmin = min(x,drag_start[0]) ymin = min(y,drag_start[1]) xmax = max(x,drag_start[0]) ymax = max(y,drag_start[1]) selection = (xmin,ymin,xmax,ymax)if event == cv2.EVENT_LBUTTONUP: drag_start = None track_window = selection selection = None

因为鼠标每动一次就会调用这个回调函数,不把值定义为全局量的话,上一次已经赋值好的值在这一次就会没有。
大佬的地址:https://blog.csdn.net/hongbin_xu/article/details/78359663

    推荐阅读