zweigraf / face-landmarking-ios

👦 Basic face landmarking on iPhone with Dlib via Swift & ObjC++
481 stars 125 forks source link

Sending UIImage to DlibWrapper.mm #31

Closed aguusvazquez closed 7 years ago

aguusvazquez commented 7 years ago

People, I'm getting crazy with something. Im trying to send a UIImage to the function, but it is very slow (more than 10 seconds)... Can this be related to the size of the image or it quality?

Show you part of my code:

First of all I created this function in the DLIB Wrapper:

`- (void)processImage:(UIImage*) uiImage {

CGFloat width = uiImage.size.width, height = uiImage.size.height;
CGContextRef context;
size_t pixelBits = CGImageGetBitsPerPixel(uiImage.CGImage);
size_t pixelBytes = pixelBits/8;
size_t dataSize = pixelBytes * ((size_t) width*height);
uchar* imageData = (uchar*) malloc(dataSize);
memset(imageData, 0, dataSize);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(uiImage.CGImage);

CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
bool isGray = false;
if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
    // gray image
    bitmapInfo = kCGImageAlphaNone;
    isGray = true;
}
else
{

}

  context = CGBitmapContextCreate(imageData, (size_t) width, (size_t) height,
                                  8, pixelBytes*((size_t)width), colorSpace,
                                 bitmapInfo);
 CGContextDrawImage(context, CGRectMake(0, 0, width, height), uiImage.CGImage);
  CGContextRelease(context);

  dlib::array2d<dlib::bgr_pixel> dlibImage;

  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);
      uchar b, g, r;
      if (isGray) {
          b = imageData[offset];
          g = imageData[offset];
          r = imageData[offset];
      } else {
          b = imageData[offset];
          g = imageData[offset+1];
          r = imageData[offset+2];
      }
      pixel = dlib::bgr_pixel(b, g, r);
      position++;
  }

  std::vector<dlib::rectangle> dets = detector(dlibImage);

 for (unsigned long j = 0; j < dets.size(); ++j)
  {
      dlib::full_object_detection shape = sp(dlibImage, dets[j]);

      for (unsigned long k = 0; k < shape.num_parts(); k++) {
          dlib::point p = shape.part(k);
          draw_solid_circle(dlibImage, p, 3, dlib::rgb_pixel(0, 255, 255));
      }
  }

  free(imageData);

} `

By the other hand, this is the function i have defined in the Header:

- (void)processImage:(UIImage*)uiImage;

I'm calling this function in the next way:

`

                                                                  [self.libreria processImage:image];

`

Of course, thanks a lot for your help!!! Probably I'm doing something wrong!

Best regards

aguusvazquez commented 7 years ago

Solved it!!! Just needed to resize the Image before sending it to the function. Thanks anyway. Maybe this post helps somebody with the same problem.