Google Cloud Vision - Does an Image batch annotation request count as one transaction or as many images as in the batch?

32 views Asked by At

I have an application that retrieves frames from a video, which are numerous. I want to use batch image annotation to optimize performance, but will it be cost-effective too? A batch image annotation http request = 1 transaction or No. of images in the batch?

I am currently using the google cloud vision web detection feature that sends individual api requests for all images and it works fine for me. But I'm looking for a more cost-effective option.

exports.analyzemultipleFrames = async fileList => {
    let cloudVisionFrames = [];

    const filteredFramesToVision = fileList.filter((file, index) => {
        let filterFrame = false;
        if (index % appConstants.frameCountAnalyse == 0) {
            filterFrame = true;
        }
        return filterFrame;
    });
    const analyzeFramesPromise = await filteredFramesToVision.map(async file => {
        const imageAnalysisout = await analyzeFrame(file.fullPath);
        cloudVisionFrames.push(file.fileName);
        return imageAnalysisout;
    });

    const frameAnalysisResult = await Promise.all(analyzeFramesPromise);

    return {
        cloudVisionFrames: cloudVisionFrames,
        frameAnalysisResult: frameAnalysisResult
    };
};

exports.analyzeResubmittedFrames = async (framesArr) => {
    let cloudVisionFrames = [];
    const analyzeFramesPromise = await framesArr.map(async file => {
        const imageAnalysisout = await analyzeSingleFrame(file);
        cloudVisionFrames.push(file);
        return imageAnalysisout;
    });

    const frameAnalysisResult = await Promise.all(analyzeFramesPromise);

    return {
        cloudVisionFrames: cloudVisionFrames,
        frameAnalysisResult: frameAnalysisResult
    };
}

async function analyzeFrame(file) {
    // eslint-disable-next-line no-async-promise-executor
    return new Promise(async resolve => {
        fs.readFile(file, async (err, data) => {
            if (err) {
                resolve({ frameAnalysis: {}, frame: file });
            }
            let base64String = Buffer.from(data).toString("base64");
            let request = {
                image: { content: base64String },
                features: appConstants.cloudVisionFeatures
            };

            try {
                const [result] = await visionAnnotateClient.annotateImage(request);
                resolve({ frameAnalysis: result, frame: file });
            } catch (err) {
                logger("Vision Error" + err);
                resolve({ frameAnalysis: {}, frame: file });
            }
            //implement google cloud vision logic to getch files
        });
    });
}

async function analyzeSingleFrame(file) {
    // eslint-disable-next-line no-async-promise-executor
    return new Promise(async resolve => {
        let request = {
            image: { source: { imageUri: file } },
            features: appConstants.cloudVisionFeatures
        };

        try {
            const [result] = await visionAnnotateClient.annotateImage(request);
            resolve({ frameAnalysis: result, frame: file });
        } catch (err) {
            logger("Single Frame Vision ERROR" + err);
            resolve({ frameAnalysis: {}, frame: file });
        }
        //implement google cloud vision logic to getch files
    });
};

1

There are 1 answers

0
Brendan On

Every image sent to the API is charged.