Can Bing Visual Search API process multiple images in a single transaction?

61 Views Asked by At

I have an application that fetches frames from a video, which are numerous. I am considering the s9 tier for the Bing Visual Search API, but it costs $15 per 1000 transactions. If each image counts as one transaction, processing a single video could cost me $15.

The below code shows how I'm currently using the free tier and getting results as expected but since it offers only 3 transactions/second (which is very low) - the results are very limited.

Hence, I wanted to upgrade to the s9 tier. I want to send all frames (min 200) and fetch Bing results from all.

Is there any way to have multiple images processed as a single transaction?

exports.analyzeResubmittedFramesAndReturnWebSearchResults = 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 frameAnalysisResult
}

async function analyzeSingleFrame(request) {
    try {
    // Extract subscription key from configuration
    const subscriptionKey = bingSearchConfig.subscriptionKey;

    // Create a new FormData object
    const form = new FormData();

    let imageData;

    if (request.includes("http")) {
        // Fetch image content from the URL
        const imageResponse = await axios.get(request, { responseType: 'stream' });
        imageData = imageResponse.data;
    } else {
        // Read image content from the local file
        const filePath = path.resolve(request);
        imageData = fs.createReadStream(filePath);
    }

    // Determine content type based on the file extension
    const contentType = 'image/jpeg'; // Example content type, you should determine this dynamically based on the file extension

    // Append the image data to the FormData object with the Content-Disposition header
    form.append('image', imageData, {
      filename: 'image.jpg', // Set the filename parameter to the desired name
      contentType: contentType // Set the contentType parameter to the content type of the image
    });

    // Define the request options
    const requestOptions = {
        method: 'POST',
        url: appConstants.bingWebSearchEndPoint,
        headers: {
        'Ocp-Apim-Subscription-Key': subscriptionKey,
        ...form.getHeaders() // Add form headers
        },
        data: form
    };

    // Send the request using axios
    const response = await axios(requestOptions);

    const hostPageData = [];

    // Iterate through tags
    response.data.tags.forEach(tag => {
        // Iterate through actions within each tag
        tag.actions.forEach(action => {
            // Check if actionType is "PagesIncluding"
            if (action.actionType === "PagesIncluding") {
                // Extract hostPageUrl and push it to the array
                const pagesIncludingData = action.data.value;
                pagesIncludingData.forEach(pageData => {
                    hostPageData.push(pageData);
                });
            }
        });
    });

    return { frameAnalysis: hostPageData, frame: request };
    } catch (error) {
        console.error('Error analyzing single frame:', error);
        return { frameAnalysis: {}, frame: request };
    }
}
0

There are 0 best solutions below