├── .gitattributes
├── .gitignore
├── README.md
├── classify.js
├── koala.jpg
├── package.json
└── rabbit.jpg
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | lerna-debug.log*
8 |
9 | # Diagnostic reports (https://nodejs.org/api/report.html)
10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11 |
12 | # Runtime data
13 | pids
14 | *.pid
15 | *.seed
16 | *.pid.lock
17 |
18 | # Directory for instrumented libs generated by jscoverage/JSCover
19 | lib-cov
20 |
21 | # Coverage directory used by tools like istanbul
22 | coverage
23 | *.lcov
24 |
25 | # nyc test coverage
26 | .nyc_output
27 |
28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29 | .grunt
30 |
31 | # Bower dependency directory (https://bower.io/)
32 | bower_components
33 |
34 | # node-waf configuration
35 | .lock-wscript
36 |
37 | # Compiled binary addons (https://nodejs.org/api/addons.html)
38 | build/Release
39 |
40 | # Dependency directories
41 | node_modules/
42 | jspm_packages/
43 |
44 | # TypeScript v1 declaration files
45 | typings/
46 |
47 | # TypeScript cache
48 | *.tsbuildinfo
49 |
50 | # Optional npm cache directory
51 | .npm
52 |
53 | # Optional eslint cache
54 | .eslintcache
55 |
56 | # Optional REPL history
57 | .node_repl_history
58 |
59 | # Output of 'npm pack'
60 | *.tgz
61 |
62 | # Yarn Integrity file
63 | .yarn-integrity
64 |
65 | # dotenv environment variables file
66 | .env
67 | .env.test
68 |
69 | # parcel-bundler cache (https://parceljs.org/)
70 | .cache
71 |
72 | # next.js build output
73 | .next
74 |
75 | # nuxt.js build output
76 | .nuxt
77 |
78 | # vuepress build output
79 | .vuepress/dist
80 |
81 | # Serverless directories
82 | .serverless/
83 |
84 | # FuseBox cache
85 | .fusebox/
86 |
87 | # DynamoDB Local files
88 | .dynamodb/
89 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Image Classification : Machine Learning in Node.js with TensorFlow.js
2 | A simple example on Image Classification in Node.js with TensorFlow.js
3 |
4 | #### Check out the [Medium Post](https://medium.com/@happytejasrathod/image-classification-machine-learning-in-node-js-with-tensorflow-js-dd8e20ba5024) for detailed explanation.
5 |
6 | ## Getting Started
7 | With current Machine Learning Tutorials running towards Python and TensorFlow, I was finding a route in a language which is close to my work, as it has so much use in life than other languages, the language I am talking about is JavaScript. From Back-End Servers (Node.js) to Front-End Web (React) and even Mobile-Platforms (React-Native), JavaScript is a language that is being used by many coding geeks and to learning a whole new language, just for machine learning is tiresome for any programmer. So I thought to bring machine learning to my own space and my language.
8 |
9 | >Sure, it's a challenge but why not to take a different path.
10 |
11 | ## Installation
12 |
13 | Open it in GitHub Desktop or git clone the repository or download the zip & extract the project.
14 |
15 | Open the terminal in the project folder and type `npm install`.
16 |
17 | ## Testing
18 |
19 | Go to the project folder, and Open the Terminal to that path.
20 |
21 | I have added an image that I have clicked and uploaded it in the repository. It’s an image of a Cute Baby Rabbit. You can use any image you have, just paste it in the project folder and give the name as the argument.
22 |
23 |
24 |
25 | - Run the file **_(classify.js)_** with an image file as an argument.
26 |
27 | ```
28 | node classify.js rabbit.jpg
29 | ```
30 |
31 | - The result will be shown in the Terminal as :
32 | ```
33 | Classification Results: [
34 | {
35 | className: 'Angora, Angora rabbit',
36 | probability: 0.9488762617111206
37 | },
38 | { className: 'hamster', probability: 0.023957064375281334 },
39 | {
40 | className: 'guinea pig, Cavia cobaya',
41 | probability: 0.016880817711353302
42 | }
43 | ]
44 | ```
45 |
46 | ### Thank you
47 | - Give a star if you find this repository helpful.
48 | ## Author
49 |
50 | * **Tejas Rathod** - [@tejas77](https://github.com/tejas77)
51 |
--------------------------------------------------------------------------------
/classify.js:
--------------------------------------------------------------------------------
1 | //TensorFlow.js is an open-source hardware-accelerated JavaScript library
2 | //for training and deploying machine learning models.
3 | const tf = require('@tensorflow/tfjs');
4 | //MobileNet : pre-trained model for TensorFlow.js
5 | const mobilenet = require('@tensorflow-models/mobilenet');
6 | //The module provides native TensorFlow execution
7 | //in backend JavaScript applications under the Node.js runtime.
8 | const tfnode = require('@tensorflow/tfjs-node');
9 | //The fs module provides an API for interacting with the file system.
10 | const fs = require('fs');
11 |
12 | const readImage = path => {
13 | //reads the entire contents of a file.
14 | //readFileSync() is synchronous and blocks execution until finished.
15 | const imageBuffer = fs.readFileSync(path);
16 | //Given the encoded bytes of an image,
17 | //it returns a 3D or 4D tensor of the decoded image. Supports BMP, GIF, JPEG and PNG formats.
18 | const tfimage = tfnode.node.decodeImage(imageBuffer);
19 | return tfimage;
20 | }
21 |
22 | const imageClassification = async path => {
23 | const image = readImage(path);
24 | // Load the model.
25 | const mobilenetModel = await mobilenet.load();
26 | // Classify the image.
27 | const predictions = await mobilenetModel.classify(image);
28 | console.log('Classification Results:', predictions);
29 | }
30 |
31 | if (process.argv.length !== 3) throw new Error('Incorrect arguments: node classify.js ');
32 |
33 | imageClassification(process.argv[2]);
34 |
--------------------------------------------------------------------------------
/koala.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tejas77/node-image-classification/4af657f5d0519c596a8337efc7555ee48e394363/koala.jpg
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "image-classification",
3 | "version": "1.0.0",
4 | "description": "A simple example on Image Classification in Node.js with TensorFlow.js",
5 | "main": "classify.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "author": "Tejas Rathod",
10 | "license": "ISC",
11 | "dependencies": {
12 | "@tensorflow-models/mobilenet": "^2.0.4",
13 | "@tensorflow/tfjs": "^1.2.10",
14 | "@tensorflow/tfjs-node": "^1.2.10"
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/rabbit.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tejas77/node-image-classification/4af657f5d0519c596a8337efc7555ee48e394363/rabbit.jpg
--------------------------------------------------------------------------------