def processFrame(im):
    utils.preprocessing(im)
    # To see in the right way
    im = cv2.flip(im, 1)
    im_height, im_width = im.shape
    global TPL_RECT
    global TPL_CENTER
    global FILTER_INIT
    global FILTER_NUM
    global FILTER_DEN
    global COS_WINDOW
    global PSR_THRES

    psr_test = False
    if TPL_RECT != None:
        tplc = TPL_RECT.astype(np.int64)
        patch = im[tplc[0, 0] : tplc[1, 0], tplc[0, 1] : tplc[1, 1]]
        if not FILTER_INIT:
            initFilter(patch)
            print tplc.shape
        else:
            if patch.shape == COS_WINDOW.shape:
                height, width = patch.shape
                #                ptpl = patch * COS_WINDOW
                #                height, width = ptpl.shape
                #                output = utils.genGaussianMatrix(width, height, (width/2, height/2), 2.0)
                #                ftpl = np.fft.fft2(ptpl)
                #                foutput = np.fft.fft2(output)
                #                n = foutput #* np.conj(ftpl)
                #                d = ftpl #* np.conj(ftpl)
                G = np.conj(FILTER_NUM / FILTER_DEN) * np.conj(np.fft.fft2(patch * COS_WINDOW))
                g = np.real(np.fft.ifft2(G))
                utils.showImage("output", g)
                utils.showImage("filter", np.real(np.fft.fftshift(np.fft.ifft2(np.conj(FILTER_NUM / FILTER_DEN)))))
                psr = utils.computePSR(g)
                psr_test = psr > PSR_THRES
                if True:
                    peak_pos = np.argmax(g)
                    dy = peak_pos // width - height // 2
                    dx = peak_pos % width - width // 2
                    if tplc[0, 0] - dy < 0:
                        dy = tplc[0, 0]
                    if tplc[1, 0] - dy >= im_height:
                        dy = tplc[1, 0] - im_height
                    if tplc[0, 1] - dx < 0:
                        dx = tplc[0, 1]
                    if tplc[1, 1] - dx >= im_width:
                        dx = tplc[1, 1] - im_width
                    tplc[:, 0] -= dy
                    tplc[:, 1] -= dx
                    TPL_RECT[:, 0] -= dy
                    TPL_RECT[:, 1] -= dx
                    new_patch = im[tplc[0, 0] : tplc[1, 0], tplc[0, 1] : tplc[1, 1]]
                    updateFilter(new_patch)
                # print psr

        utils.drawRectangle(im, tplc, not psr_test)

    return im
def initFilter(tpl):
    global COS_WINDOW
    global FILTER_INIT
    global FILTER_STD
    global FILTER_NUM
    global FILTER_DEN

    height, width = tpl.shape
    ptpl = tpl * COS_WINDOW
    output = utils.genGaussianMatrix(width, height, (width / 2, height / 2), FILTER_STD)
    ftpl = np.fft.fft2(ptpl)
    foutput = np.fft.fft2(output)
    FILTER_NUM = foutput * np.conj(ftpl)
    FILTER_DEN = ftpl * np.conj(ftpl)
    FILTER_INIT = True

    psr = utils.computePSR(output)