简体   繁体   English

在将图像提供给 CoreML 模型之前如何预处理图像?

[英]How do I preprocess the image before giving it to CoreML Model?

I created an image similarity model and used the reference data images to test it out.我创建了一个图像相似性模型并使用参考数据图像对其进行了测试。 I tested the turicreate model, and I got back zero distances for reference data images, and the same came back when using this code with the coreml model:我测试了 turicreate 模型,我得到了参考数据图像的零距离,当将此代码与 coreml 模型一起使用时,同样的情况又回来了:

image = tc.image_analysis.resize(reference_data[0]['image'], *reversed(model.input_image_shape))
image = PIL.Image.fromarray(image.pixel_data)
mlmodel.predict({'image':image})`

However, when using the model in iOS as a VNCoreMLModel, no reference image test came back with a zero distance, and most of them weren't even the shortest distance, ie reference image 0 had a shortest distance to reference id 78. Since the coreml model works in python, I figured it was a preprocessing issue, so I preprocessed the image myself before passing it to the CoreMLModel.但是,当在 iOS 中使用模型作为 VNCoreMLModel 时,没有参考图像测试返回零距离,而且大多数甚至不是最短距离,即参考图像 0 与参考 id 78 的距离最短。由于coreml 模型在 python 中工作,我认为这是一个预处理问题,所以我在将其传递给 CoreMLModel 之前自己对图像进行了预处理。 Doing this gave me a consistent output of the reference ids matching the reference images for the shortest distance--yay.这样做为我提供了与最短距离的参考图像匹配的参考 ID 的一致输出 - 是的。 The distance still isn't zero, so I have attempted to do whatever I can think of to affect the image to get some difference, but I can't get it any closer to zero.距离仍然不是零,所以我试图做任何我能想到的事情来影响图像以获得一些差异,但我无法让它更接近零。 Preprocessing code:预处理代码:

+ (CVPixelBufferRef)pixelBufferForImage:(UIImage *)image sideLength:(CGFloat)sideLength {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(sideLength, sideLength), YES, image.scale);
    [image drawInRect:CGRectMake(0, 0, sideLength, sideLength)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CFStringRef keys[2] = {kCVPixelBufferCGImageCompatibilityKey, kCVPixelBufferCGBitmapContextCompatibilityKey};
    CFBooleanRef values[2] = {kCFBooleanTrue, kCFBooleanTrue};
    CFDictionaryRef attrs = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CVPixelBufferRef buffer;
    int status = CVPixelBufferCreate(kCFAllocatorDefault, (int)(sideLength), (int)(sideLength), kCVPixelFormatType_32ARGB, attrs, &buffer);
    if (status != kCVReturnSuccess) {
        return nil;
    }

    CVPixelBufferLockBaseAddress(buffer, kCVPixelBufferLock_ReadOnly);
    void *data = CVPixelBufferGetBaseAddress(buffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
    CGContextRef context = CGBitmapContextCreate(data, sideLength, sideLength, 8, CVPixelBufferGetBytesPerRow(buffer), colorSpace, kCGImageAlphaNoneSkipFirst);

    CGContextTranslateCTM(context, 0, sideLength);
    CGContextScaleCTM(context, 1.0, -1.0);

    UIGraphicsPushContext(context);
    [resizedImage drawInRect:CGRectMake(0, 0, sideLength, sideLength)];
    UIGraphicsPopContext();
    CVPixelBufferUnlockBaseAddress(buffer, kCVPixelBufferLock_ReadOnly);
    return buffer;
}

The mlmodel takes an RGB image with size: (224, 224) mlmodel 使用大小为 (224, 224) 的 RGB 图像

What else can I do to the image to improve my results?我还能对图像做些什么来改善我的结果?

I was in the same boat as you.我和你在同一条船上。 Since image preprocessing involves usage of blurring, conversion from RGB to gray and other steps.由于图像预处理涉及使用模糊、从 RGB 到灰度的转换等步骤。 It would be easier to use Objective C++ wrapper.使用 Objective C++ 包装器会更容易。 Below link gives good understanding about how it can be linked using header classes.下面的链接很好地理解了如何使用头类链接它。

https://www.timpoulsen.com/2019/using-opencv-in-an-ios-app.html https://www.timpoulsen.com/2019/using-opencv-in-an-ios-app.html

Hope it helps!希望能帮助到你!

Image Credits : https://medium.com/@borisohayon/ios-opencv-and-swift-1ee3e3a5735b图片来源: https : //medium.com/@borisohayon/ios-opencv-and-swift-1ee3e3a5735b

学分:https://medium.com/@borisohayon/ios-opencv-and-swift-1ee3e3a5735b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM