From 43e94e67a90a101245cd9342ad4a84014e10693b Mon Sep 17 00:00:00 2001 From: tigercosmos Date: Sat, 13 Oct 2018 14:15:29 +0800 Subject: [PATCH] add meow service --- README.md | 13 ++++++++++++- api/meow.js | 33 +++++++++++++++++++++++++++++++++ main.js | 12 ++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 api/meow.js diff --git a/README.md b/README.md index e64c906..57fd504 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,23 @@ Cloud classification service for weather bot -nodejs and python with tensorflow are required. +rust, nodejs, python with tensorflow are required. ```sh git clone https://github.com/weather-bot/cloudlady cd cloudlady npm install +# download model wget https://github.com/weather-bot/cloudlady/releases/download/v0.0.0/retrained_graph.pb -P python/model +# download cats images +wget https://github.com/weather-bot/meow/releases/download/v0.0.1/cats.tar.gz +tar zxvf cats.tar.gz +# compile meow +git clone https://github.com/weather-bot/meow.git +cd meow +cargo build --release +cp ./target/release/meow ../meow +cd .. +# run npm start ``` diff --git a/api/meow.js b/api/meow.js new file mode 100644 index 0000000..62391ae --- /dev/null +++ b/api/meow.js @@ -0,0 +1,33 @@ +'use strict'; + +const express = require('express'); +const app = express.Router(); +const logger = require('node-color-log'); +const uuidv1 = require('uuid/v1'); + +app.post('/moew', async (req, res) => { + const uuid = uuidv1(); + logger.info(`/api/meow: [${uuid}] new request`); + + const info = req.body; + + const meow = path.join(__dirname, '../meow'); + const outputFile = path.join(__dirname, `../${uuid}.jpg`); + // cats is the images folder, and 27 is quantity. + const command = `${meow} bottom-mode cats/${Math.floor(Math.random()*27)}.jpg '${JSON.stringify(info)}' -o ${outputFile}`; + const { + stdout, + stderr + } = await exec(command); + if (stderr) { + logger.warn(`/api/meow: [${uuid}] return 400`); + return res.status(400).send("meow creates image failed."); + } + logger.info(`/api/meow: [${uuid}] the image created.`) + logger.info(`/api/meow: [${uuid}] return 200`); + return res.status(200).json({ + uuid + }); +}) + +module.exports = app; \ No newline at end of file diff --git a/main.js b/main.js index a7813ef..d76b01b 100644 --- a/main.js +++ b/main.js @@ -18,6 +18,18 @@ app.use(cors()); // ===== API ===== app.use('/api', require('./api/classify')); +app.use('/api', require('./api/meow')); + +// ===== Image ===== +app.get('/img', (req, res)=> { + const uuid = req.query.uuid; + const file = `${uuid}.jpg`; + res.sendFile(file); + fs.unlink(file, err => { + if (err) logger.error(err); + logger.info(`/img: [${uuid}] the image delete.`) + }); +}); // ===== Front End ===== app.get('/', (req, res) => {