iOS使用CoreML运用小型深度神经网络架构对图像进行解析

查找一个图片选择器

我用的是ImagePicker

项目有点老了,需要做一些改造,下面是新的仓库

复制代码
platform :ios, '16.0'

use_frameworks!

target 'learnings' do
  source 'https://github.com/CocoaPods/Specs.git'

  pod 'ImagePicker', :git => 'https://github.com/KevinSnoopy/ImagePicker.git'
  
end

接下来就是使用图片选择器输出图片了

复制代码
    func wrapperDidPress(_ imagePicker: ImagePicker.ImagePickerController, images: [UIImage]) {
        
    }
    
    func doneButtonDidPress(_ imagePicker: ImagePicker.ImagePickerController, images: [UIImage]) {
        if !images.isEmpty, let _ = images.first {
            /**
             在这里输出图片,可以调用模型进行解析
             */
        }
    }
    
    func cancelButtonDidPress(_ imagePicker: ImagePicker.ImagePickerController) {
        imagePicker.dismiss(animated: true)
    }

当前我使用了几个公开的模型

FCRN:

复制代码
/**
     深度估计
     根据一幅图像来预测深度。
     */
    func fcrnDepthPrediction(image: UIImage?) {
        let config = MLModelConfiguration()
        config.computeUnits = .all
        if let img = image?.cgImage, let fcrn = try? FCRN(contentsOf: FCRN.urlOfModelInThisBundle, configuration: config) {
            if let input = try? FCRNInput(imageWith: img), let output = try? fcrn.prediction(input: input) {
                print(output.depthmapShapedArray)
            }
        }
    }

MNISTClassifier:

复制代码
/**
     涂鸦分类
     对单个手写数字进行分类 (支持数字 0-9)。
     */
    func mnistClassifier(image: UIImage?) {
        if let img = image?.cgImage, let mnist = try? MNISTClassifier(contentsOf: MNISTClassifier.urlOfModelInThisBundle, configuration: MLModelConfiguration()) {
            if let input = try? MNISTClassifierInput(imageWith: img), let output = try? mnist.prediction(input: input) {
                print(output.classLabel)
                print(output.labelProbabilities)
            }
        }
    }

UpdatableDrawingClassifier:

复制代码
/**
     涂鸦分类
     基于 K-最近邻算法(KNN)模型来学习识别新涂鸦的涂鸦分类器。
     */
    func updatableDrawingClassifier(image: UIImage?) {
        if let img = image?.cgImage, let updatable = try? UpdatableDrawingClassifier(contentsOf: UpdatableDrawingClassifier.urlOfModelInThisBundle, configuration: MLModelConfiguration()) {
            if let input = try? UpdatableDrawingClassifierInput(drawingWith: img), let output = try? updatable.prediction(input: input) {
                print(output.label)
                print(output.labelProbs)
            }
        }
    }

MobileNetV2:

复制代码
/**
     图像分类
     MobileNetv2 架构经过训练,可对相机取景框内或图像中的主要对象进行分类。
     */
    func mobileNetV2(image: UIImage?) {
        if let img = image?.cgImage, let netv2 = try? MobileNetV2(contentsOf: MobileNetV2.urlOfModelInThisBundle, configuration: MLModelConfiguration()) {
            if let input = try? MobileNetV2Input(imageWith: img), let output = try? netv2.prediction(input: input) {
                print(output.classLabel)
                print(output.classLabelProbs)
            }
        }
    }

Resnet50:

复制代码
/**
     图像分类
     一种残差神经网络,它能对相机取景框内或图像中的主要对象进行分类。
     */
    func resnet50(image: UIImage?) {
        if let img = image?.cgImage, let resnet = try? Resnet50(contentsOf: Resnet50.urlOfModelInThisBundle, configuration: MLModelConfiguration()) {
            if let input = try? Resnet50Input(imageWith: img), let output = try? resnet.prediction(input: input) {
                print(output.classLabel)
                print(output.classLabelProbs)
            }
        }
    }

SqueezeNet:

复制代码
/**
     图像分类
     一种小型深度神经网络架构,它能对相机取景框内或图像中的主要对象进行分类。
     */
    func squeezeNet(image: UIImage?) {
        if let img = image?.cgImage, let net = try? SqueezeNet(contentsOf: SqueezeNet.urlOfModelInThisBundle, configuration: MLModelConfiguration()) {
            if let input = try? SqueezeNetInput(imageWith: img), let output = try? net.prediction(input: input) {
                print(output.classLabel)
                print(output.classLabelProbs)
            }
        }
    }
相关推荐
算家云2 分钟前
“液态玻璃”难解苹果AI焦虑:WWDC25背后的信任危机
人工智能·算力·算家云·租算力,到算家云·wwdc25·苹果ai·ios 26
小小鱼儿小小林4 分钟前
苹果WWDC25重磅发布:Apple Intelligence与Liquid Glass引领未来
ios·wwdc·苹果
I烟雨云渊T5 分钟前
2025年的WWDC所更新的内容
macos·ios·wwdc
Fatbobman(东坡肘子)5 分钟前
WWDC 2025 开发者特辑 | 肘子的 Swift 周报 #088
开发语言·macos·ios·swiftui·ai编程·swift·wwdc
vastgrassland5 分钟前
从WWDC看苹果产品发展的规律
macos·ios·wwdc
琼方15 分钟前
“十五五”时期智慧城市赋能全国一体化数据市场建设:战略路径与政策建议[ 注:本建议基于公开政策文件与行业实践研究,数据引用截至2025年6月11日。]
大数据·人工智能·智慧城市
飞哥数智坊18 分钟前
深夜1点被粉丝问懵?10分钟打造“AI分身”接管公众号答疑,亲测好用!
人工智能
Gsen281919 分钟前
AI大模型从0到1记录学习 大模型技术之机器学习 day27-day60
人工智能·学习·机器学习
四川兔兔21 分钟前
pytorch 之 nn 库与调试
人工智能·pytorch·python
終不似少年遊*33 分钟前
机器学习方法实现数独矩阵识别器
人工智能·python·opencv·机器学习·计算机视觉·矩阵