Skip to content

Commit

Permalink
Working on new evaluation setup, and updated image dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
colbyn committed Mar 3, 2020
1 parent b8bc316 commit f1df796
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 90 deletions.
4 changes: 2 additions & 2 deletions classifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ authors = ["colbyn <[email protected]>"]
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"
Expand Down
3 changes: 2 additions & 1 deletion classifier/src/color/quant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
Expand Down Expand Up @@ -87,6 +88,6 @@ pub fn compress(source: &DynamicImage, num_colors: usize) -> Result<Vec<u8>, 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
}
2 changes: 1 addition & 1 deletion classifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pub mod process;
pub mod color;

fn main() {
// process::run();
process::run();
}
122 changes: 36 additions & 86 deletions classifier/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -92,91 +105,28 @@ impl std::fmt::Display for Class {
}



pub fn train() {
let load_group = |pattern: &str| -> Vec<DynamicImage> {
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::<Vec<_>>()
};
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::<Vec<_>>()
.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);
});
}

0 comments on commit f1df796

Please sign in to comment.