File size: 2,893 Bytes
06cc068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
"""Compile explicit camera travel into a visual cue without rewriting the scene."""
import re
import unicodedata

# Full-cue matches only. Negations, multiple actions and editing instructions go to the director.
_TRAVEL = {
    "recule": "back", "se rapproche": "in", "avance": "in",
    "pulls back": "back", "moves backward": "back", "moves back": "back",
    "pushes in": "in", "moves closer": "in", "moves forward": "in",
}
_ANGLES = {"contre plongee": "low", "plongee": "high", "low angle": "low", "high angle": "high"}
_CUE = re.compile(
    r"(?:(?:en |from a |at a )(?P<start>" + "|".join(_ANGLES) + r"),? )?"
    r"(?:(?:la|the) )?camera (?P<move>" + "|".join(_TRAVEL) + r")"
    r"(?:,? (?:en |from a |at a )?(?P<end>" + "|".join(_ANGLES) + r")(?: view)?)?"
)
_ANGLE_TEXT = {"low": " from a low viewpoint looking up at the subject",
               "high": " from a high viewpoint looking down at the subject", "": ""}
_MOVES = {
    "back": ("pulls backward away from the subject", "The subject grows smaller in the frame as more of the surroundings come into view."),
    "in": ("tracks forward toward the subject", "The subject grows larger in the frame as the view tightens."),
}
# An exact, known first line lets a later camera cue replace the travel while retaining the angle.
_SHOTS = {
    (move, angle): f"The camera {travel}{position}. {effect}"
    for move, (travel, effect) in _MOVES.items() for angle, position in _ANGLE_TEXT.items()
}
_PREVIOUS = {text: key for key, text in _SHOTS.items()}
_CAMERA_CONTEXT = re.compile(
    r"\b(camera|lens|shot|viewpoint|zoom|pan|pans|tilt|dolly|tracking|framing|foreground|"
    r"close[- ]up|wide[- ]angle|low[- ]angle|high[- ]angle|from above|from below|bird.s.eye)\b", re.I
)


def camera_direction(instruction, old, selected):
    scene = old["prompt"].strip()
    if not scene:
        return None
    plain = unicodedata.normalize("NFKD", instruction.casefold())
    plain = "".join(c for c in plain if not unicodedata.combining(c))
    plain = " ".join(re.sub(r"[-‐‑–]", " ", plain).strip().rstrip(".!").split())
    match = _CUE.fullmatch(plain)
    if not match or (match["start"] and match["end"]):
        return None
    previous, split, description = scene.partition("\n")
    old_angle = ""
    if split and previous in _PREVIOUS:
        _, old_angle = _PREVIOUS[previous]
        scene = description
    elif "camera" in old["focus"].split("+") or _CAMERA_CONTEXT.search(scene):
        # The model can reconcile a nonstandard prior composition with the new direction.
        return None
    move = _TRAVEL[match["move"]]
    angle = _ANGLES.get(match["start"] or match["end"], old_angle)
    summary = ("Camera pulls back" if move == "back" else "Camera moves in")
    if angle:
        summary += f" at a {angle} angle"
    return selected or "append", "camera", summary, _SHOTS[move, angle] + "\n" + scene