File size: 1,332 Bytes
ace9173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import re
from typing import Tuple

def parse_camera_params(
    text: str,
    mode: str = "base"
) -> Tuple[float, float, float]:
    """
    Extract roll, pitch, fov from text using one of two patterns:
      - 'base'   mode: ... are: roll, pitch, fov.
      - 'cot'    mode: <answer>roll, pitch, fov</answer>

    Args:
        text: The full text to search.
        mode: One of {"base", "cot"}.

    Returns:
        roll, pitch, fov as floats.

    Raises:
        ValueError if the chosen pattern is not found, or mode is invalid.
    """
    # compile both regexes
    pat_base = re.compile(
        r"are:\s*([+-]?\d+(?:\.\d+)?)\s*,\s*"
        r"([+-]?\d+(?:\.\d+)?)\s*,\s*"
        r"([+-]?\d+(?:\.\d+)?)[\.\s]*$"
    )
    pat_cot = re.compile(
        r"<answer>\s*([+-]?\d+(?:\.\d+)?)\s*,\s*"
        r"([+-]?\d+(?:\.\d+)?)\s*,\s*"
        r"([+-]?\d+(?:\.\d+)?)\s*</answer>"
    )

    m = None
    if mode == "base":
        m = pat_base.search(text)
    elif mode == "cot":
        m = pat_cot.search(text)
    else:
        raise ValueError(f"Invalid mode: {mode!r}. Choose 'base', 'cot', or 'auto'.")

    if not m:
        raise ValueError(f"No camera parameters found using mode '{mode}'.")

    roll_s, pitch_s, fov_s = m.group(1), m.group(2), m.group(3)
    return float(roll_s), float(pitch_s), float(fov_s)