Last active
March 18, 2023 23:17
-
-
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.
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
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