tiagocoutinho / linuxpy

Human friendly interface to linux subsystems using python
https://tiagocoutinho.github.io/linuxpy/
GNU General Public License v3.0
33 stars 5 forks source link

Code from example does not work #21

Closed ivanstepanovftw closed 2 months ago

ivanstepanovftw commented 3 months ago

/p/i/gaze/capture.py

from linuxpy.video.device import Device

if __name__ == '__main__':
    with Device.from_id(1) as cam:
        print(f"{cam=}")
        for i, frame in enumerate(cam):
            print(f"frame #{i}: {len(frame)} bytes")
            if i > 9:
                break

Outputs

/usr/bin/python3.12 /p/i/gaze/capture.py 
cam=<Device name=/dev/video1, closed=False>
Traceback (most recent call last):
  File "/p/i/gaze/capture.py", line 7, in <module>
    for i, frame in enumerate(cam):
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 749, in __iter__
    with VideoCapture(self) as stream:
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 1407, in __enter__
    self.open()
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 1435, in open
    self.buffer.open()
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 1537, in open
    buffers = self.buffer_manager.create_buffers(Memory.MMAP)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 1282, in create_buffers
    self.buffers = self.device.create_buffers(self.type, memory, self.size)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 774, in create_buffers
    return create_buffers(self.fileno(), buffer_type, memory, count)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 719, in create_buffers
    request_buffers(fd, buffer_type, memory, count)
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/video/device.py", line 459, in request_buffers
    ioctl(fd, IOC.REQBUFS, req)
  File "/home/i/.local/lib/python3.12/site-packages/linuxpy/ioctl.py", line 67, in ioctl
    fcntl.ioctl(fd, request, *args)
OSError: [Errno 22] Invalid argument

Process finished with exit code 1

Note I have /dev/video0 for infrared and /dev/video1 for RGB. Maybe I am doing something wrong?

ivanstepanovftw commented 3 months ago

No, no, no, everything is fine when I use /dev/video0. My bad.

from io import BytesIO

from linuxpy.video.device import Device
import matplotlib.pyplot as plt
from PIL import Image

if __name__ == '__main__':
    with Device.from_id(0) as cam:
        print(f"{cam=}")
        for i, frame in enumerate(cam):
            print(f"frame #{i}: {len(frame)} bytes")
            image = Image.open(BytesIO(bytes(frame)))
            plt.imshow(image)
            plt.show()
            if i > 9:
                break

Strange since I am using /dev/video1 in my C++ code...

BTW I have 5 /dev/video* devices, not sure why.

I will let you to close this issue if everything is OK. Thank you for this beautiful library. Love it.

Dipankar1997161 commented 3 months ago

BTW I have 5 /dev/video* devices, not sure why.

try using iter_video_capture_devices function present in llinuxpy.video.device and check the results. this saves you from using 'from_id'

tiagocoutinho commented 3 months ago

Can I close the issue?

ivanstepanovftw commented 1 month ago

Many thanks for this beautiful library, will recommend to everyone

ivanstepanovftw commented 1 month ago

Strange, when my camera is occupied, it still shows that camera is closed.

from linuxpy.video.device import Device, iter_video_capture_devices
devices = list(iter_video_capture_devices())
print(f"{devices=}")
devices=[<Device name=/dev/video2, closed=True>, <Device name=/dev/video0, closed=True>]