Added FeatureExtr

FeatureExtractor provided by Servostar
This commit is contained in:
SirTalksalot75 2023-06-17 12:05:24 +02:00 committed by GitHub
parent 2f228ff2cf
commit 956894c07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 81 additions and 56 deletions

View File

@ -1,69 +1,94 @@
use std::collections::HashMap; #[derive(Debug, Clone, Serialize, Deserialize)]
enum FeatureResult {
fn extract_color_distribution(image_data: &[u8]) -> HashMap<u32, f32> { /// A boolean. Just a boolean
let mut color_distribution: HashMap<u32, f32> = HashMap::new(); Bool(bool),
let total_pixels = image_data.len() as f32 / 4.0;//für 4 Werte /// Signed 32-bit integer
I32(i32),
for pixel in image_data.chunks_exact(4) { /// 32-bit single precision floating point
let r = pixel[0] as u32; /// can be used for aspect ratio or luminance
let g = pixel[1] as u32; F32(f32),
let b = pixel[2] as u32; /// Vector for nested multidimensional
let a = pixel[3] as u32; Vec(Vec<FeatureResult>),
let rgba = (r << 24) | (g << 16) | (b << 8) | a; /// Standard RGBA color
RGBA(f32, f32, f32, f32),
*color_distribution.entry(rgba).or_insert(0.0) += 1.0; /// Indices intended for the usage in historgrams
Indices(Vec<u64>)
} }
for (_, count) in &mut color_distribution { impl Default for FeatureResult {
*count /= total_pixels; fn default() -> Self {
FeatureResult::Bool(false)
}
} }
color_distribution /// For some feature return type we want to implement a custom compare function
/// for example: historgrams are compared with cosine similarity
impl PartialEq for FeatureResult {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
(Self::I32(l0), Self::I32(r0)) => l0 == r0,
(Self::F32(l0), Self::F32(r0)) => l0 == r0,
(Self::Vec(l0), Self::Vec(r0)) => l0 == r0,
(Self::RGBA(l0, l1, l2, l3), Self::RGBA(r0, r1, r2, r3)) => l0 == r0 && l1 == r1 && l2 == r2 && l3 == r3,
(Self::Indices(_), Self::Indices(_)) => todo!("implement cosine similarity"),
_ => false,
}
}
} }
fn extract_average_brightness(image: &[u8]) -> u8 { type FeatureGenerator = Box<dyn Fn(crate::Image<f32>) -> (String, FeatureResult)>;
let mut sum: u32 = 0;
let mut count: u32 = 0;
for i in (0..image.len()).step_by(4) { #[derive(Serialize, Deserialize, Default)]
let r = image[i] as u32; struct Database {
let g = image[i + 1] as u32; images: HashMap<String, HashMap<String, FeatureResult>>,
let b = image[i + 2] as u32;
// (0.299 * R) + (0.587 * G) + (0.114 * B) /// keep feature generator for the case when we add a new image
let brightness = ((0.299 * r as f32) + (0.587 * g as f32) + (0.114 * b as f32)).round() as u32; /// this field is not serialized and needs to be wrapped in an option
#[serde(skip)]
sum += brightness; generators: Option<Vec<FeatureGenerator>>
count += 1;
}
let average_brightness = (sum / count) as u8;
average_brightness
} }
impl Database {
fn main() { pub fn add_feature(&mut self, feature: FeatureGenerator) {
for (path, features) in self.images.iter_mut() {
test2(); // compute feature for every image
todo!("run this as a closure parallel with a thread pool");
let (name, res) = feature(todo!("load image from disk"));
features.insert(name, res);
} }
fn test2(){ if let Some(generators) = self.generators.as_mut() {
generators.push(feature);
let image_data: Vec<(u8, u8, u8, u8)> = vec![ } else {
(255, 0, 0, 255), // Red self.generators = Some(vec![feature])
(0, 255, 0, 255), // Green }
(0, 0, 255, 255), // Blue }
];
//convert image data to useable &u8 slice pub fn add_image(&mut self, path: String) {
let byte_slice: &[u8] = unsafe { let image = todo!("load image from disk");
std::slice::from_raw_parts( let mut features = HashMap::new();
image_data.as_ptr() as *const u8, if let Some(generators) = self.generators {
image_data.len() * 4, for generator in generators.iter() {
) let (name, res) = generator(image);
}; features.insert(name, res);
let color_distribution = extract_color_distribution(&byte_slice); }
let color_distribution_vec: Vec<f32> = color_distribution.values().cloned().collect(); }
let average_brightness = extract_average_brightness(&byte_slice); self.images.insert(path, features);
println!("{:?}", average_brightness); }
println!("{:?}", color_distribution_vec); }
/// example feature implementation
fn average_luminance(image: Image<f32>) -> (String, FeatureResult) {
(String::from("average-brightness"), FeatureResult::F32(0.0))
}
#[test]
fn test() {
let mut data = Database::default();
data.add_feature(Box::new(average_luminance));
let _as_json = serde_json::to_string(&data);
} }