Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCVVideoCapture implementation #130

Merged
merged 9 commits into from
Feb 22, 2024
Merged

OpenCVVideoCapture implementation #130

merged 9 commits into from
Feb 22, 2024

Conversation

m-decoster
Copy link
Contributor

Describe your changes

Implement OpenCVVideoCapture based on the proposal in #81

Checklist

  • Have you modified the changelog?
  • If you made changes to the hardware interfaces, have you tested them using the manual tests?

@Victorlouisdg
Copy link
Contributor

Looks good, and seems to work perfectly with my webcam, a Zed2i as USB webcam, and an mp4 video file.

Some remarks:

  • I would rename generic_opencv/generic_opencv.py to opencv_videocapture/opencv_videocapture.py
  • OpenCV opens all video streams at 640x480 by default. I've found you can use the command below to find the supported resolutions and framerates. However it would be a nice-to-have to open at maximum resolution by default, but that doesn't seem super trivial to implement, so we can skip that for now.
  • When using video files, we raise a Runtime error at the end. Maybe we want to explore the use cases of video files further and have a different ("softer") way to signal the end of a stream. I'm thinking e.g. running a keypoint detector on a prerecorded video and saving the result to a video as well.
  • When you supply a path to a non-existent video file, we raise RuntimeError("Cannot open camera"). We could improve that message but it's not crucial.

Command to list available resolutions and framerates of a camera:

 v4l2-ctl -d /dev/video0 --list-formats-ext

@m-decoster
Copy link
Contributor Author

I would rename generic_opencv/generic_opencv.py to opencv_videocapture/opencv_videocapture.py

Okay, I was unsure about the filename.

OpenCV opens all video streams at 640x480 by default. I've found you can use the command below to find the supported resolutions and framerates. However it would be a nice-to-have to open at maximum resolution by default, but that doesn't seem super trivial to implement, so we can skip that for now.

We can do the same as with the Realsense and Zed cameras: allow the user to select a desired resolution and FPS.

capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
capture.set(cv2.CAP_PROP_FPS, fps)

When using video files, we raise a Runtime error at the end. Maybe we want to explore the use cases of video files further and have a different ("softer") way to signal the end of a stream. I'm thinking e.g. running a keypoint detector on a prerecorded video and saving the result to a video as well.

We could add a property that indicates whether the camera still has frames to produce?

When you supply a path to a non-existent video file, we raise RuntimeError("Cannot open camera"). We could improve that message but it's not crucial.

This should be fairly simple to do

@m-decoster
Copy link
Contributor Author

@Victorlouisdg When setting the desired resolution for a webcam, OpenCV silently selects the closest match supported by the webcam. Do we want to log a warning or raise an error in this case?

@m-decoster m-decoster marked this pull request as draft February 14, 2024 11:48
@Victorlouisdg
Copy link
Contributor

OpenCV silently selects the closest match supported by the webcam. Do we want to log a warning or raise an error in this case?

I guess we can use it with this behavior until it causes problems.

We could add a property that indicates whether the camera still has frames to produce?

Maybe we could just return None to signal that there are not more images, e.g:

camera = OpenCVVideoCapture(("video.mp4",))

while True:
    image = camera.get_rgb_image()
    if image == None:
        break
    cv2.imwrite(image, "image.png")

@Victorlouisdg
Copy link
Contributor

I discussed with Thomas and as we don't want to change the RGBCamera interface, we would keep the exception when calling get_rgb_image(), so usage would be like so:

while True:
    try:
        image = camera.get_rgb_image()
    except EOFError:
        break
    cv2.imwrite(image, "image.png")

It's a bit ugly, but you only need to add that to scripts where you want to support using video files as RGBCamera, which will probably be quite rare anyways.

@m-decoster m-decoster marked this pull request as ready for review February 22, 2024 14:12
@m-decoster m-decoster merged commit 33a5c7c into main Feb 22, 2024
25 checks passed
@m-decoster m-decoster deleted the opencv_camera branch February 22, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants