Skip to content

Commit 4557e39

Browse files
Initial commit
0 parents  commit 4557e39

File tree

8 files changed

+2825
-0
lines changed

8 files changed

+2825
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

app.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const express = require('express');
2+
const logger = require('morgan');
3+
const cors = require('cors');
4+
const app = express();
5+
6+
const corsOptions = {
7+
origin: '*',
8+
};
9+
10+
app.use(logger('dev'));
11+
app.use(express.json());
12+
app.use(cors(corsOptions));
13+
app.use(express.urlencoded({ extended: false }));
14+
15+
app.get('/', (req, res) => {
16+
res.send('Image Classifier API')
17+
});
18+
19+
const predictRouter = require('./routes/predict.route')
20+
app.use('/predict', predictRouter);
21+
PORT = process.env.PORT || 3000;
22+
app.listen(PORT);
23+
24+
console.log(`Running server at http://localhost:${PORT}`);

controllers/predict.controllers.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const tf = require('@tensorflow/tfjs');
2+
const mobilenet = require('@tensorflow-models/mobilenet');
3+
const image = require('get-image-data');
4+
const fs = require('fs');
5+
//loading
6+
exports.loadmodel = async function MobilenetLoad(){
7+
console.log('Loading.......')
8+
const model = await mobilenet.load();
9+
};
10+
11+
exports.makePredictions = async (req, res, next) => {
12+
const imagePath = './images/test-image.jpg';
13+
try {
14+
const loadModel = async (img) => {
15+
const output = {};
16+
// classify
17+
console.log("Predicting...");
18+
output.predictions = await model.classify(img);
19+
console.log(output);
20+
res.statusCode = 200;
21+
res.json(output);
22+
};
23+
await image(imagePath, async (err, imageData) => {
24+
// pre-process image
25+
const numChannels = 3;
26+
const numPixels = imageData.width * imageData.height;
27+
const values = new Int32Array(numPixels * numChannels);
28+
const pixels = imageData.data;
29+
for (let i = 0; i < numPixels; i++) {
30+
for (let channel = 0; channel < numChannels; ++channel) {
31+
values[i * numChannels + channel] = pixels[i * 4 + channel];
32+
}
33+
}
34+
const outShape = [imageData.height, imageData.width, numChannels];
35+
const input = tf.tensor3d(values, outShape, 'int32');
36+
await loadModel(input);
37+
// delete image file
38+
fs.unlinkSync(imagePath, (error) => {
39+
if (error) {
40+
console.error(error);
41+
}
42+
});
43+
});
44+
} catch (error) {
45+
console.log(error)
46+
}
47+
};

index.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
<form name="myForm" method="post" enctype="multipart/form-data">
5+
Select Image: <input type='file' name='file' id='image-upload'>
6+
<button type='submit' id="submit-btn">Submit</button>
7+
</form>
8+
<div id="result"></div>
9+
<script>
10+
const button = document.querySelector('#submit-btn')
11+
button.addEventListener('click', async (e) => {
12+
e.preventDefault();
13+
const URL = 'https://image-clf-api.herokuapp.com/predict'
14+
const image = document.querySelector('#image-upload');
15+
const resultDiv = document.querySelector('#result')
16+
const formData = new FormData();
17+
formData.append("file", image.files[0]);
18+
const response = await fetch(URL, {
19+
method: 'POST',
20+
body: formData
21+
});
22+
const result = await response.json();
23+
resultDiv.innerHTML = JSON.stringify(result.predictions);
24+
console.log(result);
25+
});
26+
</script>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)