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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
use rayon::prelude::*;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
pub trait MetricElem: Send + Sync + 'static + Serialize + for<'a> Deserialize<'a> {
fn dist(&self, _: &Self) -> f64;
}
impl MetricElem for f64 {
fn dist(&self, b: &f64) -> f64 {
(self - b).abs()
}
}
pub trait EmbedderT: Send + Sync {
type Embedding: MetricElem;
const NAME: &'static str;
fn embed(&self, _: &PathBuf) -> Result<Self::Embedding, String>;
}
pub trait BatchEmbedder: Send + Sync {
type Embedding: MetricElem;
const NAME: &'static str;
fn embeds(&mut self, _: &[PathBuf]) -> Result<Vec<Self::Embedding>, String>;
}
impl<T: EmbedderT> BatchEmbedder for T {
type Embedding = T::Embedding;
const NAME: &'static str = T::NAME;
fn embeds(&mut self, paths: &[PathBuf]) -> Result<Vec<Self::Embedding>, String> {
paths.par_iter()
.map(|p| self.embed(p))
.collect::<Vec<_>>()
.into_iter()
.try_collect()
}
}
pub struct BrightnessEmbedder;
impl EmbedderT for BrightnessEmbedder {
type Embedding = f64;
const NAME: &'static str = "Brightness";
fn embed(&self, path: &PathBuf) -> Result<f64, String> {
let im = image::open(path).map_err(|e| e.to_string())?;
let num_bytes = 3 * (im.height() * im.width());
if num_bytes == 0 {
return Err("Encountered NaN brightness, due to an empty image".to_string());
}
Ok(im.to_rgb8()
.iter()
.map(|e| *e as u64)
.sum::<u64>() as f64 / num_bytes as f64)
}
}
#[repr(transparent)]
#[derive(Serialize, Deserialize)]
pub struct Hue (f64);
impl MetricElem for Hue {
fn dist(&self, b: &Hue) -> f64 {
let d = self.0.dist(&b.0);
d.min(6.-d)
}
}
pub struct HueEmbedder;
impl EmbedderT for HueEmbedder {
type Embedding = Hue;
const NAME: &'static str = "Hue";
fn embed(&self, path: &PathBuf) -> Result<Hue, String> {
let im = image::open(path).map_err(|e| e.to_string())?;
let num_pixels = im.height() * im.width();
let [sr, sg, sb] = im
.to_rgb8()
.pixels()
.fold([0, 0, 0], |[or, og, ob], n| {
let [nr, ng, nb] = n.0;
[or + nr as u64, og + ng as u64, ob + nb as u64]
})
.map(|e| e as f64 / 255. / num_pixels as f64);
let hue =
if sr >= sg && sr >= sb {
(sg - sb) / (sr - sg.min(sb))
}
else if sg >= sb {
2. + (sb - sr) / (sg - sr.min(sb))
}
else {
4. + (sr - sg) / (sb - sr.min(sg))
};
if hue.is_nan() {
return Err("Encountered NaN hue, possibly because of a colorless or empty image".to_string());
}
Ok(Hue(hue))
}
}
impl MetricElem for (f64, f64, f64) {
fn dist(&self, o: &(f64, f64, f64)) -> f64 {
let (dr, dg, db) =
((self.0 - o.0), (self.1 - o.1), (self.2 - o.2));
(dr*dr + dg*dg + db*db).sqrt()
}
}
pub struct ColorEmbedder;
impl EmbedderT for ColorEmbedder {
type Embedding = (f64, f64, f64);
const NAME: &'static str = "Color";
fn embed(&self, path: &PathBuf) -> Result<(f64, f64, f64), String> {
let im = image::open(path).map_err(|e| e.to_string())?;
let num_pixels = im.height() * im.width();
let [sr, sg, sb] = im
.to_rgb8()
.pixels()
.fold([0, 0, 0], |[or, og, ob], n| {
let [nr, ng, nb] = n.0;
[or + nr as u64, og + ng as u64, ob + nb as u64]
})
.map(|e| e as f64 / num_pixels as f64);
Ok((sr, sg, sb))
}
}
|