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)
            }
        }
    }
相关推荐
weiwenhao24 分钟前
关于 nature 编程语言
人工智能·后端·开源
神经星星25 分钟前
训练成本29.4万美元,DeepSeek-R1登Nature封面,首个通过权威期刊同行评审的主流大模型获好评
人工智能
神州问学29 分钟前
【AI洞察】别再只想着“让AI听你话”,人类也需要学习“适应AI”!
人工智能
DevUI团队1 小时前
🚀 MateChat V1.8.0 震撼发布!对话卡片可视化升级,对话体验全面进化~
前端·vue.js·人工智能
聚客AI1 小时前
🎉7.6倍训练加速与24倍吞吐提升:两项核心技术背后的大模型推理优化全景图
人工智能·llm·掘金·日新计划
黎燃1 小时前
当 YOLO 遇见编剧:用自然语言生成技术把“目标检测”写成“目标剧情”
人工智能
算家计算1 小时前
AI教母李飞飞团队发布最新空间智能模型!一张图生成无限3D世界,元宇宙越来越近了
人工智能·资讯
掘金一周1 小时前
Flutter Riverpod 3.0 发布,大规模重构下的全新状态管理框架 | 掘金一周 9.18
前端·人工智能·后端
用户5191495848452 小时前
C#记录类型与集合的深度解析:从默认实现到自定义比较器
人工智能·aigc
IT_陈寒5 小时前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端