예제 #1
0
def trackCamera(conf):
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print 'Error: the video capture do not work.'
        return False

    ret, frame = cap.read()
    if ret is True:
        scaleW, scaleH = float(conf.frameWidth) / frame.shape[1], float(
            conf.frameHeight) / frame.shape[0]
        initBB = Rect(int(conf.frameWidth / 2 - kLiveBoxWidth / 2),
                      int(conf.frameHeight / 2 - kLiveBoxHeight / 2),
                      int(kLiveBoxWidth), int(kLiveBoxHeight))
        print 'Press "i" to initialize tracker'
    else:
        print 'Error: video capture invalid.'
        return False

    if not conf.quietMode:
        cv2.namedWindow('Result')

    tracker = Tracker(conf)
    startFrame, endFrame, paused, doInitialize, count = 0, MAXINT, True, False, 0
    result = np.zeros((conf.frameHeight, conf.frameWidth, 3), np.uint8)
    random.seed(conf.seed)

    for frameid in xrange(startFrame, endFrame + 1):
        ret, frameOrig = cap.read()
        if ret is True:
            frame = cv2.resize(frameOrig, (conf.frameWidth, conf.frameHeight))
            frame = cv2.flip(frame, 1)
            result = frame[:]
            if doInitialize:
                if tracker.IsInitialized():
                    tracker.Reset()
                else:
                    tracker.Initialize(frame, initBB)
                doInitialize = False
            else:
                if not tracker.IsInitialized():
                    rectangle(result, initBB, WHITE)
        else:
            print 'Error: video capture invalid.'
            return False

        if tracker.IsInitialized():
            tracker.Track(frame)
            # if !conf.quietMode and conf.debugMode:
            #     tracker.Debug()
            box = tracker.GetBox()
            rectangle(result, box, GREEN)

        # print "hello image"
        if not conf.quietMode:
            # display menu
            if frameid == startFrame:
                print("#########################################")
                print("MENU")
                print("Press \"i\" to start tracking in camera mode")
                print("Press \"q\" or esc to escape")
                print("Press \"p\" to keep tracking")
                print("Press any other key to track 1 frame")
                print("#########################################")

            # print 'show image'
            cv2.imshow("result", result)
            if paused:
                key = cv2.waitKey() & 0xFF
            else:
                key = cv2.waitKey(1) & 0xFF
            if key != -1:
                if key == 27 or key == 81 or key == 113:  # esc q
                    break
                elif key == 80 or key == 112:  # p
                    paused = not paused
                elif key == 73 or key == 105:  # i
                    doInitialize = True
                    print 'doInitialize'
            if frameid == endFrame:
                print 'End of sequence, press any key to exit.'
                cv2.waitKey()

    cap.release()
    cv2.destroyAllWindows()
    return True