Skip to content

Conversation

@joeyhlu
Copy link
Contributor

@joeyhlu joeyhlu commented Dec 4, 2024

No description provided.

Copy link
Member

@maxlou05 maxlou05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an empty file. Also, we don't import classes directly, we only import the modules then use module.Class later on in the code. Manually include the imports in each file, and only 1 module per line

class OpenCVCameraConfig:
"""
Placeholder
""" No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a newline at the end of the file. Also, are empty classes are not allowed in Python, you need to add pass to it otherwise it throws error.

camera_option: CameraOption, width: int, height: int
) -> tuple[True, base_camera.BaseCameraDevice] | tuple[False, None]:
camera_option: CameraOption, width: int, height: int, config: None
) -> tuple[True, base_camera.BaseCameraDevice | None] | tuple[False, None]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should never return True if returning Camera device failed to instatiate, that's why it was setup like this. No need to change the output type

def create_camera(
camera_option: CameraOption, width: int, height: int
) -> tuple[True, base_camera.BaseCameraDevice] | tuple[False, None]:
camera_option: CameraOption, width: int, height: int, config: None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config type should be [camera_config.PiCameraConfig | camera_config.OpenCVCameraConfig]


def test_opencv_camera_config() -> None:
config = OpenCVCameraConfig()
assert config is not None No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add empty line at end of file

Comment on lines 39 to 45
config = camera.create_still_configuration(
camera_config = camera.create_still_configuration(
{"size": (width, height), "format": "RGB888"}
)
camera.configure(config)
camera.configure(camera_config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this portion to create_preview_configuration(). If you don't want to make a new name, you can also combine the whole thing together into a line

"OpenCVCameraConfig",
"CameraOpenCV",
"CameraPiCamera2",
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add newline to the bottom of the file

"""
Dictionary containing camera controls.
"""
controls: dict[str, int | float | None] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to have a None type, as it just won't exist in the dictionary

Copy link
Member

@maxlou05 maxlou05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

controls["Contrast"] = self.contrast
if self.lens_position is not None:
controls["LensPosition"] = self.lens_position
controls["AfMode"] = controls.AfModeEnum.Manual
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to import the library at the top of this file:

try:
    from libcamera import controls  # This is a pre-installed library on the Rpi5
except:
    pass

But, this means you probably should rename your dictionary to something else like camera_controls or settings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need this file, just modify test_camera_picamera2.py, test_camera_opencv.py and test_camera_qr_exmaple.py (all in the tests/integration folder) to use the camera configs.


from modules.camera import camera_factory

from modules.camera.camera_config import PiCameraConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space above this line, and also should be from modules.camera import camera_config since we import modules

"""
match camera_option:
case CameraOption.OPENCV:
return camera_opencv.CameraOpenCV.create(width, height)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In camera_opencv.py just make create take in a config Object as well, and then write a comment #TODO: apply camera configs to camera here to indicate it will be done sometime in the future

Copy link
Member

@maxlou05 maxlou05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

@abc.abstractmethod
def create(
cls, width: int, height: int
cls, width: int, height: int, config: object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of config should be camera_config.PiCameraConfig | camera_config.OpenCVCameraConfig

try:
from libcamera import controls # This is a pre-installed library on the Rpi5
except ImportError:
pass # pylint: disable=bare-except
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you still need the bare-except disable? If not, then remove it

import numpy as np

from . import base_camera
from .camera_config import OpenCVCameraConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just from . import camera_config

def create(
cls, width: int, height: int, config: OpenCVCameraConfig
) -> "tuple[True, CameraOpenCV] | tuple[False, None]":
# TODO: apply camera configs to camera here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this comment to line 32, where you wrote the placeholder?

@classmethod
def create(cls, width: int, height: int) -> "tuple[True, CameraOpenCV] | tuple[False, None]":
def create(
cls, width: int, height: int, config: OpenCVCameraConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make config an optional argument defaulted to None


from modules.camera import camera_factory

from modules.camera import camera_factory, camera_config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate into 2 lines, and also keep 2 spaces between the imports and global constants.

from modules.camera import camera_factory
from modules.camera import camera_config


IMAGE_LOG_PREFIX = ...

Comment on lines 20 to 24
try:
assert config is not None
except AssertionError as e:
print(f"Configuration test failed: {e}")
return -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to just keep the assert line. We don't need to catch exception here, as this is an integration test.

Comment on lines 21 to 28
try:
assert config.exposure_time == 250
assert config.contrast == 1.0
assert config.analogue_gain == 64.0
assert config.lens_position is None
except AssertionError as e:
print(f"Configuration test failed: {e}")
return -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, just keep the assert lines, no need to catch the exception since this is a test


from modules.camera import camera_factory

from modules.camera import camera_factory, camera_config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: separate into 2 lines and make sure 2 spaces between imports and global constants

import cv2

from modules.camera import camera_factory
from modules.camera import camera_factory, camera_config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate into 2 lines

Copy link
Member

@maxlou05 maxlou05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

Comment on lines 7 to 8
from . import camera_configurations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move these to line 15 (so that our libraries are grouped together, and 2 lines before constants)

Copy link
Member

@maxlou05 maxlou05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved!

Copy link
Member

@maxlou05 maxlou05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

@joeyhlu joeyhlu merged commit 1c31b5c into main Dec 4, 2024
1 check passed
@joeyhlu joeyhlu deleted the fixed-camera-config-classes branch December 4, 2024 22:48
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.

3 participants