在将图像提供给CoreML Model之前如何对其进行预处理?

问题描述 投票:2回答:1

我创建了图像相似性模型,并使用参考数据图像对其进行了测试。我测试了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})`

[但是,当在iOS中将模型用作VNCoreMLModel时,没有零距离返回参考图像测试,并且大多数距离甚至都不是最短距离,即参考图像0到参考ID 78的距离最短。由于coreml模型在python中工作,因此我认为这是一个预处理问题,因此我自己对图像进行了预处理,然后再将其传递给CoreMLModel。这样做使我获得了与最短距离的参考图像匹配的参考ID的一致输出-是的。距离仍然不为零,因此我试图做我想做的任何事情来影响图像以获得一些差异,但是我无法使其更接近于零。预处理代码:

+ (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;
}

mlmodel拍摄大小为((224,224))的RGB图像

我还能对图像做些什么以改善效果?

python ios objective-c image-processing coreml
1个回答
0
投票

我和你在同一条船上。由于图像预处理涉及模糊的使用,因此需要从RGB转换为灰度以及其他步骤。使用Objective C ++包装器会更容易。下面的链接很好地了解了如何使用标头类将其链接。

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

希望有帮助!

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

© www.soinside.com 2019 - 2024. All rights reserved.