Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Microsoft Computer Vision API to automatically tag and describe Sitecore Habitat images (http://www.pveller.com/image-tagging-automation-with-computer-vision)
$vision = 'https://api.projectoxford.ai/vision/v1.0/analyze'
$features = 'Categories,Tags,Description,Color'
$images = Get-ChildItem -Path '/sitecore/media library/Habitat/Images/Square'
$result = @()
foreach ($image in $images)
{
# Reading the binary of the image so that we could send it to the Computer Vision API
$media_url = New-Object 'Sitecore.Resources.Media.MediaUri' $image
$media = [Sitecore.Resources.Media.MediaManager]::GetMedia($media_url)
$stream = $media.GetStream().Stream
$memory = New-Object 'System.IO.MemoryStream'
$stream.CopyTo($memory)
$bytes = $memory.ToArray()
$memory.Dispose()
$stream.Dispose()
$success = $False
do
{
try
{
# Calling the Computer Vision API
$response = Invoke-WebRequest `
-Uri "$($vision)?visualFeatures=$($features)" `
-Body $bytes `
-ContentType "application/octet-stream" `
-Headers @{'Ocp-Apim-Subscription-Key' = '<use-your-key>'} `
-Method 'Post' `
-ErrorAction Stop `
-UseBasicParsing | ConvertFrom-Json
# Tracking what image the response should be attached to
$response | Add-Member -Name "id" `
-MemberType NoteProperty `
-Value "$($image.ID.ToShortID())"
Write-Host "Processed $($image.Paths.FullPath)"
# Recording the tags and caption on the image item
$image.Alt = $response.description.captions[0].text
$image.Title = $response.description.captions[0].text
$image.Keywords = $response.description.tags -join ','
# $response.categories
# $response.tags
# $response.description.tags
# $response.description.captions
$result += $response
$success = $True
}
catch [System.Net.WebException]
{
Write-Host "Exceeded the limit. Need to wait one minute..."
Start-Sleep -s 60
Write-Host "Resuming now..."
}
}
while ($success -ne $True)
}
Write-Host ($result | ConvertTo-Json -Depth 5)
@michaellwest

This comment has been minimized.

Copy link

commented Sep 7, 2016

Pretty slick. Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.