Created
May 25, 2018 05:25
-
-
Save dimpler03/cbf11dca842d1569860540fe4274d2e3 to your computer and use it in GitHub Desktop.
Compute face points using dlib image
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
- (NSMutableArray *)doWorkOnSampleBuffer:(UIImage *)image inRects:(NSArray<NSValue *> *)rects { | |
dlib::array2d<dlib::bgr_pixel> dlibImage; | |
// convert uiimage to dlib image | |
CGFloat width = image.size.width, height = image.size.height; | |
CGContextRef context; | |
size_t pixelBits = CGImageGetBitsPerPixel(image.CGImage); | |
size_t pixelBytes = pixelBits/8; | |
size_t dataSize = pixelBytes * ((size_t) width*height); | |
char* imageData = (char*) malloc(dataSize); | |
memset(imageData, 0, dataSize); | |
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); | |
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast; | |
// color image bit map info | |
bitmapInfo = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault; | |
context = CGBitmapContextCreate(imageData, (size_t) width, (size_t) height, | |
8, pixelBytes*((size_t)width), colorSpace, | |
bitmapInfo); | |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage); | |
CGContextRelease(context); | |
dlibImage.clear(); | |
dlibImage.set_size((long)height, (long)width); | |
dlibImage.reset(); | |
long position = 0; | |
while (dlibImage.move_next()){ | |
dlib::bgr_pixel& pixel = dlibImage.element(); | |
long offset = position*((long) pixelBytes); | |
char b, g, r; | |
b = imageData[offset]; | |
g = imageData[offset+1]; | |
r = imageData[offset+2]; | |
pixel = dlib::bgr_pixel(b, g, r); | |
position++; | |
} | |
free(imageData); | |
std::vector<dlib::rectangle> convertedRectangles = [MLWrapper convertCGRectValueArray:rects]; | |
for (unsigned long j =0; j < convertedRectangles.size(); ++j) { | |
dlib::rectangle oneFaceRect = convertedRectangles[j]; | |
// detect all landmarks | |
dlib::full_object_detection shape = sp(dlibImage, oneFaceRect); | |
// and draw them into the image (samplebuffer) | |
for (unsigned long k = 0; k < shape.num_parts(); k++) { | |
dlib::point p = shape.part(k); | |
CGPoint point = CGPointMake(p.x(), p.y()); | |
[points addObject:[NSValue valueWithCGPoint:point]]; | |
} | |
} | |
return points; | |
} | |
+ (std::vector<dlib::rectangle>)convertCGRectValueArray:(NSArray<NSValue *> *)rects { | |
std::vector<dlib::rectangle> myConvertedRects; | |
for (NSValue *rectValue in rects) { | |
CGRect rect = [rectValue CGRectValue]; | |
long left = rect.origin.x; | |
long top = rect.origin.y; | |
long right = left + rect.size.width; | |
long bottom = top + rect.size.height; | |
dlib::rectangle dlibRect(left, top, right, bottom); | |
myConvertedRects.push_back(dlibRect); | |
} | |
return myConvertedRects; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment