tne-lab / py-behav-box-v2

Pybehave is an open source software interface and framework for controlling behavioral experiments in neuroscience and psychology.
https://py-behav-box-v2.readthedocs.io/en/latest/
MIT License
3 stars 8 forks source link

webcam setup #81

Closed alustig3 closed 5 months ago

alustig3 commented 5 months ago

I'm having trouble getting a usb webcam working with my task.

I have added the webcam as a source, here is my pybehave.ini contents:

[General]
sources="{\"testcam\": 'VideoSource(\"640\",\"480\",\"1\",\"1\",)'}"
refresh_gui=true
n_chamber=2

[pygame]
n_row=1
offset="(426.6666666666667, 30)"
n_col=2
w=541
h=1082

[pyqt]
w=341
h=1082

[subjectConfig]
sequence\default\inter_trial_interval=.1
sequence\default\rwd_seq=\"LLRRR\"

my address file:

addresses = AddressFile()
addresses.add_component("cam", "Video", "testcam", 0, None, {'fr': 30, 'row': 0, 'col': 0, 'row_span': 0, 'col_span': 0})

I am getting the following error when starting the task:

Exception in thread Thread-1 (run_):
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\karpo\Desktop\pybehave_clone\pyb\Lib\site-packages\pybehave\Sources\ThreadSource.py", line 40, in run_
    if not self.handle_events(events):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\karpo\Desktop\pybehave_clone\pyb\Lib\site-packages\pybehave\Sources\Source.py", line 71, in handle_events
    self.write_component(event.comp_id, event.value)
  File "C:\Users\karpo\Desktop\pybehave_clone\pyb\Lib\site-packages\pybehave\Sources\VideoSource.py", line 188, in write_component
    self.writers[component_id] = cv2.VideoWriter(path, fourcc, self.fr[component_id],
                                                               ~~~~~~~^^^^^^^^^^^^^^
KeyError: 'cam-0-0'
  File "C:\Users\karpo\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner

    self.run()
  File "C:\Users\karpo\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\karpo\Desktop\pybehave_clone\pyb\Lib\site-packages\pybehave\Sources\ThreadSource.py", line 40, in run_
    if not self.handle_events(events):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\karpo\Desktop\pybehave_clone\pyb\Lib\site-packages\pybehave\Sources\Source.py", line 71, in handle_events
    self.write_component(event.comp_id, event.value)
  File "C:\Users\karpo\Desktop\pybehave_clone\pyb\Lib\site-packages\pybehave\Sources\VideoSource.py", line 188, in write_component
    self.writers[component_id] = cv2.VideoWriter(path, fourcc, self.fr[component_id],
                                                               ~~~~~~~^^^^^^^^^^^^^^
KeyError: 'cam-0-0'

Using the following script, I have confirmed that the webcam is working at index 0 and has dimensions 640x480:

import cv2

cap = cv2.VideoCapture(0)

if not cap.isOpened():
    raise IOError("Cannot open webcam")

# Get the width and height of the frame
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"Width: {width}, Height: {height}")

while True:
    ret, frame = cap.read()
    if not ret:
        break

    cv2.imshow('USB Camera', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
theonlydvr commented 5 months ago

I think asyncio or qasync is working differently with either Python 3.10 or an update in the package which broke some of the task scheduling. I'm looking into this now and will hopefully have a fix quickly.

theonlydvr commented 5 months ago

I fixed this on my end with the most recent commit, looks like it's due to a bug on one of the more recent versions of qasync. The fix might be temporary depending on changes made on their end. I also addressed a bug in the metadata defaults so you may have to update your AddressFile (it didn't include the vid_type key).

alustig3 commented 5 months ago

the fix worked for me. Thanks!