예제 #1
0
def test_fall_detection_case_8():
    """Expect to not detect a fall"""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    # No person in a frame
    img_1 = _get_image(file_name="fall_img_6.png")

    # The frame represents a person who is in a standing position.
    img_2 = _get_image(file_name="fall_img_7.png")

    fall_detector.receive_next_sample(image=img_1)
    # set min time to a small number to speed up testing
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert not result
예제 #2
0
def test_fall_detection_case_1():
    """Expected to not detect a fall as key-points are not detected."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)
    fall_detector.connect_to_next_element(output)

    # The frame represents a person who is in a standing position.
    img_1 = _get_image(file_name="fall_img_1.png")

    # The frame represents a person completely falls.
    img_2 = _get_image(file_name="fall_img_3.png")

    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert not result
예제 #3
0
def test_fall_detection_case_2_1():
    """Expected to not detect a fall even though key-points are detected
    and the angle criteria is met. However the time distance between
    frames is too short."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    # The frame represents a person who is in a standing position.
    img_1 = _get_image(file_name="fall_img_1.png")

    # The frame represents a person falls.
    img_2 = _get_image(file_name="fall_img_2.png")

    start_time = time.monotonic()
    fall_detector.receive_next_sample(image=img_1)
    end_time = time.monotonic()
    safe_min = end_time - start_time + 1
    # set min time to a sufficiently big number to ensure test passes
    # on slow environments
    # the goal is to simulate two frames that are too close in time
    # to be considered for a fall detection sequence
    fall_detector.min_time_between_frames = safe_min
    fall_detector.receive_next_sample(image=img_2)

    assert not result
예제 #4
0
def test_fall_detection_case_5():
    """Expected to not detect a fall even the angle criteria is met
    because image 2 is standing up rather than fall"""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    # The frame represents a person falls.
    img_1 = _get_image(file_name="fall_img_2.png")

    # The frame represents a person who is in a standing position.
    img_2 = _get_image(file_name="fall_img_1.png")

    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert not result
예제 #5
0
def test_bad_sample_good_sample():
    """One bad sample should not prevent good samples from being processed."""
    config = _fall_detect_config()
    result = 'nothing passed to me'

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    object_detector = ObjectDetector(**config)
    output = _OutPipeElement(sample_callback=sample_callback)
    object_detector.connect_to_next_element(output)
    # bad sample
    object_detector.receive_next_sample(image=None)
    assert result == 'nothing passed to me'

    # good sample
    fall_detector = FallDetector(**config)
    fall_detector.connect_to_next_element(output)

    img_1 = _get_image(file_name='fall_img_1.png')
    img_2 = _get_image(file_name='fall_img_2.png')
    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert result
    assert len(result) == 1
    category, confidence, box, angle = result[0]
    assert box  # Add this line to avoid 'Unused local variable'
    assert category == 'FALL'
    assert confidence > 0.7
    assert angle > 60
예제 #6
0
def test_fall_detection_case_3():
    """Expect to detect a fall as key-points are detected by rotating the image."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    img_1 = _get_image(file_name='fall_img_11.png')
    img_2 = _get_image(file_name='fall_img_12.png')
    fall_detector.receive_next_sample(image=img_1)
    # set min time to a small number to speed up testing
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert result
    assert len(result) == 1
    category, confidence, box, angle = result[0]
    assert box  # Add this line to avoid 'Unused local variable'
    assert category == 'FALL'
    assert confidence > 0.3
    assert angle > 60
예제 #7
0
def test_fall_detection_case_2_2():
    """Expected to detect a fall because key-points are detected, the angle criteria is met and the time distance between frames is not too short."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    img_1 = _get_image(file_name='fall_img_1.png')
    img_2 = _get_image(file_name='fall_img_2.png')
    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert result
    assert len(result) == 1
    category, confidence, box, angle = result[0]
    assert box  # Add this line to avoid 'Unused local variable'
    assert category == 'FALL'
    assert confidence > 0.7
    assert angle > 60
예제 #8
0
def test_fall_detection_2_frame_back_case_1():
    """
    Expected to detect a fall using frame[t] and frame[t-1].
    frame[t-2] : A person is in standing position.
    frame[t-1] : A person is almost in standing position as he is walking.
    frame[t]   : A person is fall down.
    """

    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)
    fall_detector.connect_to_next_element(output)

    # A frame at t-2 timestamp when person is in standing position.
    img_1 = _get_image(file_name="fall_img_1.png")

    # A frame at t-1 timestamp when person is almost in standing position \
    # as he is walking.
    img_2 = _get_image(file_name="fall_img_1_1.png")

    # A frame at t timestamp when person falls down.
    img_3 = _get_image(file_name="fall_img_2.png")

    fall_detector.min_time_between_frames = 0.01

    fall_detector.receive_next_sample(image=img_1)
    time.sleep(fall_detector.min_time_between_frames)

    fall_detector.receive_next_sample(image=img_2)
    time.sleep(fall_detector.min_time_between_frames)

    assert not result

    fall_detector.receive_next_sample(image=img_3)

    assert result
    assert len(result) == 1

    category = result[0]["label"]
    confidence = result[0]["confidence"]
    angle = result[0]["leaning_angle"]
    keypoint_corr = result[0]["keypoint_corr"]

    assert keypoint_corr
    assert category == "FALL"
    assert confidence > 0.7
    assert angle > 60
def test_fall_detection_2_frame_back_case_2():
    """
        Expected to detect a fall using frame[t] and frame[t-2].
        frame[t-2] : A person is in standing position.
        frame[t-1] : A person is mid-way of fall.
        frame[t]   : A person is fall down.
    """
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)
    fall_detector.connect_to_next_element(output)

    # A frame at t-2 timestamp when person is in standing position.
    img_1 = _get_image(file_name='fall_img_1.png')

    # A frame at t-1 timestamp when person is mid-way of fall.
    img_2 = _get_image(file_name='fall_img_2_2.png')

    # A frame at t timestamp when person falls down.
    img_3 = _get_image(file_name='fall_img_2.png')

    fall_detector.min_time_between_frames = 0.01
    fall_detector.max_time_between_frames = 15

    fall_detector.receive_next_sample(image=img_1)
    time.sleep(fall_detector.min_time_between_frames)

    fall_detector.receive_next_sample(image=img_2)
    time.sleep(fall_detector.min_time_between_frames)

    assert not result

    fall_detector.receive_next_sample(image=img_3)

    assert result
    assert len(result) == 1

    category = result[0]['label']
    confidence = result[0]['confidence']
    angle = result[0]['leaning_angle']
    keypoint_corr = result[0]['keypoint_corr']

    assert keypoint_corr
    assert category == 'FALL'
    assert confidence > 0.7
    assert angle > 60
예제 #10
0
def test_bad_sample_good_sample():
    """One bad sample should not prevent good samples from being processed."""
    config = _fall_detect_config()
    result = "nothing passed to me"

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    object_detector = ObjectDetector(**config)
    output = _OutPipeElement(sample_callback=sample_callback)
    object_detector.connect_to_next_element(output)
    # bad sample
    object_detector.receive_next_sample(image=None)
    assert result == "nothing passed to me"

    # good sample
    fall_detector = FallDetector(**config)
    fall_detector.connect_to_next_element(output)

    # The frame represents a person who is in a standing position.
    img_1 = _get_image(file_name="fall_img_1.png")

    # The frame represents a person falls.
    img_2 = _get_image(file_name="fall_img_2.png")

    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert result
    assert len(result) == 1

    category = result[0]["label"]
    confidence = result[0]["confidence"]
    angle = result[0]["leaning_angle"]
    keypoint_corr = result[0]["keypoint_corr"]

    assert keypoint_corr
    assert category == "FALL"
    assert confidence > 0.7
    assert angle > 60
예제 #11
0
def test_fall_detection_2_frame_back_case_3():
    """
    Expected to not detect a fall using frame[t],frame[t-1] and frame[t-2].
    frame[t-2] : A person is in walking postion.
    frame[t-1] : A person is in walking postion.
    frame[t]   : A person is slight in lean postion but no fall.
    """

    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)
    fall_detector.connect_to_next_element(output)

    # A frame at t-2 timestamp when person is in walking postion.
    img_1 = _get_image(file_name="fall_img_15.png")

    # A frame at t-1 timestamp when person is in walking postion.
    img_2 = _get_image(file_name="fall_img_16.png")

    # A frame at t timestamp when person is slight in lean postion but no fall.
    img_3 = _get_image(file_name="fall_img_17.png")

    fall_detector.min_time_between_frames = 0.01

    fall_detector.receive_next_sample(image=img_1)
    time.sleep(fall_detector.min_time_between_frames)

    fall_detector.receive_next_sample(image=img_2)
    time.sleep(fall_detector.min_time_between_frames)

    assert not result

    fall_detector.receive_next_sample(image=img_3)

    assert not result
예제 #12
0
def test_fall_detection_case_3_2():
    """Expect to detect a fall as key-points are detected
    by rotating the image counter clockwise."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    # The frame represents a person who is in a standing position.
    img_1 = _get_image(file_name="fall_img_11_flip.png")

    # The frame represents a person completely falls.
    img_2 = _get_image(file_name="fall_img_12_flip.png")

    fall_detector.receive_next_sample(image=img_1)
    # set min time to a small number to speed up testing
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert result
    assert len(result) == 1

    category = result[0]["label"]
    confidence = result[0]["confidence"]
    angle = result[0]["leaning_angle"]
    keypoint_corr = result[0]["keypoint_corr"]

    assert keypoint_corr
    assert category == "FALL"
    assert confidence > 0.3
    assert angle > 60
예제 #13
0
def test_background_image():
    """Expect to not detect anything interesting in a background image."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None,
                        thumbnail=None,
                        inference_result=None,
                        **kwargs):
        nonlocal result
        result = image is not None and thumbnail is not None and not inference_result

    fall_detector = FallDetector(**config)
    output = _OutPipeElement(sample_callback=sample_callback)
    fall_detector.connect_to_next_element(output)
    img = _get_image(file_name="background.jpg")
    fall_detector.receive_next_sample(image=img)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    img = _get_image(file_name="background.jpg")
    fall_detector.receive_next_sample(image=img)
    assert result is True
예제 #14
0
def test_fall_detection_case_2_2():
    """Expected to detect a fall because key-points are detected,
    the angle criteria is met and the time distance between
    frames is not too short."""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    # The frame represents a person who is in a standing position.
    img_1 = _get_image(file_name="fall_img_1.png")

    # The frame represents a person falls.
    img_2 = _get_image(file_name="fall_img_2.png")

    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert result
    assert len(result) == 1
    category = result[0]["label"]
    confidence = result[0]["confidence"]
    angle = result[0]["leaning_angle"]
    keypoint_corr = result[0]["keypoint_corr"]

    assert keypoint_corr
    assert category == "FALL"
    assert confidence > 0.7
    assert angle > 60
예제 #15
0
def test_fall_detection_case_4():
    """No Fall"""
    config = _fall_detect_config()
    result = None

    def sample_callback(image=None, inference_result=None, **kwargs):
        nonlocal result
        result = inference_result

    fall_detector = FallDetector(**config)

    output = _OutPipeElement(sample_callback=sample_callback)

    fall_detector.connect_to_next_element(output)

    img_1 = _get_image(file_name='fall_img_1.png')
    img_2 = _get_image(file_name='fall_img_4.png')
    fall_detector.receive_next_sample(image=img_1)
    fall_detector.min_time_between_frames = 0.01
    time.sleep(fall_detector.min_time_between_frames)
    fall_detector.receive_next_sample(image=img_2)

    assert not result