Commit fcb25751 authored by John's avatar John

Added command line Args and parameterized k clusters

parent b1616c40
...@@ -8,6 +8,11 @@ name = "approx" ...@@ -8,6 +8,11 @@ name = "approx"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "argparse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "arrayvec" name = "arrayvec"
version = "0.4.7" version = "0.4.7"
...@@ -125,9 +130,12 @@ dependencies = [ ...@@ -125,9 +130,12 @@ dependencies = [
name = "image_analyzer" name = "image_analyzer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"argparse 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"palette 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "palette 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", "rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.74 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
...@@ -138,6 +146,11 @@ dependencies = [ ...@@ -138,6 +146,11 @@ dependencies = [
"adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "itoa"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "jpeg-decoder" name = "jpeg-decoder"
version = "0.1.15" version = "0.1.15"
...@@ -391,6 +404,11 @@ dependencies = [ ...@@ -391,6 +404,11 @@ dependencies = [
"rulinalg 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "rulinalg 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "ryu"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "scoped_threadpool" name = "scoped_threadpool"
version = "0.1.9" version = "0.1.9"
...@@ -401,6 +419,21 @@ name = "scopeguard" ...@@ -401,6 +419,21 @@ name = "scopeguard"
version = "0.3.3" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_json"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"ryu 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.74 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "siphasher" name = "siphasher"
version = "0.2.3" version = "0.2.3"
...@@ -448,6 +481,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -448,6 +481,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" "checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c"
"checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94" "checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94"
"checksum argparse 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37bb99f5e39ee8b23b6e227f5b8f024207e8616f44aa4b8c76ecd828011667ef"
"checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef" "checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef"
"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" "checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789"
"checksum byteorder 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8389c509ec62b9fe8eca58c502a0acaf017737355615243496cde4994f8fa4f9" "checksum byteorder 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8389c509ec62b9fe8eca58c502a0acaf017737355615243496cde4994f8fa4f9"
...@@ -463,6 +497,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -463,6 +497,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gif 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff3414b424657317e708489d2857d9575f4403698428b040b609b9d1c1a84a2c" "checksum gif 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff3414b424657317e708489d2857d9575f4403698428b040b609b9d1c1a84a2c"
"checksum image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebdff791af04e30089bde8ad2a632b86af433b40c04db8d70ad4b21487db7a6a" "checksum image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebdff791af04e30089bde8ad2a632b86af433b40c04db8d70ad4b21487db7a6a"
"checksum inflate 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6f53b811ee8e2057ccf9643ca6b4277de90efaf5e61e55fd5254576926bb4245" "checksum inflate 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6f53b811ee8e2057ccf9643ca6b4277de90efaf5e61e55fd5254576926bb4245"
"checksum itoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5adb58558dcd1d786b5f0bd15f3226ee23486e24b7b58304b60f64dc68e62606"
"checksum jpeg-decoder 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "c8b7d43206b34b3f94ea9445174bda196e772049b9bddbc620c9d29b2d20110d" "checksum jpeg-decoder 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "c8b7d43206b34b3f94ea9445174bda196e772049b9bddbc620c9d29b2d20110d"
"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
...@@ -493,8 +528,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -493,8 +528,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum rayon-core 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b055d1e92aba6877574d8fe604a63c8b5df60f60e5982bf7ccbb1338ea527356" "checksum rayon-core 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b055d1e92aba6877574d8fe604a63c8b5df60f60e5982bf7ccbb1338ea527356"
"checksum rulinalg 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5670ce3717deb2d4436bdb29f07c549b56595f1fd8af1ca4682f1c1b1ac57f86" "checksum rulinalg 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5670ce3717deb2d4436bdb29f07c549b56595f1fd8af1ca4682f1c1b1ac57f86"
"checksum rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "dee5358236bbd0835aeee9a8b04c8b9b0aaea243a1a1a71253957b4606fb9171" "checksum rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "dee5358236bbd0835aeee9a8b04c8b9b0aaea243a1a1a71253957b4606fb9171"
"checksum ryu 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e7c066b8e2923f05d4718a06d2622f189ff362bc642bfade6c6629f0440f3827"
"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum serde 1.0.74 (registry+https://github.com/rust-lang/crates.io-index)" = "f218becd0d51dd24297ef804cb9b2de179dcdc2a3ddf8a73b04b4d595d9e6338"
"checksum serde_json 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "44dd2cfde475037451fa99b7e5df77aa3cfd1536575fa8e7a538ab36dcde49ae"
"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
"checksum syn 0.14.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b7bfcbb0c068d0f642a0ffbd5c604965a360a61f99e8add013cef23a838614f3" "checksum syn 0.14.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b7bfcbb0c068d0f642a0ffbd5c604965a360a61f99e8add013cef23a838614f3"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
......
...@@ -15,3 +15,6 @@ path = "src/pi.rs" ...@@ -15,3 +15,6 @@ path = "src/pi.rs"
palette = "0.4.1" palette = "0.4.1"
image = "0.19.0" image = "0.19.0"
rusty-machine = "0.5.4" rusty-machine = "0.5.4"
serde = "1.0.74"
serde_json = "1.0.26"
argparse = "0.2.1"
...@@ -50,15 +50,15 @@ ...@@ -50,15 +50,15 @@
var width = img.width; var width = img.width;
var xpos = 0 var xpos = 0
if(width/2 < 50*data.colors.length/2){ if(width/2 < 50*data.colors.length/2){
xpos = ((width/2) + 200) + (50*data.colors.length/2) xpos = ((width/2) + 100)
}else{ }else{
xpos = ((width/2) + 200) - (50*data.colors.length/2) xpos = ((width/2) + 100)
} }
var $myCanvas = $('#myCanvas'); var $myCanvas = $('#myCanvas');
$myCanvas.drawImage({ $myCanvas.drawImage({
source: img, source: img,
x: xpos , y: (img.height/2)+200, x: xpos + (50*data.colors.length/2) , y: (img.height/2)+200,
load: palette load: palette
}); });
// code here to use the dimensions // code here to use the dimensions
......
{ {"colors":["#22496c","#bfd8e4","#4f3e34","#308194","#d88a39","#5fa5b8","#97776b","#abcddd","#c2bbbe","#7f563e","#828490","#281a0c","#90bfce","#e6ad82","#65646b","#4894a9","#b1958e","#a2a2ac","#79b3c4","#d6dadf"],"image":"./springs.jpg"}
"image":"img.jpg", \ No newline at end of file
"colors":["#6795c0", "#a4b5d3", "#282434", "#485471", "#4774a0"]
}
\ No newline at end of file
extern crate image; extern crate image;
extern crate rusty_machine; extern crate rusty_machine;
extern crate serde;
extern crate argparse;
#[macro_use]
extern crate serde_json;
use std::fs;
use std::path::Path;
use argparse::{ArgumentParser, Store};
use image::{FilterType, DynamicImage, GenericImage}; use image::{FilterType, DynamicImage, GenericImage};
use rusty_machine::linalg::{Matrix, BaseMatrix}; use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::{KMeansClassifier}; use rusty_machine::learning::k_means::{KMeansClassifier};
...@@ -19,7 +28,7 @@ use rusty_machine::learning::UnSupModel; ...@@ -19,7 +28,7 @@ use rusty_machine::learning::UnSupModel;
// ret.min(255) as u8 // ret.min(255) as u8
//} //}
fn convert(centroids:&Matrix<f64>){ fn convert(centroids:&Matrix<f64>) -> Vec<String>{
let mut colorpalette = Vec::new(); let mut colorpalette = Vec::new();
for i in 0..centroids.rows() { for i in 0..centroids.rows() {
...@@ -27,8 +36,8 @@ fn convert(centroids:&Matrix<f64>){ ...@@ -27,8 +36,8 @@ fn convert(centroids:&Matrix<f64>){
let hex = format!("#{:02x}{:02x}{:02x}",colors[0] as u8, colors[1] as u8, colors[2] as u8); let hex = format!("#{:02x}{:02x}{:02x}",colors[0] as u8, colors[1] as u8, colors[2] as u8);
colorpalette.push(hex); colorpalette.push(hex);
} }
println!("{:?}", colorpalette) println!("{:?}", colorpalette);
colorpalette
} }
fn resize_image(img:&DynamicImage, maxpix: f64) -> DynamicImage{ fn resize_image(img:&DynamicImage, maxpix: f64) -> DynamicImage{
...@@ -43,14 +52,36 @@ fn resize_image(img:&DynamicImage, maxpix: f64) -> DynamicImage{ ...@@ -43,14 +52,36 @@ fn resize_image(img:&DynamicImage, maxpix: f64) -> DynamicImage{
} }
fn main() { fn main() {
let maxpix = 100000.0; let mut maxpix = 100000.0;
let clusters:usize = 5; let mut resize = true;
let mut clusters:usize = 10;
let mut file:String = "".to_string();
{ // this block limits scope of borrows by ap.refer() method
let mut ap = ArgumentParser::new();
ap.set_description("Use Neural networks to help group colors in a photo");
ap.refer(&mut file)
.add_argument("file", Store,
"The path to the file you want to read").required();
ap.refer(&mut clusters)
.add_option(&["-k"], Store,
"How many colors you want to group into");
ap.refer(&mut maxpix)
.add_option(&["--max"], Store,
"The maximum amount of pixels to have (will resize to fit)");
ap.refer(&mut resize)
.add_option(&["-r"], Store,
"Resize the image");
ap.parse_args_or_exit();
}
let orig_img = image::open("img2.jpg").unwrap(); let orig_img = image::open(Path::new(&file)).unwrap();
let orig_dimensions = orig_img.dimensions(); let orig_dimensions = orig_img.dimensions();
let mut img = orig_img; let mut img = orig_img;
if orig_dimensions.0 * orig_dimensions.1 > maxpix as u32 { if orig_dimensions.0 * orig_dimensions.1 > maxpix as u32 && resize{
img = resize_image(&img, maxpix); img = resize_image(&img, maxpix);
} }
...@@ -75,7 +106,18 @@ fn main() { ...@@ -75,7 +106,18 @@ fn main() {
let centroids = model.centroids().as_ref().unwrap(); let centroids = model.centroids().as_ref().unwrap();
// println!("Model Centroids:\n{:.0}", centroids); // println!("Model Centroids:\n{:.0}", centroids);
convert(centroids); let palette = convert(centroids);
let colors = json!({"image":file,"colors":palette});
println!("{}",colors);
let jsoncolor = match serde_json::to_string(&colors) {
Ok(data) => data,
Err(e) => "Not able to write data".to_string(),
};
fs::write("colors.json", jsoncolor);
// let slice = MatrixSlice::from_matrix(&centroids, [0,0], 1, 3); // let slice = MatrixSlice::from_matrix(&centroids, [0,0], 1, 3);
// let slice = centroids.get_row(0).unwrap(); // let slice = centroids.get_row(0).unwrap();
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment