Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why numchannels is defined as 3, why use Int32Array #476

Open
DevTheKing opened this issue Feb 15, 2021 · 2 comments
Open

why numchannels is defined as 3, why use Int32Array #476

DevTheKing opened this issue Feb 15, 2021 · 2 comments
Labels
question Further information is requested

Comments

@DevTheKing
Copy link

  1. why numchannels is defined as 3, why use Int32Array ??
  2. what is the meaning of this line and why there is a constant value 4 ?
  3. whats is req.file.buffer ?
@GantMan
Copy link
Member

GantMan commented Feb 15, 2021

Which file are you looking at? I assume a demo?

@GantMan GantMan added the question Further information is requested label Feb 15, 2021
@DevTheKing
Copy link
Author

on this code for node js @GantMan

const express = require('express')
const multer = require('multer')
const jpeg = require('jpeg-js')

const tf = require('@tensorflow/tfjs-node')
const nsfw = require('nsfwjs')

const app = express()
const upload = multer()

let _model

const convert = async (img) => {
// Decoded image in UInt8 Byte array
const image = await jpeg.decode(img, true)

const numChannels = 3
const numPixels = image.width * image.height
const values = new Int32Array(numPixels * numChannels)

for (let i = 0; i < numPixels; i++)
for (let c = 0; c < numChannels; ++c)
values[i * numChannels + c] = image.data[i * 4 + c]

return tf.tensor3d(values, [image.height, image.width, numChannels], 'int32')
}

app.post('/nsfw', upload.single('image'), async (req, res) => {
if (!req.file) res.status(400).send('Missing image multipart/form-data')
else {
const image = await convert(req.file.buffer)
const predictions = await _model.classify(image)
image.dispose()
res.json(predictions)
}
})

const load_model = async () => {
_model = await nsfw.load()
}

// Keep the model in memory, make sure it's loaded only once
load_model().then(() => app.listen(8080))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants