forked from legendhimself/SafeSearch-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.ts
33 lines (28 loc) · 1.06 KB
/
google.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { ImageAnnotatorClient } from '@google-cloud/vision';
import { fetchBuffer } from '@libs';
// refer https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#likelihood
const annotations = ['VERY_LIKELY', 'LIKELY', 'POSSIBLE'];
/**
* Validate image using AWS Rekognition
* @param imageUrl - image URL
* @returns if the image is unsafe and the key
*/
export const validateImage = async (imageUrl: string) => {
const splitted = imageUrl.split('/');
const imageName = splitted[splitted.length - 1];
const buffer = await fetchBuffer(imageUrl);
const client = new ImageAnnotatorClient({
keyFile: process.env.credFileName,
});
const [result] = await client.safeSearchDetection(buffer);
const detections = result.safeSearchAnnotation;
const unsafe = annotations.some(
(annotation) =>
detections?.adult === annotation ||
detections?.medical === annotation ||
detections?.spoof === annotation ||
detections?.violence === annotation ||
detections?.racy === annotation,
);
return { unsafe, key: imageName };
};