File size: 1,161 Bytes
9191c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from datasets import load_dataset
from sklearn.metrics.pairwise import cosine_similarity

palette_dataset = load_dataset("danielritchie/cinematic-mood-palette")["train"]

palette_vectors = []
palette_names = []

for row in palette_dataset:
    palette_vectors.append([row["V"], row["A"], row["D"], row["Cx"], row["Co"]])
    palette_names.append(row.get("name", "unknown"))

palette_vectors = np.array(palette_vectors)


def nearest_palette_vector(raw_vad):
    raw_vec = np.array([[raw_vad[k] for k in ["V","A","D","Cx","Co"]]])
    sims = cosine_similarity(raw_vec, palette_vectors)[0]
    idx = np.argmax(sims)
    anchor = palette_vectors[idx]

    return {
        "vector": {
            "V": anchor[0],
            "A": anchor[1],
            "D": anchor[2],
            "Cx": anchor[3],
            "Co": anchor[4],
        },
        "name": palette_names[idx]
    }


def amplify_with_palette(raw, drama):
    anchor_data = nearest_palette_vector(raw)
    anchor = anchor_data["vector"]

    amplified = {
        k: float(raw[k] + drama * (anchor[k] - raw[k]))
        for k in raw
    }

    return amplified, anchor_data["name"]