3

I'm actualy trying to use OpenCV with ARKit to build an AR app. For the past 6 hours I tried everything to install OpenCV on my Swift project but that seems impossible.
I followed this tutorial on Medium and I ended up with 3 new files with the framework : OpenCVWrapper.h and OpenCVWrapper.mm for the wrapper and LittleFarm-Bridging-Header.h for the bridging header, here is the code :

OpenCVWrapper.h

#import <Foundation/Foundation.h>
#import <stdio.h>
@interface OpenCVWrapper : NSObject

- (void) isItWorking;
@end

OpenCVWrapper.mm

#import "OpenCVWrapper.h"
#import <opencv2/opencv.hpp>

@implementation OpenCVWrapper

    using namespace std;
    - (void) isItWorking {
        printf("Hey");
    }
    @end

LittleFarm-Bridging-Header.h

#import "OpenCVWrapper.h"

But then I get this error : 'opencv2/opencv.hpp' file not found
I tried to figure what was the problem and I set Framework_Search_Path to $(PROJECT_DIR). That doesn't change anything and if I tried to comment the related line I get this error :
ld: framework not found opencv2
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Edit : In Build Phase > Link Binary with Librairies the framework opencv2 is here.

Thanks in advance for the help !

Community
  • 1
  • 1
Alain Berrier
  • 621
  • 6
  • 23
  • 2
    I haven't tried it in XCode 9 yet, but that tutorial on Medium is kind of outdated. OpenCV is now available on cocoapods https://cocoapods.org/pods/OpenCV. And I suggest you use 3.1.0.1'>, it's the most stable and it's working great for me. Finally, don't forget to set USE_BITCODE = false in pod target's build settings. – hoang Cap Jul 31 '17 at 00:10
  • 1
    And in case your don't wanna use cocoapods, I think this would fix your problem: https://monosnap.com/file/EE9HHs3f214gaVuelWtU6DiKYY9L3G – hoang Cap Jul 31 '17 at 00:29
  • I tried to install OpenCV with Cocoapods but I can't import OpenCV on my project. Some frameworks seem to be missing. Here is a screen for more informations : [link](http://i.imgur.com/8p2ephQ.png) – Alain Berrier Jul 31 '17 at 10:17
  • I tried to launch my project and I get this error : [link](https://pastebin.com/Gstubgbk) – Alain Berrier Jul 31 '17 at 11:09
  • OpenCV framework is written in C++, you simply cannot import a C++ framework to your Swift code. Instead you will need to write ObjectiveC++ wrappers (.mm files), and then import these wrappers to bridging header file, then you can use open CV with Swift. – hoang Cap Jul 31 '17 at 11:36
  • Cocoapods doesn't do it automatically ? – Alain Berrier Jul 31 '17 at 11:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150603/discussion-between-alain-berrier-and-hoang-cap). – Alain Berrier Jul 31 '17 at 11:45
  • 1
    Nope. Cocoapods only copies the binary files (or source codes) to your project, then performs proper linkings/configurations to your build settings/build phases. – hoang Cap Jul 31 '17 at 11:46
  • After doing what we discussed in chat, I still have one error : **"cuda_runtime.h" file not found cuda_stream_accessor.hpp** – Alain Berrier Jul 31 '17 at 12:25

2 Answers2

3

I finaly managed to fix my problem with the great help of hoang Cap.

  • First, instead of drag and drop the openCV framework, I install it with Cocoapods
  • Notice that the version 3.2 generates some issues, 3.1.0.1 is working fine.

    pod 'OpenCV', '~> 3.1.0.1'

  • Deactivate bitcode on both Pods target and your project.

alexburtnik
  • 7,661
  • 4
  • 32
  • 70
Alain Berrier
  • 621
  • 6
  • 23
2

If someone would like to use the current version or anything newer than 3.1.0.1 of OpenCV with XCode, this workaround helps. To ignore the documentation warnings this may also help.

Amayatsky wrote:

I have moved #import <opencv2/opencv.hpp> above all other imports as suggested and that did the trick.

Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74