Created
September 7, 2016 02:36
-
-
Save pveller/6f39a68e67de1161f325c66ca5b26942 to your computer and use it in GitHub Desktop.
Using Microsoft Computer Vision API to automatically tag and describe Sitecore Habitat images (http://www.pveller.com/image-tagging-automation-with-computer-vision)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty slick. Thanks for sharing.