# -*- coding:utf-8 -*-
import numpy as np
import argparse
import cv2
points=[]
opencv_object_trckers = {
"csrt": cv2.TrackerCSRT_create,
"kcf": cv2.TrackerKCF_create,
"boosting": cv2.TrackerBoosting_create,
"mil": cv2.TrackerMIL_create,
"tid":cv2.TrackerTLD_create,
"medianflow":cv2.TrackerMedianFlow_create,
"mosse":cv2.TrackerMOSSE_create
}
def cam_operate(file):
trackers = cv2.MultiTracker_create()
vs = cv2.VideoCapture(file)
while True:
_,frame = vs.read()
if frame is None:
break
width = 600
(h, w) = frame.shape[:2]
r = width/float(w)
dim = (width,int(h*r))
frame = cv2.resize(frame,dim,interpolation = cv2.INTER_AREA)
success,boxes = trackers.update(frame)
for box in boxes:
x,y,w,h = [int(v) for v in box]
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('frame',frame)
key = cv2.waitKey(100) & 0xFF
if key == ord('s'):
box = cv2.selectROI("frame",frame,fromCenter=False,showCrosshair=True)
tracker =opencv_object_trckers["csrt"]()
trackers.add(tracker,frame,box)
elif key == ord('q') or key ==27 :
break
vs.release()
cv2.destroyAllWindows()
def main():
file = "soccer_01.mp4"
cam_operate(file)
if __name__ == "__main__":
main()
按s键选择区域,按enter开始追踪,esc或q键退出
# -*- coding:utf-8 -*-
import numpy as np
import argparse
import cv2
import dlib
points=[]
opencv_object_trckers = {
"csrt": cv2.TrackerCSRT_create,
"kcf": cv2.TrackerKCF_create,
"boosting": cv2.TrackerBoosting_create,
"mil": cv2.TrackerMIL_create,
"tid":cv2.TrackerTLD_create,
"medianflow":cv2.TrackerMedianFlow_create,
"mosse":cv2.TrackerMOSSE_create
}
def mouse_event_handler(event,x,y,flags,parms):
global points
if event == cv2.EVENT_LBUTTONDOWN:
points=[(x,y)]
elif event == cv2.EVENT_LBUTTONUP:
points.append((x,y))
def show_info(frame,tracking_status):
pos1 = (10,20)
pos2 = (10,40)
pos3 = (10,60)
info1 = "click selct an area,start tracking"
info2 = "'1'-> start tracking,'2'->stop stacking,'q'->exit"
cv2.putText(frame,info1,pos1,cv2.FONT_HERSHEY_COMPLEX,0.5,(255,255,255))
cv2.putText(frame,info2,pos2,cv2.FONT_HERSHEY_COMPLEX,0.5,(255,255,255))
if tracking_status == True:
cv2.putText(frame,"status:tracking now ...",pos3,cv2.FONT_HERSHEY_COMPLEX,0.5,(0,255,0))
else:
cv2.putText(frame,"status:tracking stop",pos3,cv2.FONT_HERSHEY_COMPLEX,0.5,(255,0,0))
def mouse_event_handler(event,x,y,flags,parms):
global points
if event == cv2.EVENT_LBUTTONDOWN:
points=[(x,y)]
elif event == cv2.EVENT_LBUTTONUP:
points.append((x,y))
def cam_operate(file):
global points
vs = cv2.VideoCapture(file)
name_window = "object tracking"
cv2.namedWindow(name_window)
cv2.moveWindow(name_window,100,100)
cv2.setMouseCallback(name_window,mouse_event_handler)
tracker = dlib.correlation_tracker()
tracking_status = False
while True:
_,frame = vs.read()
if frame is None:
break
show_info(frame,tracking_status)
#print (len(points))
if len(points) == 2:
cv2.rectangle(frame,points[0],points[1],(0,255,0),3)
dlib_rect = dlib.rectangle(points[0][0],points[0][1],points[1][0],points[1][1])
if tracking_status is True:
tracker.update(frame)
pos = tracker.get_position()
cv2.rectangle(frame,(int(pos.left()),int(pos.top())),(int(pos.right()),int(pos.bottom())),(0,255,0),3)
key = cv2.waitKey(100) & 0xFF
if key == ord('1'):
if len(points) == 2:
tracker.start_track(frame,dlib_rect)
tracking_status = True
points=[]
elif key == ord('2'):
points=[]
tracking_status = False
elif key == ord('3'):
box = cv2.selectROI(name_window,frame,fromCenter=False,showCrosshair=True)
if points == []:
points.append((box[0],box[1]))
points.append((box[0]+box[2],box[1]+box[3]))
elif key == ord('q') or key == 27:
break
cv2.imshow(name_window,frame)
vs.release()
cv2.destroyAllWindows()
def main():
file = "soccer_01.mp4"
cam_operate(file)
if __name__ == "__main__":
main()
按鼠标左键选择区域,按1键开始追踪,2键清除选择区域,esc或q键退出