Skip to content

Instantly share code, notes, and snippets.

@caevan
Created May 3, 2018 02:04
Show Gist options
  • Save caevan/a07ba5394b691466ab18aa1c36902fef to your computer and use it in GitHub Desktop.
Save caevan/a07ba5394b691466ab18aa1c36902fef to your computer and use it in GitHub Desktop.
//
// CIFaceFeatureExtension.swift
// Detector
//
// Created by Caevan Sachinwalla on 1/5/18.
//
import Foundation
import UIKit
extension CIFaceFeature {
func leftEyePositionForImage(image:UIImage) -> CGPoint{
return self.pointForImage(image: image, fromPoint: self.leftEyePosition)
}
func rightEyePositionForImage(image:UIImage) -> CGPoint{
return self.pointForImage(image:image, fromPoint:self.rightEyePosition)
}
func mouthPositionForImage(image:UIImage) -> CGPoint{
return self.pointForImage(image:image, fromPoint:self.mouthPosition)
}
func boundsForImage(image:UIImage ) -> CGRect{
return self.boundsForImage(image: image, fromBounds: self.bounds)
}
func normalizedLeftEyePositionForImage(image:UIImage) -> CGPoint{
return self.normalizedPointForImage(image: image, fromPoint: self.leftEyePosition)
}
func normalizedRightEyePositionForImage(image:UIImage) -> CGPoint {
return self.normalizedPointForImage(image:image, fromPoint:self.rightEyePosition)
}
func normalizedMouthPositionForImage(image : UIImage) -> CGPoint{
return self.normalizedPointForImage(image:image, fromPoint:self.mouthPosition)
}
func normalizedBoundsForImage(image:UIImage ) -> CGRect{
return self.normalizedBoundsForImage(image:image, fromBounds:self.bounds)
}
func pointForImage(image:UIImage, fromPoint originalPoint:CGPoint) -> CGPoint {
let imageWidth = image.size.width * image.scale;
let imageHeight = image.size.height * image.scale;
var convertedPoint:CGPoint = CGPoint(x: 0, y: 0)
switch (image.imageOrientation) {
case UIImageOrientation.up:
convertedPoint.x = originalPoint.x;
convertedPoint.y = imageHeight - originalPoint.y;
break;
case UIImageOrientation.down:
convertedPoint.x = imageWidth - originalPoint.x;
convertedPoint.y = originalPoint.y;
break;
case UIImageOrientation.left:
convertedPoint.x = imageWidth - originalPoint.y;
convertedPoint.y = imageHeight - originalPoint.x;
break;
case UIImageOrientation.right:
convertedPoint.x = originalPoint.y;
convertedPoint.y = originalPoint.x;
break;
case UIImageOrientation.upMirrored:
convertedPoint.x = imageWidth - originalPoint.x;
convertedPoint.y = imageHeight - originalPoint.y;
break;
case UIImageOrientation.downMirrored:
convertedPoint.x = originalPoint.x;
convertedPoint.y = originalPoint.y;
break;
case UIImageOrientation.leftMirrored:
convertedPoint.x = imageWidth - originalPoint.y;
convertedPoint.y = originalPoint.x;
break;
case UIImageOrientation.rightMirrored:
convertedPoint.x = originalPoint.y;
convertedPoint.y = imageHeight - originalPoint.x;
break;
}
return convertedPoint;
}
func normalizedPointForImage(image:UIImage, fromPoint originalPoint:CGPoint) -> CGPoint {
var normalizedPoint = self.pointForImage(image:image, fromPoint: originalPoint)
normalizedPoint.x /= (image.size.width*image.scale)
normalizedPoint.y /= (image.size.height*image.scale)
return normalizedPoint;
}
func pointInView(viewSize:CGSize, fromNormalizedPoint normalizedPoint:CGPoint ) -> CGPoint {
return CGPoint(x: normalizedPoint.x * viewSize.width, y: normalizedPoint.y * viewSize.height)
}
func sizeForImage(image:UIImage, fromSize originalSize:CGSize ) -> CGSize{
var convertedSize:CGSize = CGSize(width: 0, height: 0);
switch (image.imageOrientation) {
case UIImageOrientation.up,
UIImageOrientation.down,
UIImageOrientation.upMirrored,
UIImageOrientation.downMirrored:
convertedSize.width = originalSize.width;
convertedSize.height = originalSize.height;
break;
case UIImageOrientation.left,
UIImageOrientation.right,
UIImageOrientation.leftMirrored,
UIImageOrientation.rightMirrored:
convertedSize.width = originalSize.height;
convertedSize.height = originalSize.width;
break;
}
return convertedSize;
}
func normalizedSizeForImage(image:UIImage, fromSize originalSize:CGSize ) -> CGSize{
var normalizedSize = self.sizeForImage(image:image, fromSize:originalSize)
normalizedSize.width /= (image.size.width * image.scale);
normalizedSize.height /= (image.size.height*image.scale);
return normalizedSize;
}
func sizeInView(viewSize:CGSize, fromNormalizedSize normalizedSize:CGSize ) -> CGSize{
return CGSize(width:normalizedSize.width * viewSize.width,height: normalizedSize.height * viewSize.height)
}
func boundsForImage(image:UIImage, fromBounds originalBounds:CGRect ) -> CGRect {
var convertedOrigin = self.pointForImage(image: image, fromPoint: originalBounds.origin)
let convertedSize = self.sizeForImage(image: image, fromSize: originalBounds.size)
switch (image.imageOrientation) {
case UIImageOrientation.up:
convertedOrigin.y -= convertedSize.height;
break;
case UIImageOrientation.down:
convertedOrigin.x -= convertedSize.width;
break;
case UIImageOrientation.left:
convertedOrigin.x -= convertedSize.width;
convertedOrigin.y -= convertedSize.height;
break
case UIImageOrientation.right:
break;
case UIImageOrientation.upMirrored:
convertedOrigin.y -= convertedSize.height;
convertedOrigin.x -= convertedSize.width;
break;
case UIImageOrientation.downMirrored:
break;
case UIImageOrientation.leftMirrored:
convertedOrigin.x -= convertedSize.width;
convertedOrigin.y += convertedSize.height;
case UIImageOrientation.rightMirrored:
convertedOrigin.y -= convertedSize.height;
break;
}
return CGRect(x: convertedOrigin.x, y: convertedOrigin.y, width: convertedSize.width, height: convertedSize.height)
}
func normalizedBoundsForImage(image:UIImage, fromBounds originalBounds:CGRect ) -> CGRect{
var normalizedBounds = self.boundsForImage(image: image, fromBounds: originalBounds)
normalizedBounds.origin.x /= (image.size.width*image.scale);
normalizedBounds.origin.y /= (image.size.height*image.scale);
normalizedBounds.size.width /= (image.size.width*image.scale)
normalizedBounds.size.height /= (image.size.height*image.scale)
return normalizedBounds;
}
func boundsInView(viewRect:CGRect, fromNormalizedBounds normalizedBounds:CGRect ) -> CGRect{
return CGRect(x: (normalizedBounds.origin.x * viewRect.size.width)+viewRect.origin.x,
y: (normalizedBounds.origin.y * viewRect.size.height)+viewRect.origin.y,
width: normalizedBounds.size.width * viewRect.size.width,
height: normalizedBounds.size.height * viewRect.size.height)
}
func boundsInView(viewSize:CGSize, fromNormalizedBounds normalizedBounds:CGRect ) -> CGRect{
return CGRect(x: normalizedBounds.origin.x * viewSize.width,
y: normalizedBounds.origin.y * viewSize.height,
width: normalizedBounds.size.width * viewSize.width,
height: normalizedBounds.size.height * viewSize.height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment