Skip to content

Instantly share code, notes, and snippets.

@mobilequickie
Created September 20, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mobilequickie/0c62253264edd63f26c7fa9a04ff280b to your computer and use it in GitHub Desktop.
Save mobilequickie/0c62253264edd63f26c7fa9a04ff280b to your computer and use it in GitHub Desktop.
//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 a search page
else{
celebrityInImage.infoLink = "www.google.com/search?q="+celebrityInImage.name
}
//Update the celebrity links map that we will use next to creat 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