retuve.hip_us.modes.landmarks

All code relating to going from measured landmarks to metrics is in this file.

 1# Copyright 2024 Adam McArthur
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#     http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16All code relating to going from measured landmarks to metrics is in this file.
17"""
18
19import time
20from typing import List, Tuple
21
22from retuve.classes.metrics import Metric2D
23from retuve.hip_us.classes.general import HipDatasUS, HipDataUS, LandmarksUS
24from retuve.hip_us.metrics.alpha import find_alpha_angle
25from retuve.hip_us.metrics.coverage import find_coverage
26from retuve.hip_us.metrics.curvature import find_curvature
27from retuve.keyphrases.config import Config
28from retuve.keyphrases.enums import MetricUS
29from retuve.logs import log_timings
30
31
32def landmarks_2_metrics_us(
33    list_landmarks: List[LandmarksUS],
34    shape: Tuple[int, int],
35    config: Config,
36) -> HipDatasUS:
37    """
38    Converts a list of landmarks to a list of metrics.
39
40    :param list_landmarks: A list of landmarks.
41    :param shape: The shape of the image.
42    :param config: The configuration object.
43
44    :return: A list of metrics.
45    """
46    hips = HipDatasUS()
47    timings = []
48
49    for frame_no, landmarks in enumerate(list_landmarks):
50        start = time.time()
51
52        alpha = find_alpha_angle(landmarks)
53        coverage = find_coverage(landmarks)
54        curvature = find_curvature(landmarks, shape, config)
55
56        metrics = []
57
58        for name, value in [
59            (MetricUS.ALPHA, alpha),
60            (MetricUS.COVERAGE, coverage),
61            (MetricUS.CURVATURE, curvature),
62        ]:
63            if name in config.hip.measurements:
64                metrics.append(Metric2D(name, value))
65
66        hip = HipDataUS(
67            landmarks=landmarks,
68            metrics=metrics,
69            frame_no=frame_no,
70        )
71
72        hips.append(hip)
73        timings.append(time.time() - start)
74
75    log_timings(timings, title="Landmarks->Metrics Speed:")
76
77    return hips
def landmarks_2_metrics_us( list_landmarks: List[retuve.hip_us.classes.general.LandmarksUS], shape: Tuple[int, int], config: retuve.keyphrases.config.Config) -> retuve.hip_us.classes.general.HipDatasUS:
33def landmarks_2_metrics_us(
34    list_landmarks: List[LandmarksUS],
35    shape: Tuple[int, int],
36    config: Config,
37) -> HipDatasUS:
38    """
39    Converts a list of landmarks to a list of metrics.
40
41    :param list_landmarks: A list of landmarks.
42    :param shape: The shape of the image.
43    :param config: The configuration object.
44
45    :return: A list of metrics.
46    """
47    hips = HipDatasUS()
48    timings = []
49
50    for frame_no, landmarks in enumerate(list_landmarks):
51        start = time.time()
52
53        alpha = find_alpha_angle(landmarks)
54        coverage = find_coverage(landmarks)
55        curvature = find_curvature(landmarks, shape, config)
56
57        metrics = []
58
59        for name, value in [
60            (MetricUS.ALPHA, alpha),
61            (MetricUS.COVERAGE, coverage),
62            (MetricUS.CURVATURE, curvature),
63        ]:
64            if name in config.hip.measurements:
65                metrics.append(Metric2D(name, value))
66
67        hip = HipDataUS(
68            landmarks=landmarks,
69            metrics=metrics,
70            frame_no=frame_no,
71        )
72
73        hips.append(hip)
74        timings.append(time.time() - start)
75
76    log_timings(timings, title="Landmarks->Metrics Speed:")
77
78    return hips

Converts a list of landmarks to a list of metrics.

Parameters
  • list_landmarks: A list of landmarks.
  • shape: The shape of the image.
  • config: The configuration object.
Returns

A list of metrics.