Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Last active March 18, 2023 23:17
Show Gist options
  • Save SKaplanOfficial/b978fab3724bd9f3b5b2f4942ff12ccf to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/b978fab3724bd9f3b5b2f4942ff12ccf to your computer and use it in GitHub Desktop.
AppleScriptObjC script to extract labels of animals within images using the Vision framework.
use framework "Vision"
on getImageAnimals(imagePath)
-- Get image content
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:imagePath
-- Set up request handler using image's raw data
set requestHandler to current application's VNImageRequestHandler's alloc()'s initWithData:(theImage's TIFFRepresentation()) options:(current application's NSDictionary's alloc()'s init())
-- Initialize text request
set theRequest to current application's VNRecognizeAnimalsRequest's alloc()'s init()
-- Perform the request and get the results
requestHandler's performRequests:(current application's NSArray's arrayWithObject:(theRequest)) |error|:(missing value)
set theResults to theRequest's results()
-- Obtain and return the string labels of the results
set theAnimals to ""
repeat with observation in theResults
repeat with label in (observation's labels())
set theAnimals to (theAnimals & label's identifier as text) & ", "
end repeat
end repeat
return text 1 thru -3 of theAnimals
end getImageAnimals
return getImageAnimals("/Users/exampleUser/Documents/animals.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment