This function draws bounding boxes and landmarks on the image and return the result. Parameters: bboxes - bboxes of shape [n, 5]. 'n' for number of bboxes, '5' for coordinate and confidence (x1, y1, x2, y2, c). landmarks - landmarks of shape [n, 5, 2]
(img: np.ndarray, bboxes: np.ndarray, landmarks: np.ndarray, scores: np.ndarray)
| 4 | from typing import List, Tuple |
| 5 | |
| 6 | def draw(img: np.ndarray, bboxes: np.ndarray, landmarks: np.ndarray, scores: np.ndarray) -> np.ndarray: |
| 7 | ''' |
| 8 | This function draws bounding boxes and landmarks on the image and return the result. |
| 9 | |
| 10 | Parameters: |
| 11 | bboxes - bboxes of shape [n, 5]. 'n' for number of bboxes, '5' for coordinate and confidence |
| 12 | (x1, y1, x2, y2, c). |
| 13 | landmarks - landmarks of shape [n, 5, 2]. 'n' for number of bboxes, '5' for 5 landmarks |
| 14 | (two for eyes center, one for nose tip, two for mouth corners), '2' for coordinate |
| 15 | on the image. |
| 16 | Returns: |
| 17 | img - image with bounding boxes and landmarks drawn |
| 18 | ''' |
| 19 | |
| 20 | # draw bounding boxes |
| 21 | if bboxes is not None: |
| 22 | color = (0, 255, 0) |
| 23 | thickness = 2 |
| 24 | for idx in range(bboxes.shape[0]): |
| 25 | bbox = bboxes[idx].astype(np.int) |
| 26 | cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), color, thickness) |
| 27 | cv2.putText(img, '{:.4f}'.format(scores[idx]), (bbox[0], bbox[1]+12), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255)) |
| 28 | |
| 29 | # draw landmarks |
| 30 | if landmarks is not None: |
| 31 | radius = 2 |
| 32 | thickness = 2 |
| 33 | color = [ |
| 34 | (255, 0, 0), # right eye |
| 35 | ( 0, 0, 255), # left eye |
| 36 | ( 0, 255, 0), # nose tip |
| 37 | (255, 0, 255), # mouth right |
| 38 | ( 0, 255, 255) # mouth left |
| 39 | ] |
| 40 | for idx in range(landmarks.shape[0]): |
| 41 | face_landmarks = landmarks[idx].astype(np.int) |
| 42 | for idx, landmark in enumerate(face_landmarks): |
| 43 | cv2.circle(img, (int(landmark[0]), int(landmark[1])), radius, color[idx], thickness) |
| 44 | return img |