This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.js
More file actions
132 lines (104 loc) · 3.47 KB
/
Copy pathclassify.js
File metadata and controls
132 lines (104 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//TensorFlow.js is an open-source hardware-accelerated JavaScript library
//for training and deploying machine learning models.
const tf = require('@tensorflow/tfjs');
//MobileNet : pre-trained model for TensorFlow.js
const mobilenet = require('@tensorflow-models/mobilenet');
//The module provides native TensorFlow execution
//in backend JavaScript applications under the Node.js runtime.
const tfnode = require('@tensorflow/tfjs-node');
//The fs module provides an API for interacting with the file system.
const fs = require('fs');
//use path module
const path = require('path');
//use express module
const express = require('express');
//use hbs view engine
const hbs = require('hbs');
//use bodyParser middleware
const bodyParser = require('body-parser');
//express constructor initialized
const app = express();
//multer for upload of file
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/imgs');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage
});
var product_img='';
// To be understood by yourself
const readImage = path => {
//reads the entire contents of a file.
//readFileSync() is synchronous and blocks execution until finished.
const imageBuffer = fs.readFileSync(path);
//Given the encoded bytes of an image,
//it returns a 3D or 4D tensor of the decoded image. Supports BMP, GIF, JPEG and PNG formats.
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}
// To be understood by yourself
const imageClassification = async path => {
const image = readImage(path);
// Load the model.
const mobilenetModel = await mobilenet.load();
// Classify the image.
const predictions = await mobilenetModel.classify(image);
console.log('Classification Results:', predictions);
return predictions
}
//if (process.argv.length !== 3) throw new Error('Incorrect arguments: node classify.js <IMAGE_FILE>');
//set views file
app.set('views', path.join(__dirname, 'views'));
//set view engine
app.set('view engine', 'hbs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
//set folder public as static folder for static file
//app.use('/assets', express.static(__dirname + '/public'));
app.use(express.static('public'));
//Route for login and authentication
app.get('/', (req, res) => {
res.render('classify.hbs');
});
//route for homepage
app.get('/classify',async (req, res) => {
const predictions=await imageClassification('/home/sadaqatali/Documents/AP_Presentation/node-image-classification-master/public'+product_img);
if(predictions){
var results={
product_img:product_img,
class:predictions[0].className,
probability: Number((predictions[0].probability * 100).toFixed(1))
}
// console.log(results.product_img);
res.render('classify.hbs',{
results: results
});
// console.log(results.object);
}else{
console.log('none');
}
});
//route for insert data
app.post('/upload', upload.single('object_img'), (req, res) => {
//var product_img= '/assets/imgs/' + req.file.originalname;
product_img= '/imgs/' + req.file.originalname;
var results={
product_img:product_img
}
console.log(results);
res.render('classify.hbs',{
results: results
});
});
//server listening
app.listen(8000, () => {
console.log('Server is running at port 8000');
});