Skip to content
August 3, 2017 / kiranpatils

Integrating Cognitive Services to set ALT Tag during Image Upload

Challenge:

It is interesting – How this idea came up. Basically, was browsing Facebook page and while doing that – My Internet was slow and during image load process — I noticed an ALT Tag — Which had something like this and when Image got loaded. It had exactly same thing!

 

While this thing got stuck somewhere in mind. Came across following videos [Which I’m sure most of you as well]:

And that revoked the thought process. Especially — For those who are not as privileged as you and me to see the world. But through technology if it helps them visualize — That will help!

Then came across this link : https://azure.microsoft.com/en-us/try/cognitive-services/ [“Microsoft Cognitive Services let you build apps with powerful algorithms using just a few lines of code. They work across devices, keep improving, and are easy to set up. Try the Cognitive Services APIs for free.”] — This is really awesome – As For me impacting human lives and making it better using technology is the only thing which drives me in to this field. I’m sure for lot of you as well!

It provides the lot of services, which I recommend you to explore. But for this blog post, We only need : https://azure.microsoft.com/en-in/services/cognitive-services/computer-vision/ – You can try it out before you use it!

Tried few things and created simple Console application as per this doc : https://azure.microsoft.com/en-in/services/cognitive-services/computer-vision/

And was impressed to see how it works — It just works! – My eyes lit up – And as you — I always think — How can I integrate this in Sitecore. And that’s how this blog post came in to this world!

https://i1.wp.com/boardofwisdom.com/cachetogo/images/quotes/524839.png

Let’s see how — I have been able to integrate it with Sitecore!

Solution:

You must have observed — Most of the site’s Media Library will have an empty ALT Tag. And it is not easy for someone to go and type such meaningful ALT Tag. Then I thought to use Cognitive Services to suggest us an ALT Tag and based on suggestion set that automatically in ALT Tag field. So, Content Authors/Editors, End users are happy – This is how it works:

 

As you do, I also did Quick search and tried to find is something similar already done. And found Mike’s blog : https://sitecorejunkie.com/2014/06/18/set-default-alternate-text-on-images-uploaded-to-the-sitecore-media-library/ – Where he is extracting item name from path and setting it as an ALT Tag from getMediaCreatorOptions pipeline.

  1. I thought to alter his logic of getting ALT Tag instead of substring making an API call and setting it — Sounds easy, right? But my dear friend life is not as easy as it seems to be — After trying this out — I found that there is no way for me to access uploaded file or uploaded media stream 😦 — I tried to some options. But with no luck
  2. So, thought to try something else — And I tried item:created, item:saving, item:saved and in each of them I had the same problem. Was unable to get Media Stream
  3. It looks like when these events are getting called — It is creating particular media item and after that BLOB gets attached – I’m assuming this. Have asked Sitecore folks to clarify.
  4. Then finally, Thought to implement : uiUpload – Usually I avoid adding anything to ui* processors. As it will only be handled in UI scenarios and not programmatically scenarios. But I had no option and then I thought of YAGNI principle — I don’t need it now as this is my MVP [Minimum Viable Product]
  5. And here’s all the code you need for that — As I mentioned this is just kind of POC – Key and URL should come from Configuration, Code needs to be ironed out. Before it goes on production — And I will leave it up to you for brevity!

Let’s do our bit to make world better than we found it – Happy Coding! 🙂

UPDATE 1 : Thanks Kamruz

After posting this — Kamruz mentioned something similar folks from Community has already done. It looks like I need to sharpen my Google Search Skills – It would have save sometime. But on a +ve side, I have been able to learn and share

  1. https://www.markstiles.net/blog/2017/2/22/sitecore-cognitive-services/Mark
  2. https://marketplace.sitecore.net/en/Modules/A/Alt_Tag_Generator.aspxTom
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: