def test_one_person_no_face(): """Expect to detect one person.""" config = _object_detect_config() result = None 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) img = _get_image(file_name="person-no-face.jpg") object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category = result[0]["label"] confidence = result[0]["confidence"] (x0, y0) = result[0]["box"]["xmin"], result[0]["box"]["ymin"] (x1, y1) = result[0]["box"]["xmax"], result[0]["box"]["ymax"] assert category == "person" assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_one_label_filter(): """Expect to detect one person and no other objects.""" config = _object_detect_config() confidence_threshold = 0.7 config["confidence_threshold"] = confidence_threshold config["label_filter"] = ["person"] result = None 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) img = _get_image(file_name="person-couch.jpg") object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category = result[0]["label"] confidence = result[0]["confidence"] (x0, y0) = result[0]["box"]["xmin"], result[0]["box"]["ymin"] (x1, y1) = result[0]["box"]["xmax"], result[0]["box"]["ymax"] assert category == "person" assert confidence > confidence_threshold assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_thermal_one_person_miss_face_two_stage_pipe(): """Expect to detect a person but not a face.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None, **kwargs): nonlocal result result = inference_result # test stage one, obect detection -> out object_detector = ObjectDetector(**object_config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) img = _get_image(file_name="person_thermal_bw.jpg") object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 label = result[0]["label"] confidence = result[0]["confidence"] (x0, y0) = result[0]["box"]["xmin"], result[0]["box"]["ymin"] (x1, y1) = result[0]["box"]["xmax"], result[0]["box"]["ymax"] assert label == "person" assert confidence > 0.8 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 # test stage 2, rearrange pipe elements: object->face->out face_detector = FaceDetector(**face_config) object_detector.connect_to_next_element(face_detector) face_detector.connect_to_next_element(output) object_detector.receive_next_sample(image=img) assert not result
def test_no_labels_filter(): """Expect to detect all labeled objects - one person and one couch.""" config = _object_detect_config() config['confidence_threshold'] = 0.6 # No label_filter set, which is the same as None # config['label_filter'] = None result = None 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) img = _get_image(file_name='person-couch.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 2 category, confidence, (x0, y0, x1, y1) = result[0] assert category == 'person' assert confidence > 0.7 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 category, confidence, (x0, y0, x1, y1) = result[1] assert category == 'couch' assert confidence > 0.6 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_one_person_high_confidence_face_low_confidence_two_stage_pipe(): """Expect to detect a person but not a face.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None): nonlocal result result = inference_result # test stage one, obect detection -> out object_detector = ObjectDetector(element_config=object_config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) img = _get_image(file_name='person.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category, confidence, (x0, y0, x1, y1) = result[0] assert category == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 # test stage 2, rearrange pipe elements: object->face->out face_detector = FaceDetector(element_config=face_config) object_detector.connect_to_next_element(face_detector) face_detector.connect_to_next_element(output) object_detector.receive_next_sample(image=img) assert not result
def test_one_person_two_stage_pipe_high_face_confidence(): """Detect a person in 1st stage and a face in 2nd stage.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None, **kwargs): nonlocal result result = inference_result object_detector = ObjectDetector(**object_config) face_detector = FaceDetector(**face_config) object_detector.connect_to_next_element(face_detector) output = _OutPipeElement(sample_callback=sample_callback) face_detector.connect_to_next_element(output) img = _get_image(file_name='person-face.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 label = result[0]['label'] confidence = result[0]['confidence'] (x0, y0) = result[0]['box']['xmin'], result[0]['box']['ymin'] (x1, y1) = result[0]['box']['xmax'], result[0]['box']['ymax'] assert label == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
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
def test2_one_person_high_confidence_face_low_confidence_two_stage_pipe(): """Expect to detect a person but not a face.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None, **kwargs): nonlocal result result = inference_result # test stage one, obect detection -> out object_detector = ObjectDetector(**object_config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) img = _get_image(file_name='person-face2.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 label = result[0]['label'] confidence = result[0]['confidence'] (x0, y0) = result[0]['box']['xmin'], result[0]['box']['ymin'] (x1, y1) = result[0]['box']['xmax'], result[0]['box']['ymax'] assert label == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 # test stage 2, rearrange pipe elements: object->face->out face_detector = FaceDetector(**face_config) object_detector.connect_to_next_element(face_detector) face_detector.connect_to_next_element(output) object_detector.receive_next_sample(image=img) assert not result
def test_bad_sample_good_sample(): """One bad sample should not prevent good samples from being processed.""" config = _object_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 img = _get_image(file_name='person.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category, confidence, (x0, y0, x1, y1) = result[0] assert category == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_one_person(): """Expect to detect one person.""" config = _object_detect_config() result = None 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) img = _get_image(file_name='person.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category = result[0]['label'] confidence = result[0]['confidence'] (x0, y0) = result[0]['box']['xmin'], result[0]['box']['ymin'] (x1, y1) = result[0]['box']['xmax'], result[0]['box']['ymax'] assert category == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_no_sample(): """Expect element to pass empty sample to next element.""" config = _object_detect_config() result = 'Something' def sample_callback(image=None, inference_result=None): nonlocal result result = image is None and inference_result is None object_detector = ObjectDetector(element_config=config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) object_detector.receive_next_sample() assert result is True
def test_background_image(): """Expect to not detect anything interesting in a background image.""" config = _object_detect_config() result = None def sample_callback(image=None, inference_result=None): nonlocal result result = inference_result object_detector = ObjectDetector(element_config=config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) img = _get_image(file_name='background.jpg') object_detector.receive_next_sample(image=img) assert not result
def test_one_label_not_in_picture(): """Expect to detect nothing because there is no object with the given label in the picture.""" config = _object_detect_config() config['confidence_threshold'] = 0.6 config['label_filter'] = ['car'] result = None 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) img = _get_image(file_name='person-couch.jpg') object_detector.receive_next_sample(image=img) assert not result
def test_bad_label_filter(): """Expect to detect nothing because the label is not in the training label set.""" config = _object_detect_config() config['confidence_threshold'] = 0.6 config['label_filter'] = ['SomeR@ndomJunk'] result = None 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) img = _get_image(file_name='person-couch.jpg') object_detector.receive_next_sample(image=img) assert not result
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
def test_one_person_no_face_two_stage(): """Expect to detect one person.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None, **kwargs): nonlocal result result = inference_result object_detector = ObjectDetector(**object_config) face_detector = FaceDetector(**face_config) object_detector.connect_to_next_element(face_detector) output = _OutPipeElement(sample_callback=sample_callback) face_detector.connect_to_next_element(output) img = _get_image(file_name='person-no-face.jpg') object_detector.receive_next_sample(image=img) assert not result
def test_one_person_two_stage_pipe_low_person_confidence(): """Fail to detect person in 1st stage hence no face in 2nd stage.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None): nonlocal result result = inference_result object_detector = ObjectDetector(element_config=object_config) face_detector = FaceDetector(element_config=face_config) object_detector.connect_to_next_element(face_detector) output = _OutPipeElement(sample_callback=sample_callback) face_detector.connect_to_next_element(output) img = _get_image(file_name='person-face.jpg') object_detector.receive_next_sample(image=img) assert not result
def test_two_person_high_confidence_one_face_high_confidence_two_stage_pipe(): """Expect to detect two persons but only one face.""" object_config = _object_detect_config() face_config = _face_detect_config() result = None def sample_callback(image=None, inference_result=None, **kwargs): nonlocal result result = inference_result # test stage one, obect detection -> out object_detector = ObjectDetector(**object_config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) img = _get_image(file_name='person2-face1.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 2 label, confidence, (x0, y0, x1, y1) = result[0] assert label == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 label, confidence, (x0, y0, x1, y1) = result[1] assert label == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 # test stage 2, rearrange pipe elements: object->face->out face_detector = FaceDetector(**face_config) object_detector.connect_to_next_element(face_detector) face_detector.connect_to_next_element(output) object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 label, confidence, (x0, y0, x1, y1) = result[0] assert label == 'person' assert confidence > 0.6 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_one_person_no_face(): """Expect to detect one person.""" config = _object_detect_config() result = None def sample_callback(image=None, inference_result=None): nonlocal result result = inference_result object_detector = ObjectDetector(element_config=config) output = _OutPipeElement(sample_callback=sample_callback) object_detector.connect_to_next_element(output) img = _get_image(file_name='person-no-face.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category, confidence, (x0, y0, x1, y1) = result[0] assert category == 'person' assert confidence > 0.9 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_two_labels_filter(): """Expect to detect one person and one couch.""" config = _object_detect_config() config['confidence_threshold'] = 0.6 config['label_filter'] = ['person', 'couch'] result = None 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) img = _get_image(file_name='person-couch.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 2 category = result[0]['label'] confidence = result[0]['confidence'] (x0, y0) = result[0]['box']['xmin'], result[0]['box']['ymin'] (x1, y1) = result[0]['box']['xmax'], result[0]['box']['ymax'] assert category == 'person' assert confidence > 0.7 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1 category = result[1]['label'] confidence = result[1]['confidence'] (x0, y0) = result[1]['box']['xmin'], result[1]['box']['ymin'] (x1, y1) = result[1]['box']['xmax'], result[1]['box']['ymax'] assert category == 'couch' assert confidence > 0.6 assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1
def test_one_label_filter(): """Expect to detect one person and no other objects.""" config = _object_detect_config() confidence_threshold = 0.7 config['confidence_threshold'] = confidence_threshold config['label_filter'] = ['person'] result = None 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) img = _get_image(file_name='person-couch.jpg') object_detector.receive_next_sample(image=img) assert result assert len(result) == 1 category, confidence, (x0, y0, x1, y1) = result[0] assert category == 'person' assert confidence > confidence_threshold assert x0 > 0 and x0 < x1 assert y0 > 0 and y0 < y1