Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello OpenCV on iOS #94

Open
soapgu opened this issue Dec 29, 2021 · 0 comments
Open

Hello OpenCV on iOS #94

soapgu opened this issue Dec 29, 2021 · 0 comments
Labels
problem problem or trouble Research

Comments

@soapgu
Copy link
Owner

soapgu commented Dec 29, 2021

  • 前言

上一篇已经成功在本机mac上安装opcv的库,并启动一个C++控制台程序成功调用。
但是一般我们的程序都是以app的形式作为载体的,我们需要能直接调用api并且和当前应用的界面有个“深度对接”。本来是想先从mac App,AppKit这条路来尝试的。但是opencv官网上貌似暂时只支持ios平台。所以还是先走通ios这条路再尝试mac app,毕竟ios app 和mac app有很多相似点,实在不行可以考虑通用app在macOS直接运行

  • 获取 opencv iOS的库之路

本来以为很“简单”,摸着邓兄过河就行了。

参考资料

  1. 直接下载Libragy Realse - iOS pack
    从官方下载iOS的库,解压zip。opencv2.framework 加入到Framworks里面,在代码里面import opencv2,敲代码也没问题。但是build以后,报错了...
/Users/guhui/WorkSpace/PlayPen/OpenCVIOS/OpenCVIOS/ViewController.swift:9:8: Could not find module 'opencv2' for target 'arm64-apple-ios-simulator'; found: x86_64-apple-ios-simulator, armv7s, armv7s-apple-ios, armv7-apple-ios, i386-apple-ios-simulator, arm64-apple-ios, armv7, at: /Users/guhui/WorkSpace/PlayPen/OpenCVIOS/opencv2.framework/Modules/opencv2.swiftmodule

一下子有点懵,剧情不是按照我想的发展啊。
难道又是我M1 arm的锅,换个思路。我直接从源代码编译出来用总行吧。“一条龙服务”总不会不支持吧

  1. build framwrok by source
    https://github.com/opencv/opencv/tree/4.x/platforms/ios/readme.txt 里面讲得还是蛮清楚的

进控制台

git clone [email protected]:opencv/opencv.git
python opencv/platforms/ios/build_framework.py ios

接下来是漫长的等待...
建议可以出去喝一杯再回来看
编译似乎没什么问题

重新引用opencv2.framework ,结果还是一样的报错
图片
生成的sample项目编译一下也是一样的错误

  1. 最终解决方案
    一下陷入迷茫不知道怎么解决了。
    图片
    还是要冷静,首先从报错关键信息入手应该是arm64-apple-ios-simulator这个关键字,打开opencv2.framework,从/Modules/opencv2.swiftmodule看确实没有arm64-apple-ios-simulator,只有“兄弟” i386-apple-ios-simulator,x86_64-apple-ios-simulator。根本就没有编译出这个架构的模块
    那再回头到编译这个动作去解决

openCV 官方教程对编译库有更详细的说明

输入help命令看看详细说明

guhui@guhuideMacBook-Pro Githubs % python opencv/platforms/ios/build_framework.py ios --help                                                            
usage: build_framework.py [-h] [--opencv DIR] [--contrib DIR]
                          [--without MODULE] [--disable FEATURE] [--dynamic]
                          [--disable-bitcode]
                          [--iphoneos_deployment_target IPHONEOS_DEPLOYMENT_TARGET]
                          [--build_only_specified_archs]
                          [--iphoneos_archs IPHONEOS_ARCHS]
                          [--iphonesimulator_archs IPHONESIMULATOR_ARCHS]
                          [--enable_nonfree] [--debug] [--debug_info]
                          [--framework_name FRAMEWORK_NAME] [--legacy_build]
                          [--run_tests] [--build_docs] [--disable-swift]
                          OUTDIR

The script builds OpenCV.framework for iOS.

positional arguments:
  OUTDIR                folder to put built framework

optional arguments:
  -h, --help            show this help message and exit
  --opencv DIR          folder with opencv repository (default is "../.."
                        relative to script location)
  --contrib DIR         folder with opencv_contrib repository (default is
                        "None" - build only main framework)
  --without MODULE      OpenCV modules to exclude from the framework. To
                        exclude multiple, specify this flag again, e.g. "--
                        without video --without objc"
  --disable FEATURE     OpenCV features to disable (add WITH_*=OFF). To
                        disable multiple, specify this flag again, e.g. "--
                        disable tbb --disable openmp"
  --dynamic             build dynamic framework (default is "False" - builds
                        static framework)
  --disable-bitcode     disable bitcode (enabled by default)
  --iphoneos_deployment_target IPHONEOS_DEPLOYMENT_TARGET
                        specify IPHONEOS_DEPLOYMENT_TARGET
  --build_only_specified_archs
                        if enabled, only directly specified archs are built
                        and defaults are ignored
  --iphoneos_archs IPHONEOS_ARCHS
                        select iPhoneOS target ARCHS. Default is
                        "armv7,armv7s,arm64"
  --iphonesimulator_archs IPHONESIMULATOR_ARCHS
                        select iPhoneSimulator target ARCHS. Default is
                        "i386,x86_64"
  --enable_nonfree      enable non-free modules (disabled by default)
  --debug               Build "Debug" binaries (disabled by default)
  --debug_info          Build with debug information (useful for Release mode:
                        BUILD_WITH_DEBUG_INFO=ON)
  --framework_name FRAMEWORK_NAME
                        Name of OpenCV framework (default: opencv2, will
                        change to OpenCV in future version)
  --legacy_build        Build legacy opencv2 framework (default: False,
                        equivalent to "--framework_name=opencv2
                        --without=objc")
  --run_tests           Run tests
  --build_docs          Build docs
  --disable-swift       Disable building of Swift extensions

IPHONESIMULATOR_ARCHS select iPhoneSimulator target ARCHS. Default is "i386,x86_64"
文档很清晰,默认架构没有。
尝试

python opencv/platforms/ios/build_framework.py ios --iphonesimulator_archs arm64 

Using IPHONEOS_DEPLOYMENT_TARGET=9.0
Using iPhoneOS ARCHS=[u'armv7', u'armv7s', u'arm64']
Using iPhoneSimulator ARCHS=[u'arm64']
============================================================
ERROR: Cannot have the same architecture for multiple platforms in a fat framework! Consider using build_xcframework.py in the apple platform folder instead. Duplicate archs are set([u'arm64'])
============================================================

晕死,模拟器和iphone架构重复还不给执行。
不高兴去改脚本了,暂时把arm64从iphone架构阉掉,反正我先只用模拟器运行

 python opencv/platforms/ios/build_framework.py ios --iphonesimulator_archs x86_64,arm64 --iphoneos_archs armv7,armv7s
  • Demo Code

storyboard拖一个button并和ViewController关联
从unsplash下一个图拖到Assets里面
图片

//
//  ViewController.swift
//  OpenCVIOS
//
//  Created by guhui on 2021/12/29.
//

import UIKit
import opencv2

class ViewController: UIViewController {
    @IBOutlet var imageButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let orgImage = UIImage(named: "picture")
        let src = Mat(uiImage: orgImage!)
        let gray = Mat()
        Imgproc.cvtColor(src: src, dst: gray, code: .COLOR_RGBA2GRAY)
        imageButton.setImage(gray.toUIImage(), for: .normal)
    }

}

运行结果
截屏2021-12-29 下午4 48 43

@soapgu soapgu added the problem problem or trouble label Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem problem or trouble Research
Projects
None yet
Development

No branches or pull requests

1 participant