Skip to content

Instantly share code, notes, and snippets.

@lakshaygupta21

lakshaygupta21/server.js

Last active Aug 18, 2020
Embed
What would you like to do?
var express = require('express')
var app = express();
const Train = require("./models/Train");
const db = require('./mongoConnect.js')
const brain = require('brain.js')
const cors = require('cors')
const net = new brain.NeuralNetwork()
db()
app.use(cors());
app.get('/', async (req, res) => {
res.send('Ping-Pong')
})
app.get('/predict/:r/:g/:b', async (req, res) => {
var red = req.params.r;
var green = req.params.g;
var blue = req.params.b;
res.json({
'result': net.run([red, green, blue])[0],
'r': parseFloat(red),
'g': parseFloat(green),
'b': parseFloat(blue)
})
})
app.get('/train', async (req, res) => {
// var tg = new Train({
// input:[0,0,0],output:[1]
// })
// var ty = new Train({
// input:[1,1,1], output:[0]
// })
// await tg.save()
// await ty.save()
var t = await Train.find({}).select('-_id -__v')
net.train(t)
res.json({
'success': 'true'
});
})
app.get('/result/:r/:g/:b', async (req, res) => {
try {
var r = req.params.r;
var g = req.params.g;
var b = req.params.b;
res.json({
'result': net.run([r, g, b])[0]
})
} catch (err) {
console.log('Error')
}
})
app.get('/white/:r/:g/:b', async (req, res) => {
var r = req.params.r;
var g = req.params.g;
var b = req.params.b;
if ((r >= 0 && r <= 1) && (g >= 0 && g <= 1) && (b >= 0 && b <= 1)) {
var t = new Train({
input: [r, g, b],
output: [1]
})
await t.save();
res.json({
r: Math.random(),
g: Math.random(),
b: Math.random(),
result: net.run([r, g, b])[0]
})
} else {
res.json({
'success': 'failed'
})
}
})
try {
app.get('/black/:r/:g/:b', async (req, res) => {
var r = req.params.r;
var g = req.params.g;
var b = req.params.b;
if ((r >= 0 && r <= 1) && (g >= 0 && g <= 1) && (b >= 0 && b <= 1)) {
var t = new Train({
input: [r, g, b],
output: [0]
})
await t.save();
res.json({
r: Math.random(),
g: Math.random(),
b: Math.random(),
result: net.run([r, g, b])[0]
})
} else {
res.json({
'success': 'failed'
})
}
})
} catch (err) {
}
app.listen(process.env.PORT || 4000, function() {
console.log('Started')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.