|
| 1 | +import numpy as np |
| 2 | +import cv2 |
| 3 | +import sys |
| 4 | +from time import time |
| 5 | + |
| 6 | +import KCF |
| 7 | + |
| 8 | + |
| 9 | +selectingObject = False |
| 10 | +initTracking = False |
| 11 | +onTracking = False |
| 12 | +ix, iy, cx, cy = -1, -1, -1, -1 |
| 13 | +w, h = 0, 0 |
| 14 | + |
| 15 | +inteval = 1 |
| 16 | +duration = 0.01 |
| 17 | + |
| 18 | + |
| 19 | +# mouse callback function |
| 20 | +def draw_boundingbox(event, x, y, flags, param): |
| 21 | + global selectingObject, initTracking, onTracking, ix, iy, cx,cy, w, h |
| 22 | + |
| 23 | + if event == cv2.EVENT_LBUTTONDOWN: |
| 24 | + selectingObject = True |
| 25 | + onTracking = False |
| 26 | + ix, iy = x, y |
| 27 | + cx, cy = x, y |
| 28 | + |
| 29 | + elif event == cv2.EVENT_MOUSEMOVE: |
| 30 | + cx, cy = x, y |
| 31 | + |
| 32 | + elif event == cv2.EVENT_LBUTTONUP: |
| 33 | + selectingObject = False |
| 34 | + if(abs(x-ix)>10 and abs(y-iy)>10): |
| 35 | + w, h = abs(x - ix), abs(y - iy) |
| 36 | + ix, iy = min(x, ix), min(y, iy) |
| 37 | + initTracking = True |
| 38 | + else: |
| 39 | + onTracking = False |
| 40 | + |
| 41 | + elif event == cv2.EVENT_RBUTTONDOWN: |
| 42 | + onTracking = False |
| 43 | + if(w>0): |
| 44 | + ix, iy = x-w/2, y-h/2 |
| 45 | + initTracking = True |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + |
| 51 | + if(len(sys.argv)==1): |
| 52 | + cap = cv2.VideoCapture(0) |
| 53 | + elif(len(sys.argv)==2): |
| 54 | + if(sys.argv[1].isdigit()): # True if sys.argv[1] is str of a nonnegative integer |
| 55 | + cap = cv2.VideoCapture(int(sys.argv[1])) |
| 56 | + else: |
| 57 | + cap = cv2.VideoCapture(sys.argv[1]) |
| 58 | + inteval = 30 |
| 59 | + else: assert(0), "too many arguments" |
| 60 | + |
| 61 | + tracker = KCF.kcftracker(False, True, False, False) # hog, fixed_window, multiscale, lab |
| 62 | + |
| 63 | + cv2.namedWindow('tracking') |
| 64 | + cv2.setMouseCallback('tracking',draw_boundingbox) |
| 65 | + |
| 66 | + while(cap.isOpened()): |
| 67 | + ret, frame = cap.read() |
| 68 | + if not ret: |
| 69 | + break |
| 70 | + |
| 71 | + if(selectingObject): |
| 72 | + cv2.rectangle(frame,(ix,iy), (cx,cy), (0,255,255), 1) |
| 73 | + elif(initTracking): |
| 74 | + cv2.rectangle(frame,(ix,iy), (ix+w,iy+h), (0,255,255), 2) |
| 75 | + |
| 76 | + tracker.init([ix,iy,w,h], frame) |
| 77 | + |
| 78 | + initTracking = False |
| 79 | + onTracking = True |
| 80 | + elif(onTracking): |
| 81 | + t0 = time() |
| 82 | + boundingbox = tracker.update(frame) #frame had better be contiguous |
| 83 | + t1 = time() |
| 84 | + |
| 85 | + boundingbox = map(int, boundingbox) |
| 86 | + cv2.rectangle(frame,(boundingbox[0],boundingbox[1]), (boundingbox[0]+boundingbox[2],boundingbox[1]+boundingbox[3]), (0,255,255), 1) |
| 87 | + |
| 88 | + duration = 0.8*duration + 0.2*(t1-t0) |
| 89 | + #duration = t1-t0 |
| 90 | + cv2.putText(frame, 'FPS: '+str(1/duration)[:4].strip('.'), (8,20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255), 2) |
| 91 | + |
| 92 | + cv2.imshow('tracking', frame) |
| 93 | + c = cv2.waitKey(inteval) & 0xFF |
| 94 | + if c==27 or c==ord('q'): |
| 95 | + break |
| 96 | + |
| 97 | + cap.release() |
| 98 | + cv2.destroyAllWindows() |
0 commit comments