Forked from PrateekKumarSingh/BingImageSearch.ps1
Created
September 29, 2018 19:15
-
-
Save johndonnelly/947487eb8a2548afdb5bbb1c46f5b710 to your computer and use it in GitHub Desktop.
This file contains 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
# installation | |
Install-Module PSCognitiveService -Force -Scope CurrentUser -Verbose | |
Import-Module PSCognitiveService -Force -Verbose | |
# create AzureRM Cognitive Service subscription | |
New-CognitiveServiceAccount -AccountType Face -ResourceGroupName RG1 -Location southeastasia -SKUName F0 -Verbose | Out-Null | |
New-CognitiveServiceAccount -AccountType ComputerVision -ResourceGroupName RG1 -Location southeastasia -SKUName F0 -Verbose | Out-Null | |
# create AzureRM Cognitive Service subscription when you're unsure of resource groups, price tier and location | |
New-CognitiveServiceAccount -AccountType Bing.Search.v7 -Verbose | |
# login and obtain subscription keys, configure them locally | |
New-LocalConfiguration -FromAzure -AddKeysToProfile -Verbose | Out-Null | |
# search images on web using 'Bing' API | |
$images = (Search-Image -Text 'jim carrey' -Count 20 -SafeSearch strict -Verbose).value.contenturl | |
$images | ForEach-Object { | |
try{ | |
# capture emotions each image | |
$happiness = (Get-Face -URL $_).faceattributes.emotion.happiness | |
if($happiness -gt .90){ # filter out happy images | |
# analyze image and get a caption | |
$caption = (Get-ImageAnalysis -URL $_).description.captions.text | |
# creates a logical file name | |
$filename = if($caption){$caption}else{'unnamed'} | |
$path = "c:\temp\$filename.jpg" | |
$i = 1 | |
while(Test-Path $path){ | |
if($filename -like "*(*)*"){ | |
$filename = $($filename[0..$($filename.length-4)] -join '') +"($i)" | |
} | |
else{ $filename = $filename+"($i)" } | |
$path = "c:\temp\$filename.jpg"; $i++ | |
} | |
Write-Host "Downloading image as: $path" -ForegroundColor Cyan | |
Invoke-WebRequest "$_" -OutFile $path # download the images | |
} | |
} | |
catch{ | |
$_.exception.message | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment