Skip to content

Commit

Permalink
Merge pull request #13 from schani-rs/feature/send-to-image-recognition
Browse files Browse the repository at this point in the history
Send processed image to the image recognition service
  • Loading branch information
ChristophWurst authored Jun 25, 2017
2 parents cf90e3d + 38be733 commit f25027b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ resolve = "0.1.2"
rocket = "0.2.8"
rocket_codegen = "0.2.8"
schani = { git = "https://github.com/schani-rs/schani.git" }
serde_json="1.0.2"
serde = "^0.9"
serde_derive = "^0.9"
serde_json="^0.9"
temporary = "0.6.3"
tokio-core = "0.1.7"
tokio-io = "^0.1"
Expand Down
53 changes: 53 additions & 0 deletions src/image_recognition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use super::error;
use std::fs::File;
use std::io::Read;
use std::io::prelude::*;
use std::path::PathBuf;

use hyper::{self, header, method, Url};
use hyper::client::Request;
use serde_json;

// TODO: create external library to prevent code duplication across microservices
#[derive(Debug, Deserialize)]
struct Prediction {
class: String,
score: f64,
}

pub fn classify_image(image_path: &PathBuf) -> Result<(), error::Error> {
info!(
"loading image file for processing from {:?}",
image_path,
);

let mut image_buf = vec![];
let mut image_file = try!(File::open(image_path));
try!(image_file.read_to_end(&mut image_buf));
info!("loaded image file, size is {} bytes", image_buf.len());

let url = try!(Url::parse("http://image_recognition:8000/recognize"));
let mut req = try!(Request::new(method::Method::Post, url));
req.headers_mut().set(header::ContentLength(
image_buf.len() as u64,
));
let mut stream_req = try!(req.start());
try!(stream_req.write_all(image_buf.as_slice()));
try!(stream_req.flush());
let resp = try!(stream_req.send());

if resp.status != hyper::Ok {
return Err(error::Error::Generic(format!(
"unexpected status {} when sending image for recognition",
resp.status
)));
}

let predictions: Vec<Prediction> = try!(serde_json::from_reader(resp).map_err(|err| {
error::Error::Generic(format!("could not deserialize predictions json: {}", err))
}));

info!("predicionts: {:?}", predictions);

Ok(())
}
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ extern crate hyper;
#[macro_use]
extern crate log;
extern crate schani;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate temporary;
extern crate url;

pub mod rawtherapee;
mod image_recognition;
pub mod error;
pub mod rawtherapee;
mod store;

use rawtherapee::process_raw;
use store::{load_raw_file, upload_image_file};
use image_recognition::classify_image;

use temporary::Directory;

pub fn process_raw_image(image_id: i32) -> Result<(), error::Error> {
Expand All @@ -21,6 +26,7 @@ pub fn process_raw_image(image_id: i32) -> Result<(), error::Error> {

let tmp_path = directory.join(image_id.to_string() + &".NEF".to_string());
println!("{:?}", tmp_path);
let img_path = directory.join(image_id.to_string() + &".jpg".to_string());
let target_path = directory.into_path();

try!(load_raw_file(image_id, &tmp_path));
Expand All @@ -29,6 +35,7 @@ pub fn process_raw_image(image_id: i32) -> Result<(), error::Error> {
info!("processed image {} …", image_id);
try!(upload_image_file(image_id, &target_path));
info!("uploaded image {} …", image_id);
try!(classify_image(&img_path));

Ok(())
}

0 comments on commit f25027b

Please sign in to comment.