I just need to use auto captioning in cloudinary AI content analysis but no caption is added plz hel

Kesh
Kesh Member Posts: 4
edited February 20 in Developer APIs

var cloudinary = require('cloudinary').v2;

cloudinary.config({

    cloud_name: 'dlfaxu97v',

    api_key: '444767835834191',

    api_secret: 'DpC-5IFjR870n8PybNVv5q78fCw'

  });


  const uploadImage = async (imagePath) => {

    // Use the uploaded file's name as the asset's public ID and

    // allow overwriting the asset with new versions

    const options = {

        use_filename: true,

        unique_filename: false,

        overwrite: true,

        context: {

            detection: "captioning"

        },

    };

    try {

        const result = await cloudinary.uploader.upload(imagePath, options);

        console.log(result);

        return result;

    } catch (error) {

        console.error(error);

    }

};


/////////////////////////////////////

// Gets details of an uploaded image

/////////////////////////////////////

const getAssetInfo = async (publicId) => {

    // Return colors in the response

    const options = {

      colors: true,

    };

    try {

        // Get details about the asset

        const result = await cloudinary.api.resource(publicId, options);

        console.log(result);

        return result.colors;

        } catch (error) {

        console.error(error);

    }

};

const createImageTag = (publicId, ...colors) => {

    // Set the effect color and background color

    const [effectColor, backgroundColor] = colors;

    // Create an image tag with transformations applied to the src URL

    let imageTag = cloudinary.image(publicId, {

      transformation: [

        { width: 250, height: 250, gravity: 'faces', crop: 'thumb' },

        { radius: 'max' },

        { effect: 'outline:10', color: effectColor },

        { background: backgroundColor },

      ],

    });

    return imageTag;

};




(async () => {

  // Set the image to upload

  const imagePath = '../CHAPRI.jpg';

  // Upload the image

  const publicId = await uploadImage(imagePath);

 

})();

Tagged:

Comments

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 151

    Hey @Kesh. Thanks for getting in touch.

    Looking at your code, it seems you're invoking the analysis add-on incorrectly. You should specify detection directly within options rather than a parameter of context. Using context sets the contextual metadata of an asset to the value specified, so by saying detection: "captioning" , you're setting the contextual metadata with the key of "detection" to the value of "captioning".

    Rather than doing this

    const options = {
        use_filename: true,
        unique_filename: false,
        overwrite: true,
        context: {
            detection: "captioning"
        },
    };
    

    Please try this instead:

    const options = {
        use_filename: true,
        unique_filename: false,
        overwrite: true,
        detection: "captioning"
    };
    

    Also, it's very important to note that you should never share your API secret with anyone, including Cloudinary support. Anybody who gets access to your cloud name, API key and API secret can cause a lot of trouble on your account, as they're able to authenticate as you.

    For your security and peace of mind, I have deactivated the API credentials that you included in your post, and created you a new set. You can view these in Settings -> Access Keys.

    I hope this helps. Please let me know if you have any questions.

    Kind regards,

    -Danny

  • Kesh
    Kesh Member Posts: 4

    const cloudinary = require('cloudinary');


    cloudinary.config({

     });


    const uploadImage = async (imagePath) => {

      // Use the uploaded file's name as the asset's public ID and

      // allow overwriting the asset with new versions

      const options = {

        use_filename: true,

        unique_filename: false,

        overwrite: true,

        detection: "captioning"

    };

      try {

        const result = await cloudinary.v2.uploader.upload(imagePath, options);

        console.log(result);

        return result;

      } catch (error) {

        console.error(error);

      }

    };


    (async () => {

      // Set the image to upload

      const imagePath = '../cap_sport.jpg';

      // Upload the image

      const publicId = await uploadImage(imagePath);


      // Add any further processing if needed

    })();



    Now i use the same provided my you but image get uploaded in cloudinary but no automatic captions are added in it can u plz correct me??

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 151

    Sadly, the caption isn't automatically added to the asset. You would need to handle the upload response in your code and then apply the caption in your image accordingly.

    The reason we do it this way is to accommodate different workflows. Providing the caption in the response allows you to handle it however you see fit, so for some customers this would be adding the caption to a database. For others, they might import it into the CMS, and for other customers, they might want to add it as either structured or contextual metadata.

    I think the best way to handle this would be to use a notification URL to handle the upload response, letting you keep your upload logic and your captioning logic separate. Please see https://cloudinary.com/documentation/cloudinary_ai_content_analysis_addon#asynchronous_handling-1 for further information, or of course, feel free to ask any additional questions you may have

    Kind regards

    -Danny

  • Kesh
    Kesh Member Posts: 4

    Can you provide me changes in my code or different code so that I can add caption in my image using AI

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 151

    I'm afraid I'm not able to provide an exact example, but if you want to add the AI-generated caption as contextual metadata, you just need to parse the upload response and run it through the context() method of the Upload API. You would specify the public ID, resource type, and delivery type of the asset, and exactly what you would like as the contextual metadata. This section of our documentation should provide you with the means of doing this: https://cloudinary.com/documentation/image_upload_api_reference#context

    Kind regards,

    -Danny

  • carlevison
    carlevison Administrator, Cloudinary Staff Posts: 7

    Hi @Kesh - there's an example here that should help you: https://cloudinary.com/documentation/upload_parameters#on_success_update_script

    This video may also help: https://www.youtube.com/watch?v=P86l0ho0EUU.

    Both show how to use the onSuccess parameter to update your metadata with a caption.

  • Kesh
    Kesh Member Posts: 4

    Thanku it worked!!