codelab-friendlychat-web
codelab-friendlychat-web copied to clipboard
Step 10 : Error: No image present. at _coerceRequest (/workspace/node_modules/@google-cloud/vision/build/src/helpers.js:54:25)
Problem The code lab explanation uses "@google-cloud/[email protected]", but the latest version of cloud vision api was 2.3.0
I proceeded as "https://codelabs.developers.google.com/codelabs/firebase-cloud-functions" explains, but I got an error below.
Step 10 : Error: No image present. at _coerceRequest (/workspace/node_modules/@google-cloud/vision/build/src/helpers.js:54:25)
how to resolve Here's are the code I modified and I could finally got it!
Comment out line is the github code. Not comment out line is the alternate code I wrote.
// const Vision = require("@google-cloud/vision");
// const vision = new Vision();
const vision = require("@google-cloud/vision");
const visionClient = new vision.ImageAnnotatorClient();
// const image = {
// source: { imageUri: `gs://${object.bucket}/${object.name}` },
// };
const request = {
image: {
source: { imageUri: `gs://${object.bucket}/${object.name}` },
},
};
// const batchAnnotateImagesResponse = await vision.safeSearchDetection(
// image
// );
// const safeSearchResult =
// batchAnnotateImagesResponse[0].safeSearchAnnotation;
// const Likelihood = visionClient.types.Likelihood;
// if (
// Likelihood[safeSearchResult.adult] >= Likelihood.LIKELY ||
// Likelihood[safeSearchResult.violence] >= Likelihood.LIKELY
// ) {
// console.log(
// "The image",
// object.name,
// "has been detected as inappropriate."
// );
// return blurImage(object.name);
// }
// console.log("The image", object.name, "has been detected as OK.");
visionClient
.safeSearchDetection(request)
.then((batchAnnotateImagesResponse) => {
safeSearchResult = batchAnnotateImagesResponse[0].safeSearchAnnotation;
const Likelihood = {
UNKNOWN: 0,
VERY_UNLIKELY: 1,
UNLIKELY: 2,
POSSIBLE: 3,
LIKELY: 4,
VERY_LIKELY: 5,
};
if (
Likelihood[safeSearchResult.adult] >= Likelihood.LIKELY ||
Likelihood[safeSearchResult.violence] >= Likelihood.LIKELY
) {
console.log(
"The image",
object.name,
"has been detected as inappropriate."
);
return blurImage(object.name);
}
console.log("The image", object.name, "has been detected as OK.");
})
.catch((err) => {
console.error(err);
});
I hope this help someone like me who will almost spend a lot of time to resolve this problem.
This definitely helped. Tutorials should always try to use the latest versions. Just a few things to add:
- This issue should be for Step 9 (at the time I'm writing this).
- You don't need to modify the safeSearchDetection function as an arrow function.
Here's the most basic fix imo:
//const Vision = require('@google-cloud/vision');
//const vision = new Vision();
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
...
//const image = {
// source: {imageUri: `gs://${object.bucket}/${object.name}`},
//};
const request = {
image: {
source: { imageUri: `gs://${object.bucket}/${object.name}` },
},
};
...
//const batchAnnotateImagesResponse = await vision.safeSearchDetection(image);
const batchAnnotateImagesResponse = await visionClient.safeSearchDetection(request);
const safeSearchResult = batchAnnotateImagesResponse[0].safeSearchAnnotation;
// const Likelihood = vision.types.Likelihood;
const Likelihood = {
UNKNOWN: 0,
VERY_UNLIKELY: 1,
UNLIKELY: 2,
POSSIBLE: 3,
LIKELY: 4,
VERY_LIKELY: 5,
};
Sources: https://cloud.google.com/vision/docs/detecting-safe-search https://github.com/googleapis/nodejs-vision/issues/17