将.mlmodel导入到项目中会用到的方法

  • 将UIImage转换成CVPixelBufferRef

    /// 下面这个属性是UIImage的,你只要UIImage的对象 image.CGImage传进来就行了
    /// @property(nullable, nonatomic,readonly) CGImageRef CGImage; 
    
    - (CVPixelBufferRef)GetpixelBufferWithCGImage:(CGImageRef)cgimage
    {
        NSDictionary *options = @{
                                  (NSString*)kCVPixelBufferCGImageCompatibilityKey : @YES,
                                  (NSString*)kCVPixelBufferCGBitmapContextCompatibilityKey : @YES,
                                  (NSString*)kCVPixelBufferIOSurfacePropertiesKey: [NSDictionary dictionary]
                                  };
        CVPixelBufferRef pxbuffer = NULL;
        
        CGFloat frameWidth = CGImageGetWidth(cgimage);
        CGFloat frameHeight = CGImageGetHeight(cgimage);
        
        CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
                                              frameWidth,
                                              frameHeight,
                                              kCVPixelFormatType_32BGRA,
                                              (__bridge CFDictionaryRef) options,
                                              &pxbuffer);
        
        NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
        
        CVPixelBufferLockBaseAddress(pxbuffer, 0);
        void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
        NSParameterAssert(pxdata != NULL);
        
        CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
        
        CGContextRef context = CGBitmapContextCreate(pxdata,
                                                     frameWidth,
                                                     frameHeight,
                                                     8,
                                                     CVPixelBufferGetBytesPerRow(pxbuffer),
                                                     rgbColorSpace,
                                                     (CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
        NSParameterAssert(context);
        CGContextConcatCTM(context, CGAffineTransformIdentity);
        CGContextDrawImage(context, CGRectMake(0,
                                               0,
                                               frameWidth,
                                               frameHeight),
                           cgimage);
        CGColorSpaceRelease(rgbColorSpace);
        CGContextRelease(context);
        
        CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
        
        return pxbuffer;
    }
    
  • 裁剪图片,全部为224*224

    //需要传过来的参数有:图片 image 和自定的尺寸
    - (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
        //原始 iamge
        UIImage *sourceImage = image;
        //新的image 用来接收裁剪后的image 开始时为 nil 
        UIImage *newImage = nil;
        UIGraphicsBeginImageContext(targetSize);
        CGRect thumbnailRect = CGRectZero;
        //裁剪后的image 的原点和 裁剪前 的 image 的 原点相同
        thumbnailRect.origin = CGPointZero;
        //裁剪后的image 的宽和 指定的宽 相同
        thumbnailRect.size.width  = targetSize.width;
        //裁剪后的image 的长和 指定的长 相同
        thumbnailRect.size.height = targetSize.height;
        //将原始image 在设定的 位置上绘制(裁剪)
        [sourceImage drawInRect:thumbnailRect];
        //把裁剪好的 image 放在 新的image 上
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage ;
    }
    
  • 调用系统相机或从相册中选择照片后,得到结果

    - (void)RecognizeWithImage:(UIImage *)pImage
    {
        MobileNet *mobileNet = [[MobileNet alloc]init];
        //只需要调用predictionFromImage:error:这一个方法就可以得到结果了
        MobileNetOutput *output = [mobileNet predictionFromImage:[[pImage ScalingToSize:CGSizeMake(224, 224)] GetpixelBuffer] error:nil];
        _photoName.text = output.classLabel;
        //所有可能的结果 以及每种结果的可能性百分比
        NSLog(@"classLabelProbs%@",output.classLabelProbs);
        //可能性最大的那个结果
        NSLog(@"classLabel%@",output.classLabel);
    }
    
要将Microsoft.ML库添加到Unity项目,需要执行以下步骤: 1. 下载并安装.NET Core SDK:在您的计算机上安装.NET Core SDK,以便可以使用Microsoft.ML库。 2. 创建Unity项目:创建一个新的Unity项目或打开现有的项目。 3. 添加Microsoft.ML库:在Unity项目,右键单击Assets文件夹并选择“Import Package”>“Custom Package”。选择包含Microsoft.ML库的NuGet包,并将其导入Unity项目。 4. 配置项目:在Unity项目,创建一个新的C#脚本并添加以下代码,以配置项目以使用Microsoft.ML库: ``` using System.IO; using Microsoft.ML; using UnityEngine; public class MyMLModel : MonoBehaviour { private PredictionEngine<ModelInput, ModelOutput> _predictionEngine; private void Start() { MLContext mlContext = new MLContext(); // Load the model ITransformer mlModel = mlContext.Model.Load("model.zip", out var modelSchema); // Create prediction engine _predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel); // Make a prediction ModelInput input = new ModelInput(); input.SepalLength = 5.1f; input.SepalWidth = 3.5f; input.PetalLength = 1.4f; input.PetalWidth = 0.2f; ModelOutput output = _predictionEngine.Predict(input); Debug.Log($"Prediction: {output.Prediction}"); } } public class ModelInput { public float SepalLength { get; set; } public float SepalWidth { get; set; } public float PetalLength { get; set; } public float PetalWidth { get; set; } } public class ModelOutput { public string Prediction { get; set; } } ``` 5. 运行项目:在Unity项目,单击“Play”按钮以运行项目并查看Microsoft.ML库的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值