Skip to content

Commit

Permalink
fix: dall detection debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelin committed Feb 10, 2021
1 parent d497374 commit f06f218
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ _mdwriter.cson
ambianic-debug2.sh
config.yaml.local
/.ropeproject
tmp*.jpg
49 changes: 23 additions & 26 deletions src/ambianic/pipeline/ai/fall_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@ def find_keypoints(self, image):
rotations = [Image.ROTATE_270, Image.ROTATE_90]
angle = 0
pose = None
poses, thumbnail = self._pose_engine.detect_poses(image)
poses, thumbnail, _ = self._pose_engine.detect_poses(image)
width, height = thumbnail.size
# if no pose detected with high confidence,
# try rotating the image +/- 90' to find a fallen person
# currently only looking at pose[0] because we are focused on a lone person falls
#while (not poses or poses[0].score < min_score) and rotations:
pose_score, pose_dix = self.estimate_spinalVector_score(poses[0])
while pose_score < min_score and rotations:
spinal_vector_score, pose_dix = self.estimate_spinal_vector_score(poses[0])
while spinal_vector_score < min_score and rotations:
angle = rotations.pop()
transposed = image.transpose(angle)
# we are interested in the poses but not the rotated thumbnail
poses, _ = self._pose_engine.detect_poses(transposed)
pose_score, pose_dix = self.estimate_spinalVector_score(poses[0])
poses, _, _ = self._pose_engine.detect_poses(transposed)
spinal_vector_score, pose_dix = self.estimate_spinal_vector_score(poses[0])

if poses and poses[0]:
pose = poses[0]

# lets check if we found a good pose candidate

if (pose and pose_score >= min_score):
if (pose and spinal_vector_score >= min_score):
# if the image was rotated, we need to rotate back to the original image coordinates
# before comparing with poses in other frames.
if angle == Image.ROTATE_90:
Expand All @@ -181,15 +181,15 @@ def find_keypoints(self, image):
tmp_swap = keypoint.yx[0]
keypoint.yx[0] = keypoint.yx[1]
keypoint.yx[1] = height-tmp_swap
else:
# we could not detect a pose with sufficient confidence
log.debug(f"No pose detected with sufficient confidence. Score {pose_score} < {min_score} minimum threshold")
log.info(f"""A pose detected with spinal_vector_score={spinal_vector_score} >= {min_score} confidence threshold.
Pose keypoints: {pose_dix}"
"""
)
else:
pose = None

if pose_dix:
log.debug(f"Pose detected with confidence {pose_score} and keypoints: {pose_dix}")

return pose, thumbnail, pose_score, pose_dix
return pose, thumbnail, spinal_vector_score, pose_dix

def find_changes_in_angle(self, pose_dix, inx):
'''
Expand Down Expand Up @@ -257,11 +257,9 @@ def draw_lines(self, thumbnail, pose_dix, score):
body_lines_drawn += 1

# save a thumbnail for debugging
if body_lines_drawn > 0 and log.getEffectiveLevel() <= logging.DEBUG: # development mode
# DEBUG: save template_image for debugging
timestr = int(time.monotonic()*1000)
path = f'tmp-fall-detect-thumbnail-{timestr}-score-{score}.jpg'
thumbnail.save(path, format='JPEG')
timestr = int(time.monotonic()*1000)
path = f'tmp-fall-detect-thumbnail-{timestr}-score-{score}.jpg'
thumbnail.save(path, format='JPEG')

return body_lines_drawn

Expand All @@ -284,7 +282,7 @@ def get_line_angles_with_yaxis(self, pose_dix):

return (l_angle, r_angle)

def estimate_spinalVector_score(self, pose):
def estimate_spinal_vector_score(self, pose):
pose_dix = {}
is_leftVector = is_rightVector = False

Expand Down Expand Up @@ -326,9 +324,8 @@ def find_spinalLine():
else:
spinalVectorScore = 0

pose_score = spinalVectorScore

return pose_score, pose_dix
log.debug(f"Estimated spinal vector score: {spinalVectorScore}")
return spinalVectorScore, pose_dix

def fall_detect(self, image=None):
assert image
Expand All @@ -345,21 +342,22 @@ def fall_detect(self, image=None):
thumbnail = self._prev_data[-1][self.THUMBNAIL]
else:
# Detection using tensorflow posenet module
pose, thumbnail, pose_score, pose_dix = self.find_keypoints(image)
pose, thumbnail, spinal_vector_score, pose_dix = self.find_keypoints(image)

inference_result = None
if not pose:
log.debug("No pose detected or detection score does not meet confidence threshold.")
log.debug(f"No pose detected or detection score does not meet confidence threshold of {self.confidence_threshold}.")
else:
inference_result = []

current_body_vector_score = pose_score
current_body_vector_score = spinal_vector_score

# Find line angle with vertcal axis
left_angle_with_yaxis, rigth_angle_with_yaxis = self.get_line_angles_with_yaxis(pose_dix)

# save an image with drawn lines for debugging
self.draw_lines(thumbnail, pose_dix, pose_score)
if log.getEffectiveLevel() <= logging.DEBUG: # development mode
self.draw_lines(thumbnail, pose_dix, spinal_vector_score)

for t in [-1, -2]:
lapse = now - self._prev_data[t][self.TIMESTAMP]
Expand All @@ -383,7 +381,6 @@ def fall_detect(self, image=None):
box = [0, 0, 1, 1]
inference_result.append(('FALL', fall_score, box, leaning_angle))
log.info("Fall detected: %r", inference_result)

break
else:
log.debug(f"No fall detected due to low confidence score: {fall_score} < {self.confidence_threshold} min threshold. Inference result: {inference_result}")
Expand Down
7 changes: 4 additions & 3 deletions src/ambianic/pipeline/ai/pose_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ def detect_poses(self, img):
keypoint_dict[KEYPOINTS[point_i]] = keypoint

# overall pose score is calculated as the average of all individual keypoint scores
pose_scores = cnt/keypoint_count
poses.append(Pose(keypoint_dict, pose_scores))
pose_score = cnt/keypoint_count
log.debug(f"Overall pose score (keypoint score average): {pose_score}")
poses.append(Pose(keypoint_dict, pose_score))
if cnt > 0 and log.getEffectiveLevel() <= logging.DEBUG: # development mode
# save template_image for debugging
timestr = int(time.monotonic()*1000)
log.debug(f"Detected a pose with {cnt} keypoints that score over the minimum confidence threshold of {self.confidence_threshold}.")
debug_image_file_name = f'tmp-pose-detect-image-time-{timestr}-keypoints-{cnt}.jpg'
template_image.save(debug_image_file_name, format='JPEG')
log.debug(f"Debug image saved: {debug_image_file_name}")
return poses, thumbnail
return poses, thumbnail, pose_score

0 comments on commit f06f218

Please sign in to comment.