Created
October 17, 2018 04:42
-
-
Save dmennis/4b471657170e28d3f747c3fb30bcace4 to your computer and use it in GitHub Desktop.
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
rekognitionObject?.recognizeCelebrities(celebRequest!){ | |
(result, error) in | |
if error != nil{ | |
print(error!) | |
return | |
} | |
//1. First we check if there are any celebrities in the response | |
if ((result!.celebrityFaces?.count)! > 0){ | |
//2. Celebrities were found. Lets iterate through all of them | |
for (index, celebFace) in result!.celebrityFaces!.enumerated(){ | |
//Check the confidence value returned by the API for each celebirty identified | |
if(celebFace.matchConfidence!.intValue > 50){ //Adjust the confidence value to whatever you are comfortable with | |
//We are confident this is celebrity. Lets point them out in the image using the main thread | |
DispatchQueue.main.async { | |
[weak self] in | |
//Create an instance of Celebrity. This class is availabe with the starter application you downloaded | |
let celebrityInImage = Celebrity() | |
celebrityInImage.scene = (self?.CelebImageView)! | |
//Get the coordinates for where this celebrity face is in the image and pass them to the Celebrity instance | |
celebrityInImage.boundingBox = ["height":celebFace.face?.boundingBox?.height, "left":celebFace.face?.boundingBox?.left, "top":celebFace.face?.boundingBox?.top, "width":celebFace.face?.boundingBox?.width] as! [String : CGFloat] | |
//Get the celebrity name and pass it along | |
celebrityInImage.name = celebFace.name! | |
//Get the first url returned by the API for this celebrity. This is going to be an IMDb profile link | |
if (celebFace.urls!.count > 0){ | |
celebrityInImage.infoLink = celebFace.urls![0] | |
} | |
//If there are no links direct them to IMDB search page | |
else{ | |
celebrityInImage.infoLink = "https://www.imdb.com/search/name-text?bio="+celebrityInImage.name | |
} | |
//Update the celebrity links map that we will use next to create buttons | |
self?.infoLinksMap[index] = "https://"+celebFace.urls![0] | |
//Create a button that will take users to the IMDb link when tapped | |
let infoButton:UIButton = celebrityInImage.createInfoButton() | |
infoButton.tag = index | |
infoButton.addTarget(self, action: #selector(self?.handleTap), for: UIControlEvents.touchUpInside) | |
self?.CelebImageView.addSubview(infoButton) | |
} | |
} | |
} | |
} | |
//If there were no celebrities in the image, lets check if there were any faces (who, granted, could one day become celebrities) | |
else if ((result!.unrecognizedFaces?.count)! > 0){ | |
//Faces are present. Point them out in the Image (left as an exercise for the reader) | |
/**/ | |
} | |
else{ | |
//No faces were found (presumably no people were found either) | |
print("No faces in this pic") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment