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)
            }
        }
    }
相关推荐
西猫雷婶19 小时前
CNN卷积计算
人工智能·神经网络·cnn
2501_9159090619 小时前
HTML5 与 HTTPS,页面能力、必要性、常见问题与实战排查
前端·ios·小程序·https·uni-app·iphone·html5
格林威20 小时前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
倔强青铜三21 小时前
苦练Python第63天:零基础玩转TOML配置读写,tomllib模块实战
人工智能·python·面试
B站计算机毕业设计之家21 小时前
智慧交通项目:Python+YOLOv8 实时交通标志系统 深度学习实战(TT100K+PySide6 源码+文档)✅
人工智能·python·深度学习·yolo·计算机视觉·智慧交通·交通标志
高工智能汽车21 小时前
棱镜观察|极氪销量遇阻?千里智驾左手服务吉利、右手对标华为
人工智能·华为
txwtech1 天前
第6篇 OpenCV RotatedRect如何判断矩形的角度
人工智能·opencv·计算机视觉
正牌强哥1 天前
Futures_ML——机器学习在期货量化交易中的应用与实践
人工智能·python·机器学习·ai·交易·akshare
倔强青铜三1 天前
苦练Python第62天:零基础玩转CSV文件读写,csv模块实战
人工智能·python·面试