Created
April 2, 2018 14:37
-
-
Save coolicer/651ff0b97d1cdf8cae7dfc215a162d77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Koa = require('koa'); | |
const Router = require('koa-router'); | |
const static = require('koa-static') | |
const nunjucks = require('koa-nunjucks-2'); | |
const multer = require('koa-multer'); | |
const path = require('path'); | |
const fs = require('fs'); | |
const app = new Koa(); | |
const router = new Router(); | |
const AipOcrClient = require("baidu-aip-sdk").ocr; | |
const APP_ID = ""; | |
const API_KEY = ""; | |
const SECRET_KEY = ""; | |
const client = new AipOcrClient(APP_ID, API_KEY, SECRET_KEY); | |
const storage = multer.diskStorage({ | |
destination: function (req, file, cb) { | |
cb(null, './uploads') | |
}, | |
filename: function (req, file, cb) { | |
cb(null, file.originalname) | |
} | |
}) | |
const upload = multer({ storage: storage }); | |
router | |
.get('/', async (ctx, next) => { | |
await ctx.render('page/index'); | |
}) | |
// fieldname: 'avatar', | |
// originalname: 'main_icon.png', | |
// encoding: '7bit', | |
// mimetype: 'image/png', | |
// destination: 'uploads/', | |
// filename: '6d8b3f078e4ca33d4be77b9e92ddce05', | |
// path: 'uploads/6d8b3f078e4ca33d4be77b9e92ddce05', | |
// size: 41208 | |
.post('/upload', upload.single('file'), async (ctx, next) => { | |
let file = ctx.req.file; | |
ctx.body = { | |
code: 200, | |
data: { | |
url: file.originalname | |
} | |
}; | |
}) | |
.get('/ocr/:path', async ctx => { | |
const options = {}; | |
options["language_type"] = "CHN_ENG"; | |
options["probability"] = "true"; | |
let path = ctx.params.path; | |
const image = fs.readFileSync('./uploads/' + path).toString("base64"); | |
let result = await client.accurateBasic(image, options) | |
await ctx.render('page/ocr', { words: result.words_result}) | |
}); | |
app.use(static( | |
path.join( __dirname, '/uploads') | |
)); | |
app.use(nunjucks({ | |
ext: 'html', | |
path: path.join(__dirname, 'view'), | |
nunjucksConfig: { | |
trimBlocks: true | |
} | |
})); | |
app | |
.use(router.routes()) | |
.use(router.allowedMethods()); | |
app.listen(7002, () => { | |
console.log('server run at http://127.0.0.1:7002'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment