Cài đặt opencv cho mac os mojave

A couple of comments here...

The error I'm seeing when trying to run OpenCV from my MacOS development environment Is:

OpenCV: not authorized to capture video (status 0), requesting... OpenCV: camera failed to properly initialize! Error opening video stream or file Program ended with exit code: 255

I know those words originate from the OpenCV library here. My initial thought was that this was an OpenCV issue. With a bit more testing I think it's something else. As others have noted, MacOS security / permissions issue. But here's the rub.

If I go to Mac Apple Icon (Upper Left Corner) --> Systems Preferences --> Security and Privacy I can glean a lot of info.

Cài đặt opencv cho mac os mojave

Check on the Camera Icon.

Cài đặt opencv cho mac os mojave

In my case this shows two applications which require additional permissions to get access to the camera, Terminal and Virtualbox (not sure what happens to browser, Facetime?) I do note, Xcode didn't make this list.

When I click over to Microphone, I see different apps listed, INCLUDING Xcode.

Cài đặt opencv cho mac os mojave

How does that even work? I did do a whole lot of testing, including researching modifying the Info.plist for the Xcode application package (Finder --> Applications Folder --> Xcode --> Rt click, Show Package Contents. Copy Info.plist save it elsewhere, modify it via Xcode, resubmit.) Note: Don't try this without keeping a copy of the original Info.plist. Total fail. Adding the NSCameraUsageDescription key/value was a total bust. Xcode won't open at all. Reminder DON'T lose the original Info.plist.

This whole thing is baffling. Why does Apple allow us to access the camera via terminal but not in Xcode? What's the logic there?

I sure would like to be able to step thru code to understand frame by frame possible design issues. This just isn't fun.

So a couple of things to understand.

  1. Yes, you can run an OpenCV project on MacOS WITH your camera after the program has been successfully compiled to Unix Executable. You have to ensure permissions for the Terminal are set in Security and Privacy per photo above. Obviously you build the executable in your development tool (in my case Xcode) then open the executable from the projects Build/Debug folder. The app opens in the terminal window and works just fine as noted by SSteve.

  2. If you really want to do some video / camera debugging, you do have the option to "pre-record" a video, then open that video in your development environment. At that point you can use the debugger. How do you guys do frame by frame analysis? This is the only way I know of that will at least partially work.

  3. (edit update 5/22/19...) Whoa. I just realized.. you can attach the debugger to a running (terminal) process. You can totally do frame by frame debugging, using the camera (as long as the program compiles to a functional executable.) Now this is pretty cool, and gets me to 98% functionality. To do this, start the terminal executable, then go to Xcode --> Debug --> Attach to Process. Select the running application, add Breakpoints to the source code and debug/step along. Works well.

I start my OpenCV project with:

int main(int argc, char** argv){
    // Parse command line arguments
    CommandLineParser parser(argc,argv,keys);

    // Create a VideoCapture object & open the input file
    VideoCapture cap;
    if (parser.has("video")){
        cap.open(parser.get<String>("video"));
    }
    else
        cap.open(0);
   ...

It's a hack work around, but better than nothing. (Sure wish Apple included the camera in iOS emulator, that would be another way to solve this, sigh.) Obviously a lot depends on where you are going with your project. Ultimately I need mine to run on an iPad; Proveout on MacOS, then wrap code in Swift, etc...

For reference, I'm using macOS Mojave, 10.14.4, MacBook 2.7GHz i7

PS. The security preferences above doesn't show Chrome with Camera access. Seems odd. I just tested the camera at this site... in Chrome, and it asks for permission and works exactly as expected. Its not clear on what's going on here.

PS2. Am I the only person to file a bug report on this issue? Link included for your convenience. Thanks.

Cài đặt opencv cho mac os mojave

Đã đăng vào thg 12 23, 2019 1:30 SA 3 phút đọc

Hôm nay mình sẽ hướng dẫn các bạn cài đặt OpenCV4 trên MacOS

Bước 1: Cài đặt Xcode:

Để cài đặt Xcode chỉ cần vào Apple App Store tìm ứng dụng Xcode và tiến hành cài đặt.

Sau khi Xcode được cài đặt thì chúng ta cần chấp nhận những điều khoản. Mở Terminal và chạy lệnh sau:

$ sudo xcodebuild -license

Hãy chấp nhận những điều khoản, sau đó cài đặt Apple Command Line (việc này là bắt buộc), nên bạn có thể cài đặt tools bên dưới:

$ sudo xcode-select --install

Cài đặt opencv cho mac os mojave

Click vào install để cài đặt.

Bước 2: Cài đặt Homebrew:

Để cài đặt Homebrew bạn có thể dùng qua lệnh sau thông qua Terminal:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Tiếp theo là cập nhật Homebrew:

$ brew update

Tiếp theo chỉnh sửa bash_profile bằng lệnh:

$ nano ~/.bash_profile

Đến đây ta sẽ vào chỉnh sửa file bash_profile. Thêm PATH sau vào cuối file:

# Homebrew
export PATH=/usr/local/bin:$PATH

Bấm Ctrl + X để thoát nano. Bạn sẽ nhận được cảnh báo có lưu file không. Hãy nhớ lưu lại. Sau đó chạy lệnh:

$ source ~/.bash_profile

Bước 4: Cài đặt các điều kiện tiên quyết bằng Homebrew:

Cài đặt python 3.6:

Tại sao lại là python 3.6. Vì python 3.7 không hỗ trợ Keras mặc dù có hỗ trợ Tensorflow nhưng cũng không khuyến khích cài đặt python 3.7 vậy để tránh trường hợp dùng thư viện mà lại có lỗi không mong muốn nên cũng không phải là sự lựa chọn tốt cho OpenCV. Lệnh bên dưới là để cài python 3.6.5_1:

$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
$ brew switch python 3.6.5_1

Sau đó xác nhận xem python đã được cài đặt chưa:

$ python3

Kết quả:

Python 3.6.5
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Hãy xác nhận lận nữa:

$ which python3
/usr/local/bin/python3

nếu bạn thấy /usr/local/bin/python3nghĩa là bạn đang sử dụng Homebrew python (đó là điều chúng ta mong muốn) còn /usr/bin/python3 nghĩa là bạn đang sử dụng python3 của hệ thống và bạn cần phải sửa lại trong file bash_profile của bạn và source nó.

Hãy dành chút thời gian cho việc kiểm tra xem bạn dùng python của Homebrew hay hệ thống.

Hãy cài những điều kiện tiên quyết tiếp theo

OpenCV yêu cầu một vài điều kiện trước khi được cài đặt. Bạn sẽ cài đặt những gói sau:

  • Công cụ được sử dụng để xây dựng và biên dịch
  • Thư viện được sử dụng cho các hoạt động I / O của hình ảnh
  • Thư viện tối ưu hóa
$ brew install cmake pkg-config
$ brew install jpeg libpng libtiff openexr
$ brew install eigen tbb

Cài đặt wget:

$ brew install wget

Phần tiếp theo sẽ hơi dài nên mình tách ra làm 2 phần.

Phần 2: https://viblo.asia/p/huong-dan-cai-dat-opencv-4-len-macos-phan-2-924lJqJ8ZPM

Nguồn: https://www.pyimagesearch.com/2018/08/17/install-opencv-4-on-macos/

All rights reserved