)

while (True):
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    if (frame is None): break  #check for empty frames
    #Return the binary mask from the backprojection algorithm
    frame_mask = my_back_detector.returnMask(frame,
                                             morph_opening=True,
                                             blur=True,
                                             kernel_size=5,
                                             iterations=2)
    if (my_mask_analyser.returnNumberOfContours(frame_mask) > 0
            and ENABLE_CAPTURE == True):
        x_center, y_center = my_mask_analyser.returnMaxAreaCenter(frame_mask)
        x_rect, y_rect, w_rect, h_rect = my_mask_analyser.returnMaxAreaRectangle(
            frame_mask)
        area = w_rect * h_rect
        cv2.circle(frame, (x_center, y_center), 3, [0, 255, 0], 5)
        #Check the position of the target and press the keys
        #KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, KEY_SPACE
        #KEY_W, KEY_S, KEY_D, KEY_A
        #DOWN
        if (y_center > int(cam_h / 2) + offset and area > 10000):
            ui.write(e.EV_KEY, e.KEY_DOWN, 1)
            print("KEY_DOWN")
        #UP
        elif (y_center < int(cam_h / 2) - offset and area > 10000):
            ui.write(e.EV_KEY, e.KEY_UP, 1)
            print("KEY_UP")
        else:
            print("WAITING")
noise_probability = 0.15 #in range [0, 1.0]

while(True):

    # Capture frame-by-frame
    ret, frame = video_capture.read()
    if(frame is None): break #check for empty frames

    #Return the binary mask from the backprojection algorithm
    frame_mask = my_back_detector.returnMask(frame, morph_opening=True, blur=True, kernel_size=5, iterations=2)

    if(my_mask_analyser.returnNumberOfContours(frame_mask) > 0):
        #Use the binary mask to find the contour with largest area
        #and the center of this contour which is the point we
        #want to track with the particle filter
        x_rect,y_rect,w_rect,h_rect = my_mask_analyser.returnMaxAreaRectangle(frame_mask)
        x_center, y_center = my_mask_analyser.returnMaxAreaCenter(frame_mask)
        #Adding noise to the coords
        coin = np.random.uniform()
        if(coin >= 1.0-noise_probability): 
            x_noise = int(np.random.uniform(-300, 300))
            y_noise = int(np.random.uniform(-300, 300))
        else: 
            x_noise = 0
            y_noise = 0
        x_rect += x_noise
        y_rect += y_noise
        x_center += x_noise
        y_center += y_noise
        cv2.rectangle(frame, (x_rect,y_rect), (x_rect+w_rect,y_rect+h_rect), [255,0,0], 2) #BLUE rect
cv2.moveWindow('Video', 20, 20)
is_first_frame = True

while (True):

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    frame_mask = my_motion_detector.returnMask(frame)
    #Uncomment if you want more information about the frame with
    #with the largest area.
    #cx, cy = my_mask_analyser.returnMaxAreaCenter(frame_mask)
    #cnt = my_mask_analyser.returnMaxAreaContour(frame_mask)

    if (my_mask_analyser.returnNumberOfContours(frame_mask) > 0):
        x, y, w, h = my_mask_analyser.returnMaxAreaRectangle(frame_mask)
        cv2.rectangle(frame, (x, y), (x + w, y + h), [0, 255, 0], 2)

    #Writing in the output file
    out.write(frame)

    #Showing the frame and waiting
    # for the exit command
    if (frame is None): break  #check for empty frames
    cv2.imshow('Video', frame)  #show on window
    if cv2.waitKey(1) & 0xFF == ord('q'): break  #Exit when Q is pressed

#Release the camera
video_capture.release()
print("Bye...")
cv2.moveWindow('Video', 20, 20)
is_first_frame = True

while(True):

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    frame_mask = my_motion_detector.returnMask(frame)
    #Uncomment if you want more information about the frame with
    #with the largest area.
    #cx, cy = my_mask_analyser.returnMaxAreaCenter(frame_mask)
    #cnt = my_mask_analyser.returnMaxAreaContour(frame_mask)
    
    if(my_mask_analyser.returnNumberOfContours(frame_mask) > 0):
        x,y,w,h = my_mask_analyser.returnMaxAreaRectangle(frame_mask)
        cv2.rectangle(frame, (x,y), (x+w,y+h), [0,255,0], 2)

    #Writing in the output file
    out.write(frame)

    #Showing the frame and waiting
    # for the exit command
    if(frame is None): break #check for empty frames
    cv2.imshow('Video', frame) #show on window
    if cv2.waitKey(1) & 0xFF == ord('q'): break #Exit when Q is pressed

#Release the camera
video_capture.release()
print("Bye...")