Skip to content

Commit 3635816

Browse files
author
Guvanch Nurgeldiyev
committed
Create An API for Image Object Detection
0 parents  commit 3635816

File tree

18 files changed

+5414
-0
lines changed

18 files changed

+5414
-0
lines changed

.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NODE_ENV=production
2+
HOST=localhost
3+
PORT=3000

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
node_modules/
3+
*.env

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Guvanch Nurgeldiyev <guvanch@nurgeldiyev.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Building a Object Detection API with Tensorflow.js & COCO SSD pretrained model
2+
3+
Basic API for posting images and getting the result of detection.
4+
Uses COCO SSD pretrained model to detect objects in 300x300 px images. List of object [names](https://github.com/tensorflow/tfjs-models/blob/master/coco-ssd/src/classes.ts)
5+
6+
## Getting Started
7+
8+
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
9+
10+
### Prerequisites
11+
12+
- Installed local [Node.js](https://nodejs.org/) environment
13+
- Package manager to install packages. [Yarn](https://yarnpkg.com/) or [npm](https://www.npmjs.com/)
14+
15+
Clone the repo in your environment
16+
17+
```bash
18+
git clone https://github.com/gnurgeldiyev/object-detection-api.git
19+
```
20+
21+
### Installing
22+
23+
```bash
24+
# move into project folder
25+
cd object-detection-api
26+
27+
# install the dependencies
28+
yarn install
29+
30+
# rename the .env.sample
31+
mv .env.sample .env
32+
33+
# add your variables
34+
nano .env
35+
```
36+
37+
#### Running the server
38+
39+
```bash
40+
yarn start
41+
```
42+
43+
## Example
44+
45+
```bash
46+
# POST - /detection
47+
curl -H "Content-Type: application/octet-stream" --data-binary "@image/bicycle 300x300.jpg" "http://localhost:3000/detection"
48+
```
49+
50+
##### Response
51+
52+
```json
53+
{
54+
"duration": 171,
55+
"result": [
56+
{
57+
"bbox": [
58+
5.610904097557068,
59+
85.95118224620819,
60+
295.6936866044998,
61+
171.45759165287018
62+
],
63+
"class": "bicycle",
64+
"score": 0.9325430393218994
65+
}
66+
]
67+
}
68+
```
69+
70+
## Running the tests
71+
72+
```bash
73+
yarn test
74+
```
75+
76+
## Built With
77+
78+
* [Express](https://github.com/expressjs/express/) - The web framework used
79+
* [Tensorflow.js](https://github.com/tensorflow/tfjs) - For using ML model
80+
* [COCO SSD](https://github.com/tensorflow/tfjs-models/tree/master/coco-ssd) - Pretrained Model for Object Detection
81+
* [Jest](https://github.com/facebook/jest) - For testing
82+
* [SuperTest](https://github.com/visionmedia/supertest) - For testing HTTP API
83+
84+
## License
85+
86+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

config/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const dotenv = require('dotenv')
2+
3+
const result = dotenv.config()
4+
if (result.error) {
5+
throw result.error
6+
}
7+
8+
module.exports = {
9+
NODE_ENV: process.env.NODE_ENV,
10+
HOST: process.env.HOST,
11+
PORT: process.env.PORT
12+
}

controller/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const Model = require('../util/model')
2+
const model = new Model()
3+
4+
/**
5+
* @returns prediction result & duration
6+
*/
7+
async function detectObjects(req, res) {
8+
const start = new Date()
9+
const rawImage = req.body
10+
let prediction = []
11+
try {
12+
prediction = await model.detect(rawImage)
13+
} catch(err) {
14+
console.log(err)
15+
return res.status(500).json({ message: 'Internal server error' })
16+
}
17+
return res.status(200).json({
18+
duration: new Date() - start,
19+
result: prediction
20+
})
21+
}
22+
23+
module.exports = {
24+
detectObjects
25+
}

image/bicycle 300x300.jpg

20.6 KB
Loading

image/door 300x300.png

141 KB
Loading

image/plant 300x300.jpeg

32.5 KB
Loading

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const app = require('./server')
2+
const { HOST, PORT } = require('./config')
3+
4+
// run the server
5+
app.listen(PORT, HOST, () => {
6+
console.log(`Server listening on http://${HOST}:${PORT}`)
7+
})

0 commit comments

Comments
 (0)