This is the first time I'm deploying a NodeJS/ExpressJS API to Firebase functions and I'm getting this error:
Error: Unexpected end of form
at Multipart._final (/.../functions/node_modules/busboy/lib/types/multipart.js:588:17)
at callFinal (node:internal/streams/writable:696:27)
at prefinish (node:internal/streams/writable:725:7)
at finishMaybe (node:internal/streams/writable:735:5)
at Multipart.Writable.end (node:internal/streams/writable:633:5)
at onend (node:internal/streams/readable:693:10)
at processTicksAndRejections (node:internal/process/task_queues:78:11)
I'm trying to do is to send an image to the /upload
end-point which takes the photo run it through mobilenet
from Tensorflow and return the predictions array. My Code of index.js
is:
const app = express();
const storage = multer.memoryStorage();
app.use(cors());
const upload = multer({ storage });
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//#region Mobile Net Endpoint
let model;
mobilenet.load().then(loadedModel => {
model = loadedModel;
console.info('MobileNet model loaded successfully');
}).catch(error => {
console.error('Error loading MobileNet model:', error);
});
app.post('/upload', upload.single('file'), (req, res) => {
// Handle the uploaded file here
const file = req.file;
if (!file) {
return res.status(400).json({ message: 'No file uploaded' });
} else if (file) {
const readImage = () => {
const imageBuffer = file.buffer;
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}
const imageClassification = async () => {
const image = readImage();
const mobilenetModel = await mobilenet.load();
const predictions = await mobilenetModel.classify(image);
return res.json({ predictions });
// console.log('Classification Results:', predictions);
}
imageClassification().then((pred) => {
console.log("pred: ", pred);
}).catch((error) => {
console.log("error: ", error);
return res.status(400).json({ message: error });
})
}
});
//#endregion
I don't want to store the image in firebase storage or others that is why there is no implementation of that. I wanted the image to stay in tmp
folder while the predictions are being calculated by mobile net. It works on localhost
where the image is stored in my hard drive so not sure what I'm doing wrong for firebase functions here.