groupgets / purethermal1-uvc-capture

USB Video Class capture examples for PureThermal 1 / PureThermal 2 FLIR Lepton Dev Kit
130 stars 81 forks source link

Problem uvc_open #56

Open Takhmasib opened 3 years ago

Takhmasib commented 3 years ago

Hi, I'm trying to run the code. I'm using Pure Thermal 2. everything works fine, but at the end I get uvc_open error. Does anyone have an idea? How I can solve that and what is the base of the problem?

shinya-ohtani commented 3 years ago

If you are talking about something other than macOS 12, I'm not sure what the cause is. However, if you are trying to run on macOS 12, this seems to be a fundamental problem that cannot be solved forever. It seems to me that we have three options in this case

  1. run your app on macOS 11.6.2 or earlier.
  2. run your app with root privileges on macOS 12 (11.6.3 or later)
  3. run VMware on macOS 12 and run your app on ubuntu or other OS on it

The following issue is a good reference for this problem. I have yet to find a solution to this problem other than the above. libusb/libusb#1014

shinya-ohtani commented 3 years ago

Help me

If anyone has succeeded in capturing 16-bit temperature images from PureThermal module on macOS 12, I would appreciate it if you could tell me how to do it.

Detail

uvc_open() is a function of a library called libuvc that the sample code for PureThermal is using to capture 16-bit temperature images from a USB-connected PureThermal. And that libuvc is using another library called libusb for USB access.

As I understand, when we try to capture images using libusb + libuvc on macOS, we need to unload some of the existing loaded kernel drivers to avoid macOS conflicts, and reload those kernel drivers when we exit the app.

Refer to Apple macOS API: IOUSBHostObjectInitOption's 'deviceCapture'

Callers must have either the com.apple.vm.device-access entitlement and the IOUSBHostDevice object from IOServiceAuthorize(::) authorization, or have root privileges. Using this option terminates all clients and drivers of the IOUSBHostDevice and associated IOUSBHostInterface clients, as well as the caller. Upon destroy() of the IOUSBHostDevice, the device resets and IOUSBHostInterface reregisters for IOKit matching.

Until macOS 11, the macOS API could be used with user privileges without difficulty, but in macOS 12, only apps with root privileges or com.apple.vm.device-access entitlement can use this API.

Also, if this entitlement is forged for temporary use, the app will not be allowed to run by macOS. Therefore, it is necessary to have this entitlement officially granted by Apple, but Apple seems to have a policy of not granting it to any apps other than VMware, and not to regular applications. I also applied, but was rejected.

A potential workaround seems to be the suggestion to use libusb/hidapi, another cross-platform library, instead of libusb libuvc. However, hidapi uses IOKit's IOHIDManager, a macOS API, to create the connected device list. This API does not recognize the PureThermal module as a HID, so we could not use hidapi to capture images from PureThermal.

CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);

The second possible workaround is to give up the cross-platform library and use macOS AVFoundation's AVCapture directly. So I tried this API. However, although the PureThermal module's RGB image profile was listed by that API, the 16-bit temperature image profile was not listed. So, of course, the RGB image capture was successful, but the 16-bit temperature image capture was not.

AVCaptureDevice.DiscoverySession(
        deviceTypes: [AVCaptureDevice.DeviceType.externalUnknown],
        mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)

So, unfortunately, my final remaining options are as mentioned before.

  1. run your app on macOS 11
  2. run your app with root privileges on macOS 12
  3. run VMware on macOS 12 and run your app on ubuntu or other OS on it

If anyone has succeeded in capturing 16-bit temperature images from PureThermal module on macOS 12, I would appreciate it if you could tell me how to do it.

mctosima commented 3 years ago

I experienced the same issue. Unable to capture the video stream using Python and PT2 + Lepton 3 using MacOS Monterey.

Takhmasib commented 3 years ago

Thanks for the information. I am using a Raspberry Pi 4. The problem was with the camera. I just changed it and it worked. Thank you

shinya-ohtani commented 2 years ago

I have confirmed that there is a version of uvc_open() that fails even on macOS 11. To be precise, uvc_open() fails on 11.6.3 or later due to com.apple.vm.device-access entitlement issue.

(I also modified my comment at the top of this issue.)

kekiefer commented 2 years ago

I've tried this again, and as of now some of this has been cleaned up. At least for MacOS 12.4, with libuvc/libuvc master and libusb from homebrew, and the master+libuvc-upstream branch here, it is working for me.

mctosima commented 2 years ago

hi @shinya-ohtani , any update on this?

strangely, i could route the purethermal2 video stream through OBS software then start the virtual camera from OBS and stream those virtual camera using cv2. But the bit depth is only 8 bit.

rvegele commented 1 year ago

Hi @mctosima could you share how you did this? I'm trying the same but trying to find the virtual camera index (for python) if proving problematic.

I end up with the error: [ERROR:0@1.273] global cap.cpp:323 open VIDEOIO(AVFOUNDATION): raised unknown C++ exception! while running opencv-capture.py

mctosima commented 1 year ago

Hi @mctosima could you share how you did this? I'm trying the same but trying to find the virtual camera index (for python) if proving problematic.

I end up with the error: [ERROR:0@1.273] global cap.cpp:323 open VIDEOIO(AVFOUNDATION): raised unknown C++ exception! while running opencv-capture.py

Really sorry that I didn't notice that there's a reply to me.

Here's how you can connect to OBS virtual camera.

  1. First you have to install the OBS and its virtual camera plugins
  2. Then to make ur life easier, just use the pygrabber to set the camera device. See example below:
pip install pygrabber==0.1
from pygrabber.dshow_graph import FilterGraph

graph = FilterGraph()

print(graph.get_input_devices())# list of camera device 

try:
    device =graph.get_input_devices().index("name camera that I want to use it ")

except ValueError as e:

    device = graph.get_input_devices().index("Integrated Webcam")#use default camera if the name of the camera that I want to use is not in my list

vid=cv2.VideoCapture(device)
badgerhoneymoon commented 7 months ago

instead of OBS, VLC streaming works as well, but the delay is huge though

Breezewrf commented 7 months ago

Hi

I also encountered this problem today, and I am pleased to introduce my solution, hope this helpful for others:

Trouble intro

  1. My device is Lepton 3.5 using PureThermal Mini Pro JST-SR, connected to Ubuntu 22.
  2. Firstly build the libusb and libuvc.
  3. Then I meet uvc_open error both when exec uvc-deviceinfo.py and uvc-radiometry.py, the res of res = libuvc.uvc_open(dev, byref(devh)) is -3.

Solution

  1. type lsusb in terminal, find the thermal camera device: Bus 001 Device 039: ID 1e4e:0100 Cubeternet WebCam
  2. cd /etc/udev/rules.d, sudo gedit 70-thermal-camera.rules, SUBSYSTEM=="usb", ATTRS{idVendor}=="1e4e", ATTRS{idProduct}=="0100",MODE="0666"
  3. Unplug and Re-insert the camera USB
  4. Reference: https://blog.csdn.net/xiaxiangnanxp1989/article/details/8605611