From f1df796989d3cf784936ff81cee309a3614e9323 Mon Sep 17 00:00:00 2001 From: colbyn Date: Tue, 3 Mar 2020 15:09:14 -0700 Subject: [PATCH] Working on new evaluation setup, and updated image dependencies. --- classifier/Cargo.toml | 4 +- classifier/src/color/quant.rs | 3 +- classifier/src/main.rs | 2 +- classifier/src/process.rs | 122 ++++++++++------------------------ 4 files changed, 41 insertions(+), 90 deletions(-) diff --git a/classifier/Cargo.toml b/classifier/Cargo.toml index cb3f5e9..ebd7364 100644 --- a/classifier/Cargo.toml +++ b/classifier/Cargo.toml @@ -5,9 +5,9 @@ authors = ["colbyn "] edition = "2018" [dependencies] +image = "0.23" +imageproc = "0.20" libc = "^0.2" -imageproc = "0.19.2" -image = "0.22.2" rand = "0.7.3" glob = "0.3.0" rayon = "1.3.0" diff --git a/classifier/src/color/quant.rs b/classifier/src/color/quant.rs index 55033c7..ce81149 100644 --- a/classifier/src/color/quant.rs +++ b/classifier/src/color/quant.rs @@ -8,6 +8,7 @@ use exoquant::optimizer::Optimizer; use exoquant::*; use lodepng::Bitmap; use lodepng::RGBA; +use image::imageops::{FilterType}; use image::{DynamicImage, GenericImage, GenericImageView}; fn encode_indexed(palette: &[Color], image: &[u8], width: u32, height: u32) -> Vec { @@ -87,6 +88,6 @@ pub fn compress(source: &DynamicImage, num_colors: usize) -> Result, Str pub fn reduce_palette(source: &DynamicImage, num_colors: usize) -> DynamicImage { let result = compress(source, num_colors).expect("failed to reduce color palette"); - let result = ::image::load_from_memory_with_format(&result, ::image::ImageFormat::PNG).expect("decode png"); + let result = ::image::load_from_memory_with_format(&result, ::image::ImageFormat::Png).expect("decode png"); result } \ No newline at end of file diff --git a/classifier/src/main.rs b/classifier/src/main.rs index 8d9c3d3..503adfa 100644 --- a/classifier/src/main.rs +++ b/classifier/src/main.rs @@ -3,5 +3,5 @@ pub mod process; pub mod color; fn main() { - // process::run(); + process::run(); } diff --git a/classifier/src/process.rs b/classifier/src/process.rs index 02c522d..77d3c5a 100644 --- a/classifier/src/process.rs +++ b/classifier/src/process.rs @@ -3,6 +3,7 @@ use std::path::{PathBuf, Path}; use std::collections::{HashMap, HashSet}; use std::ops::Deref; use rand::prelude::*; +use image::imageops::FilterType; use image::{GenericImage, GenericImageView, ImageBuffer, DynamicImage}; use image::{Luma, Rgb, Pixel}; use imageproc::region_labelling::{ @@ -18,9 +19,26 @@ use serde::{Serialize, Deserialize}; use crate::color::palette::{self, ToPrettyRgbPalette}; +/////////////////////////////////////////////////////////////////////////////// +// NOISY LAYER +/////////////////////////////////////////////////////////////////////////////// + +pub struct NoisyLayer(DynamicImage); + +impl NoisyLayer { + pub fn new(input: DynamicImage) -> Self { + unimplemented!() + } +} + + +/////////////////////////////////////////////////////////////////////////////// +// PASSES +/////////////////////////////////////////////////////////////////////////////// + pub fn quantizer(image: &DynamicImage) -> DynamicImage { - let image = image.resize_exact(600, 600, ::image::FilterType::Lanczos3); + let image = image.resize_exact(600, 600, FilterType::Lanczos3); let image = image.unsharpen(1.2, 4); let image = crate::color::quant::reduce_palette(&image, 64); let image = image.to_luma(); @@ -42,11 +60,6 @@ pub fn quantizer(image: &DynamicImage) -> DynamicImage { DynamicImage::ImageRgb8(image) } -pub fn preprocess(image: &DynamicImage, class: Class) -> DynamicImage { - let image = quantizer(&image); - image -} - /////////////////////////////////////////////////////////////////////////////// // CLASSIFY @@ -92,91 +105,28 @@ impl std::fmt::Display for Class { } - -pub fn train() { - let load_group = |pattern: &str| -> Vec { - glob::glob(pattern) - .expect("input glob") - .filter_map(Result::ok) - .map(|input_path| { - let image = ::image::open(input_path).expect("load input image"); - let image = quantizer(&image); - image - }) - .collect::>() - }; - let dataset = vec![ - ( - load_group("assets/samples/high/**/*.jpeg"), - Class::Hi, - ), - ( - load_group("assets/samples/low/**/*.jpeg"), - Class::Lo, - ), - ( - load_group("assets/samples/extra-low/**/*.jpeg"), - Class::ExLo, - ), - ( - load_group("assets/samples/high-basic/**/*.jpeg"), - Class::HiBasic, - ), - ]; - for (media, class) in dataset { - - } -} - - /////////////////////////////////////////////////////////////////////////////// // MAIN /////////////////////////////////////////////////////////////////////////////// -// pub fn process(input_path: &str, output_path: &str, class: Class) { -// // RUN -// let image = ::image::open(input_path).expect("open source image"); -// let debug_image = preprocess(&image, class); - -// // FILE PATHS -// let base_path = PathBuf::from(output_path) -// .parent() -// .expect("parent path") -// .to_owned(); -// std::fs::create_dir_all(&base_path); - -// let debug_output_path = { -// let mut path = PathBuf::from(output_path) -// .file_name() -// .map(|x| PathBuf::from(x.clone())) -// .expect("file name"); -// path.set_extension("debug.jpeg"); -// base_path.join(path) -// }; - -// // SAVE -// // image.save(output_path); -// debug_image.save(debug_output_path); -// // let compressed = unsafe { -// // crate::codec::jpeg::encode(&image, 4) -// // }; -// // std::fs::write(debug_output_path, compressed); -// } - pub fn run() { - // let output_path = PathBuf::from("assets/output"); - // let paths = glob::glob("assets/samples/focus/**/*.jpeg") - // .expect("input glob") - // .filter_map(Result::ok) - // .map(|input_path| { - // let output_path = input_path - // .file_name() - // .map(|name| { - // output_path.join(name) - // }) - // .expect("init output file name"); - // println!("path: {:?}", output_path); - // }); + let output_dir = PathBuf::from("assets/output"); + std::fs::create_dir_all(&output_dir); + let paths = glob::glob("assets/samples/focus/**/*.jpeg") + .expect("input glob") + .filter_map(Result::ok) + .collect::>() + .into_par_iter() + .for_each(|input_path| { + let mut output_path = input_path + .file_name() + .map(|name| output_dir.join(name)) + .expect("init output file name"); + output_path.set_extension("jpeg"); + let src_image = ::image::open(input_path).expect("open source image"); + let out_image = quantizer(&src_image); + out_image.save(output_path); + }); }