From e30d064cbe9f00ed6c8239f2a7cb1ff9d29e9867 Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Mon, 2 Oct 2023 23:41:57 -0700 Subject: [PATCH] some updates to work with 2023 py/numpy/torch versions --- ptgaze/common/camera.py | 4 ++-- ptgaze/common/face_model.py | 7 ++----- ptgaze/common/face_model_68.py | 3 --- ptgaze/common/face_model_mediapipe.py | 3 --- ptgaze/common/visualizer.py | 6 +++--- .../head_pose_estimation/face_landmark_estimator.py | 12 ++++++------ ptgaze/head_pose_estimation/head_pose_normalizer.py | 2 +- .../models/mpiifacegaze/backbones/resnet_simple.py | 6 ++++-- 8 files changed, 18 insertions(+), 25 deletions(-) diff --git a/ptgaze/common/camera.py b/ptgaze/common/camera.py index 5e04e4a..817ea91 100644 --- a/ptgaze/common/camera.py +++ b/ptgaze/common/camera.py @@ -31,9 +31,9 @@ def project_points(self, tvec: Optional[np.ndarray] = None) -> np.ndarray: assert points3d.shape[1] == 3 if rvec is None: - rvec = np.zeros(3, dtype=np.float) + rvec = np.zeros(3, dtype=float) if tvec is None: - tvec = np.zeros(3, dtype=np.float) + tvec = np.zeros(3, dtype=float) points2d, _ = cv2.projectPoints(points3d, rvec, tvec, self.camera_matrix, self.dist_coefficients) diff --git a/ptgaze/common/face_model.py b/ptgaze/common/face_model.py index 5aca3f8..9591bdf 100644 --- a/ptgaze/common/face_model.py +++ b/ptgaze/common/face_model.py @@ -1,5 +1,3 @@ -import dataclasses - import cv2 import numpy as np from scipy.spatial.transform import Rotation @@ -8,7 +6,6 @@ from .face import Face -@dataclasses.dataclass(frozen=True) class FaceModel: LANDMARKS: np.ndarray REYE_INDICES: np.ndarray @@ -26,8 +23,8 @@ def estimate_head_pose(self, face: Face, camera: Camera) -> None: # The default values of rvec and tvec below mean that the # initial estimate of the head pose is not rotated and the # face is in front of the camera. - rvec = np.zeros(3, dtype=np.float) - tvec = np.array([0, 0, 1], dtype=np.float) + rvec = np.zeros(3, dtype=float) + tvec = np.array([0, 0, 1], dtype=float) _, rvec, tvec = cv2.solvePnP(self.LANDMARKS, face.landmarks, camera.camera_matrix, diff --git a/ptgaze/common/face_model_68.py b/ptgaze/common/face_model_68.py index a402e4b..422dc75 100644 --- a/ptgaze/common/face_model_68.py +++ b/ptgaze/common/face_model_68.py @@ -1,11 +1,8 @@ -import dataclasses - import numpy as np from .face_model import FaceModel -@dataclasses.dataclass(frozen=True) class FaceModel68(FaceModel): """3D face model for Multi-PIE 68 points mark-up. diff --git a/ptgaze/common/face_model_mediapipe.py b/ptgaze/common/face_model_mediapipe.py index 46b68bc..91787fb 100644 --- a/ptgaze/common/face_model_mediapipe.py +++ b/ptgaze/common/face_model_mediapipe.py @@ -1,11 +1,8 @@ -import dataclasses - import numpy as np from .face_model import FaceModel -@dataclasses.dataclass(frozen=True) class FaceModelMediaPipe(FaceModel): """3D face model for MediaPipe 468 points mark-up. diff --git a/ptgaze/common/visualizer.py b/ptgaze/common/visualizer.py index ca1a2d6..b56e993 100644 --- a/ptgaze/common/visualizer.py +++ b/ptgaze/common/visualizer.py @@ -25,12 +25,12 @@ def draw_bbox(self, lw: int = 1) -> None: assert self.image is not None assert bbox.shape == (2, 2) - bbox = np.round(bbox).astype(np.int).tolist() + bbox = np.round(bbox).astype(np.int64).tolist() cv2.rectangle(self.image, tuple(bbox[0]), tuple(bbox[1]), color, lw) @staticmethod def _convert_pt(point: np.ndarray) -> Tuple[int, int]: - return tuple(np.round(point).astype(np.int).tolist()) + return tuple(np.round(point).astype(np.int64).tolist()) def draw_points(self, points: np.ndarray, @@ -71,7 +71,7 @@ def draw_model_axes(self, face: Face, length: float, lw: int = 2) -> None: assert face.head_position is not None assert face.landmarks is not None # Get the axes of the model coordinate system - axes3d = np.eye(3, dtype=np.float) @ Rotation.from_euler( + axes3d = np.eye(3, dtype=float) @ Rotation.from_euler( 'XYZ', [0, np.pi, 0]).as_matrix() axes3d = axes3d * length axes2d = self._camera.project_points(axes3d, diff --git a/ptgaze/head_pose_estimation/face_landmark_estimator.py b/ptgaze/head_pose_estimation/face_landmark_estimator.py index 1653f6c..b4c872f 100644 --- a/ptgaze/head_pose_estimation/face_landmark_estimator.py +++ b/ptgaze/head_pose_estimation/face_landmark_estimator.py @@ -20,7 +20,7 @@ def __init__(self, config: DictConfig): elif self.mode == 'face_alignment_dlib': self.detector = dlib.get_frontal_face_detector() self.predictor = face_alignment.FaceAlignment( - face_alignment.LandmarksType._2D, + face_alignment.LandmarksType.TWO_D, face_detector='dlib', flip_input=False, device=config.device) @@ -28,7 +28,7 @@ def __init__(self, config: DictConfig): self.detector = face_alignment.detection.sfd.sfd_detector.SFDDetector( device=config.device) self.predictor = face_alignment.FaceAlignment( - face_alignment.LandmarksType._2D, + face_alignment.LandmarksType.TWO_D, flip_input=False, device=config.device) elif self.mode == 'mediapipe': @@ -57,10 +57,10 @@ def _detect_faces_dlib(self, image: np.ndarray) -> List[Face]: for bbox in bboxes: predictions = self.predictor(image[:, :, ::-1], bbox) landmarks = np.array([(pt.x, pt.y) for pt in predictions.parts()], - dtype=np.float) + dtype=float) bbox = np.array([[bbox.left(), bbox.top()], [bbox.right(), bbox.bottom()]], - dtype=np.float) + dtype=float) detected.append(Face(bbox, landmarks)) return detected @@ -77,7 +77,7 @@ def _detect_faces_face_alignment_dlib(self, predictions = [] detected = [] for bbox, landmarks in zip(bboxes, predictions): - bbox = np.array(bbox, dtype=np.float).reshape(2, 2) + bbox = np.array(bbox, dtype=float).reshape(2, 2) detected.append(Face(bbox, landmarks)) return detected @@ -91,7 +91,7 @@ def _detect_faces_face_alignment_sfd(self, predictions = [] detected = [] for bbox, landmarks in zip(bboxes, predictions): - bbox = np.array(bbox, dtype=np.float).reshape(2, 2) + bbox = np.array(bbox, dtype=float).reshape(2, 2) detected.append(Face(bbox, landmarks)) return detected diff --git a/ptgaze/head_pose_estimation/head_pose_normalizer.py b/ptgaze/head_pose_estimation/head_pose_normalizer.py index 6dac2a6..979bf14 100644 --- a/ptgaze/head_pose_estimation/head_pose_normalizer.py +++ b/ptgaze/head_pose_estimation/head_pose_normalizer.py @@ -65,4 +65,4 @@ def _get_scale_matrix(self, distance: float) -> np.ndarray: [0, 1, 0], [0, 0, self.normalized_distance / distance], ], - dtype=np.float) + dtype=float) diff --git a/ptgaze/models/mpiifacegaze/backbones/resnet_simple.py b/ptgaze/models/mpiifacegaze/backbones/resnet_simple.py index bc732b0..5460b61 100644 --- a/ptgaze/models/mpiifacegaze/backbones/resnet_simple.py +++ b/ptgaze/models/mpiifacegaze/backbones/resnet_simple.py @@ -20,8 +20,10 @@ def __init__(self, config: DictConfig): pretrained_name = config.model.backbone.pretrained if pretrained_name: - state_dict = torch.hub.load_state_dict_from_url( - torchvision.models.resnet.model_urls[pretrained_name]) + if pretrained_name != 'resnet18': + raise NotImplementedError() + url = torchvision.models.resnet.ResNet18_Weights.IMAGENET1K_V1.url + state_dict = torch.hub.load_state_dict_from_url(url) self.load_state_dict(state_dict, strict=False) # While the pretrained models of torchvision are trained # using images with RGB channel order, in this repository