我正在制作一個使用計算機視覺 (OpenCV) 的跨平臺 Java 應用程式。我需要從網路攝像頭接收視頻。目前使用標準的 OpenCV 方法:
VideoCapture videoCapture = new VideoCapture();
videoCapture.open(cameraIndex);
...
videoCapture.read(frame);
當然,在訪問相機之前加載原生的OpenCV庫:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
該庫是使用以下引數構建的:
-D WITH_IPP=OFF \
-D BUILD_opencv_java=ON \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=../../opencv-4.5.3/build/install \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.3/modules \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=OFF \
-D OPENCV_JAVA_SOURCE_VERSION=1.8 \
-D OPENCV_JAVA_TARGET_VERSION=1.8 \
-D BUILD_SHARED_LIBS=OFF \
-D BUILD_FAT_JAVA_LIB=ON \
-D WITH_MATLAB=OFF \
-D BUILD_ZLIB=OFF \
-D BUILD_TIFF=OFF \
-D BUILD_JASPER=OFF \
-D BUILD_JPEG=OFF \
-D BUILD_PNG=OFF \
-D WITH_JPEG=OFF \
-D WITH_PNG=OFF \
-D WITH_OPENEXR=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
該相機適用于 Windows 10 (x64) 和 Kali Linux (amd64)。但不是在 macOS 上。
執行后videoCapture.open(cameraIndex),控制臺中出現錯誤訊息并關閉應用程式:
OpenCV: not authorized to capture video (status 0), requesting...
OpenCV: can not spin main run loop from other thread, set OPENCV_AVFOUNDATION_SKIP_AUTH=1 to disable authorization request and perform it in your application.
OpenCV: camera failed to properly initialize!
我嘗試了很多事情,例如export OPENCV_AVFOUNDATION_SKIP_AUTH=0或Thread.sleep(1000)之后videoCapture.open()或在另一個執行緒中打開相機。我還添加了 Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSCameraUsageDescription</key>
<string>Application needs permission to access the camera to capture images</string>
</dict>
</plist>
進入我的 .jar 的 src 檔案夾。但沒有任何幫助。
我已經閱讀了這些:
- https://github.com/opencv/opencv/issues/16255
- https://developer.apple.com/forums/thread/109057
- OpenCV command line app can't access camera under macOS Mojave
- https://answers.opencv.org/question/225281/videocapture-0-does-not-work-in-mac-catalina-the-program-crashed/
and many many other resources but there is still no answer.
I even tried using the OpenPVP-capture-java (https://github.com/openpnp/openpnp-capture-java) library, but that didn't help.
objc[2116]: Class PlatformAVCaptureDelegate is implemented in both /private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/jna-3506402/jna6724263821710587077.tmp (0x14fac7cd8) and /private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/jna-3506402/jna1573965830287255196.tmp (0x14faf9cd8). One of the two will be used. Which one is undefined.
2021-10-09 20:44:39.093 java[2116:99975] Requesting permission, bundle path for Info.plist: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin
zsh: abort java -Djava.library.path=. -jar CamTest.jar
In the system settings, the terminal has access to the camera. Also, there is a problem with the camera on Ubuntu (after videoCapture.open(), the program freezes). But this is a topic for another question.
Is there a way to access the camera for OpenCV (Java) on macOS? Perhaps using some kind of third party cross platform library ...
uj5u.com熱心網友回復:
我再次回答我自己的問題......
謝謝@ChristophRackwitz!在您詢問相機是否在python中作業后,我檢查了它(我不知道為什么我之前沒有這樣做)。因此,python 中的相機(使用 opencv-contrib-python)可以正常作業,所以我意識到問題出在 Java(JDK)本身上。
我使用以下命令安裝了 JDK 8:
brew tap adoptopenjdk/openjdk
brew install --cask adoptopenjdk8
所以,我的 JAVA_HOME 是 /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
我不知道是 Java 壞了還是別的什么,但是當我安裝 Java11 時:brew install java11并設定JAVA_HOME為/usr/local/Cellar/openjdk@11/11.0.12/它終于可以作業了!
我花了很多時間在這個問題上,解決方案只是錯誤的 Java 版本......這很奇怪,因為我的應用程式(和 .jar 檔案)是用 JDK 8 編譯的。在其他作業系統(Windows、Linux)上,一切都很好使用 Java 8 沒有問題。無論如何,現在當我從終端啟動應用程式時,它請求訪問相機,一切正常!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313197.html
