Skip to content

Instantly share code, notes, and snippets.

@mobilequickie mobilequickie/celebrity.swift Secret
Created Sep 20, 2018

What would you like to do?
//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 =!
//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
celebrityInImage.infoLink = ""
//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)
//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)
//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
You can’t perform that action at this time.