· 4 years ago · Mar 17, 2021, 12:50 PM
1const apiKey = 'AIzaSyDIZNFlrYZw65EJjNnGrRcgSHAtUBgz-PM'; // <-- use your API key here
2const {GoogleAuth, grpc} = require('google-gax');
3const vision = require('@google-cloud/vision');
4
5
6function getApiKeyCredentials() {
7 const sslCreds = grpc.credentials.createSsl();
8 const googleAuth = new GoogleAuth();
9 const authClient = googleAuth.fromAPIKey(apiKey);
10 const credentials = grpc.credentials.combineChannelCredentials(
11 sslCreds,
12 grpc.credentials.createFromGoogleCredential(authClient)
13 );
14 return credentials;
15}
16
17
18(async () => {
19
20const sslCreds = getApiKeyCredentials();
21
22console.log(sslCreds)
23const client = new vision.ImageAnnotatorClient({sslCreds});
24//const client = new vision.ImageAnnotatorClient();
25
26const [result] = await client.safeSearchDetection('/Users/pshon/1.png');
27const detections = result.safeSearchAnnotation;
28console.log('Safe search:');
29console.log(`Adult: ${detections.adult}`);
30console.log(`Medical: ${detections.medical}`);
31console.log(`Spoof: ${detections.spoof}`);
32console.log(`Violence: ${detections.violence}`);
33console.log(`Racy: ${detections.racy}`);
34})()