cv2.VideoCapture.read() prevent hidapi from receiving more data

asked 2020-07-07 09:25:34 -0600

updated 2020-07-07 17:12:23 -0600

I have a usb2 video camera and an hid usb device connected on the same board.

I can read data from the hid device using hidapi and it works fine.

However, as soon as I do:

success, frame = video_capture.read()

hid.read() will never return anything anymore. Freezes there forever (if I use a timeout of 0).

Any idea why it would do this? Does the read() function do anything to reserve the usb bus or anything?

EDIT1:

Some more info:

If I do video_capture.release() then I can read from hid again. Until I do video_capture.read() which re-freezes hid.

Also the video_capture.read() will block hid reads even if 2 different python scripts.

  • I start a first script that reads from hid in a loop first and it does it's thing reading and printing the result.
  • Then I start a second script that reads from the webcam.

Once the webcam script is started, the hid script freeze at read(). The webcam script is fine.

EDIT2:

This will reproduce the issue. It will print 1 hid report and then 1 time True and stop on the next hid read.

import cv2
import time
import hid


_hid = hid.device()
_hid.open(0x04D8, 0x00DD)

video_capture = cv2.VideoCapture(0)
video_capture.set(cv2.CAP_PROP_BUFFERSIZE, 1)
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)

while True:
    report = b"\x10"
    _hid.write(b"\0" + report + b"\0" * (64 - len(report)))
    print(_hid.read(64))

    success, frame = video_capture.read()
    print(success)
    time.sleep(1)

I'm running

  • Language => Python 3.8.3
  • OpenCV => 4.3.0
  • Operating System / Platform => Debian GNU/Linux 10 on armv7l
edit retag flag offensive close merge delete

Comments

have you tried with other usb devices (i.e a mouse) ?

an hid usb device

can you be more specific ?

berak gravatar imageberak ( 2020-07-07 09:56:30 -0600 )edit

Snippet:

# read back the answer
    print(f"Read the data")
    while True:
        d = hid.read(64)
        if d:
            print(d)
        else:
            break
print(f"Closing the device")
    hid.close()
supra56 gravatar imagesupra56 ( 2020-07-07 13:57:14 -0600 )edit

No I tried only the mcp2221a, I'll give a mouse a try.

Francois Gervais gravatar imageFrancois Gervais ( 2020-07-07 16:08:08 -0600 )edit