diff options
Diffstat (limited to 'megapixels/app/utils/draw_utils.py')
| -rw-r--r-- | megapixels/app/utils/draw_utils.py | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/megapixels/app/utils/draw_utils.py b/megapixels/app/utils/draw_utils.py index 3a389e68..3378e3e8 100644 --- a/megapixels/app/utils/draw_utils.py +++ b/megapixels/app/utils/draw_utils.py @@ -4,6 +4,9 @@ from math import sqrt import numpy as np import cv2 as cv +from app.utils import logger_utils + +log = logger_utils.Logger.getLogger() end_list = np.array([17, 22, 27, 42, 48, 31, 36, 68], dtype=np.int32) - 1 @@ -105,46 +108,61 @@ def plot_pose_box(im, Ps, pts68s, color=(40, 255, 0), line_width=2): pose_types = {'pitch': (0,0,255), 'roll': (255,0,0), 'yaw': (0,255,0)} -def draw_landmarks2D(im, points, radius=3, color=(0,255,0), stroke_weight=2): +def draw_landmarks2D(im, points_norm, radius=3, color=(0,255,0)): '''Draws facial landmarks, either 5pt or 68pt ''' - for x,y in points: - cv.circle(im, (x,y), radius, color, -1, cv.LINE_AA) - + im_dst = im.copy() + dim = im.shape[:2][::-1] + for x,y in points_norm: + pt = (int(x*dim[0]), int(y*dim[1])) + cv.circle(im_dst, pt, radius, color, -1, cv.LINE_AA) + return im_dst -def draw_landmarks3D(im, points, radius=3, color=(0,255,0), stroke_weight=2): +def draw_landmarks3D(im, points, radius=3, color=(0,255,0)): '''Draws 3D facial landmarks ''' + im_dst = im.copy() for x,y,z in points: - cv.circle(im, (x,y), radius, color, -1, cv.LINE_AA) - + cv.circle(im_dst, (x,y), radius, color, -1, cv.LINE_AA) + return im_dst -def draw_bbox(im, bbox, color=(0,255,0), stroke_weight=2): - '''Draws a dimensioned (not-normalized) BBox onto cv image +def draw_bbox(im, bbox_norm, color=(0,255,0), stroke_weight=2): + '''Draws BBox onto cv image ''' - cv.rectangle(im, bbox.pt_tl, bbox.pt_br, color, stroke_weight) - + im_dst = im.copy() + bbox_dim = bbox_norm.to_dim(im.shape[:2][::-1]) + cv.rectangle(im_dst, bbox_dim.pt_tl, bbox_dim.pt_br, color, stroke_weight) + return im_dst def draw_pose(im, pt_nose, image_pts): '''Draws 3-axis pose over image + TODO: normalize point data ''' - cv.line(im, pt_nose, tuple(image_pts['pitch'].ravel()), pose_types['pitch'], 3) - cv.line(im, pt_nose, tuple(image_pts['yaw'].ravel()), pose_types['yaw'], 3) - cv.line(im, pt_nose, tuple(image_pts['roll'].ravel()), pose_types['roll'], 3) - + im_dst = im.copy() + log.debug(f'pt_nose: {pt_nose}') + log.debug(f'image_pts pitch: {image_pts["pitch"]}') + cv.line(im_dst, pt_nose, tuple(image_pts['pitch']), pose_types['pitch'], 3) + cv.line(im_dst, pt_nose, tuple(image_pts['yaw']), pose_types['yaw'], 3) + cv.line(im_dst, pt_nose, tuple(image_pts['roll']), pose_types['roll'], 3) + return im_dst -def draw_text(im, pt, text, color=(0,255,0)): +def draw_text(im, pt_norm, text, color=(0,255,0)): '''Draws degrees as text over image ''' - cv.putText(im, text, pt, cv.FONT_HERSHEY_SIMPLEX, 0.75, color, thickness=1, lineType=cv.LINE_AA) - + im_dst = im.copy() + dim = im.shape[:2][::-1] + pt = tuple(map(int, (pt_norm[0]*dim[0], pt_norm[1]*dim[1]))) + cv.putText(im_dst, text, pt, cv.FONT_HERSHEY_SIMPLEX, 0.75, color, thickness=1, lineType=cv.LINE_AA) + return im_dst def draw_degrees(im, pose_data, color=(0,255,0)): '''Draws degrees as text over image ''' + im_dst = im.copy() for i, pose_type in enumerate(pose_types.items()): k, clr = pose_type v = pose_data[k] t = '{}: {:.2f}'.format(k, v) origin = (10, 30 + (25 * i)) - cv.putText(im, t, origin, cv.FONT_HERSHEY_SIMPLEX, 0.5, clr, thickness=2, lineType=2) + cv.putText(im_dst, t, origin, cv.FONT_HERSHEY_SIMPLEX, 0.5, clr, thickness=2, lineType=2) + return im_dst
\ No newline at end of file |
