Skip to content

Commit

Permalink
Initial lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
WillB97 committed Sep 29, 2023
1 parent ffb13d1 commit a31141b
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 60 deletions.
10 changes: 8 additions & 2 deletions april_vision/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""An AprilTags wrapper with camera discovery and axis conversion."""
# ruff: noqa: E402
import os

os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
Expand All @@ -8,8 +9,13 @@
from .detect_cameras import CalibratedCamera, find_cameras
from .frame_sources import FrameSource, USBCamera
from .helpers import generate_marker_size_mapping
from .marker import (CartesianCoordinates, Marker, Orientation,
PixelCoordinates, SphericalCoordinate)
from .marker import (
CartesianCoordinates,
Marker,
Orientation,
PixelCoordinates,
SphericalCoordinate,
)
from .utils import Frame
from .vision import Processor

Expand Down
18 changes: 13 additions & 5 deletions april_vision/cli/calibrate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Camera calibration script.
"""
"""Camera calibration script."""
import argparse
import logging
from datetime import datetime
Expand All @@ -19,7 +17,8 @@


class CalBoard:
"""Class used to represent a calibration board"""
"""Class used to represent a calibration board."""

def __init__(
self,
rows: int,
Expand All @@ -38,10 +37,11 @@ def __init__(
def corners_from_id(self, marker_id: int) -> List[Tuple[float, float, float]]:
"""
Takes an input of a marker ID and returns the coordinates of the corners of the marker.
The coordinates are 3D real world positions, top left of the board is 0,0,0.
The Z coordinate of the board is always zero.
The list of co-ords are in the order:
[bottom_left, bottom_right, top_right, top_left]
bottom_left, bottom_right, top_right, top_left
"""
marker_pixel_size = self.marker_size / self.marker_details.width_at_border

Expand Down Expand Up @@ -90,6 +90,7 @@ def parse_detections(
) -> Tuple[List[Tuple[float, float, float]], List[Tuple[float, float]]]:
"""
Pairs up 2D pixel corners with 3D real world co-ords.
Takes the input of marker detections and the board design and outputs two lists
where board_obj_points[i] pairs with board_img_points[i].
"""
Expand All @@ -108,6 +109,7 @@ def parse_detections(


def main(args: argparse.Namespace) -> None:
"""Main function for calibrate command."""
# Setup the camera
video_dev = cv2.VideoCapture(args.index)

Expand Down Expand Up @@ -188,6 +190,12 @@ def write_cal_file(
avg_reprojection_error: float,
vidpid: Optional[str] = None,
) -> None:
"""
Write the calibration data to an XML file.
This file can be loaded by the detect_cameras module.
The file is also compatible with the OpenCV calibration module.
"""
LOGGER.info("Generating calibration XML file")
output_filename = cal_filename
if not output_filename.lower().endswith(".xml"):
Expand Down
1 change: 1 addition & 0 deletions april_vision/cli/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


def parse_properties(args: argparse.Namespace) -> List[Tuple[int, int]]:
"""Parse the camera properties supplied on the command line."""
props = []

if args.set_fps is not None:
Expand Down
2 changes: 1 addition & 1 deletion april_vision/cli/marker_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
def create_subparser(subparsers: argparse._SubParsersAction) -> None:
"""
Marker_generator command parser.
Add multiple subparsers to deal with different modes of page generation.
"""

parser = subparsers.add_parser(
"marker_generator",
description="Generate markers",
Expand Down
3 changes: 2 additions & 1 deletion april_vision/cli/marker_generator/marker_modes/mode_cal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Marker_generator subparser CAL_BOARD used to generate a calibration board."""
import argparse
import logging

Expand All @@ -14,7 +15,7 @@


def main(args: argparse.Namespace) -> None:
"""Generate a calibration board"""
"""Generate a calibration board."""
tag_data = get_tag_family(args.marker_family)
LOGGER.info(tag_data)

Expand Down
3 changes: 2 additions & 1 deletion april_vision/cli/marker_generator/marker_modes/mode_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Marker_generator subparser IMAGE used to generate an image of a marker."""
import argparse
import logging

Expand All @@ -14,7 +15,7 @@


def main(args: argparse.Namespace) -> None:
"""Generate a marker image"""
"""Generate a marker image."""
tag_data = get_tag_family(args.marker_family)
LOGGER.info(tag_data)

Expand Down
14 changes: 11 additions & 3 deletions april_vision/cli/marker_generator/marker_modes/mode_single.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Marker_generator subparser SINGLE used to generate a PDF of a marker."""
import argparse
import logging

Expand All @@ -7,14 +8,21 @@
from april_vision.marker import MarkerType

from ..marker_tile import MarkerTile
from ..utils import (DEFAULT_COLOUR, DEFAULT_FONT, DEFAULT_FONT_SIZE, DPI,
PageSize, mm_to_pixels, parse_marker_ranges)
from ..utils import (
DEFAULT_COLOUR,
DEFAULT_FONT,
DEFAULT_FONT_SIZE,
DPI,
PageSize,
mm_to_pixels,
parse_marker_ranges,
)

LOGGER = logging.getLogger(__name__)


def main(args: argparse.Namespace) -> None:
"""Generate a single marker on a page with the provided arguments"""
"""Generate a single marker on a page with the provided arguments."""
tag_data = get_tag_family(args.marker_family)
LOGGER.info(tag_data)

Expand Down
15 changes: 12 additions & 3 deletions april_vision/cli/marker_generator/marker_modes/mode_tile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Marker generator mode to generate a PDF with multiple markers per page."""
import argparse
import logging

Expand All @@ -7,14 +8,21 @@
from april_vision.marker import MarkerType

from ..marker_tile import MarkerTile
from ..utils import (DEFAULT_COLOUR, DEFAULT_FONT, DEFAULT_FONT_SIZE, DPI,
PageSize, mm_to_pixels, parse_marker_ranges)
from ..utils import (
DEFAULT_COLOUR,
DEFAULT_FONT,
DEFAULT_FONT_SIZE,
DPI,
PageSize,
mm_to_pixels,
parse_marker_ranges,
)

LOGGER = logging.getLogger(__name__)


def main(args: argparse.Namespace) -> None:
"""Generate a page of multiple markers with the provided arguments"""
"""Generate a page of multiple markers with the provided arguments."""
tag_data = get_tag_family(args.marker_family)
LOGGER.info(tag_data)

Expand Down Expand Up @@ -147,6 +155,7 @@ def main(args: argparse.Namespace) -> None:
def create_subparser(subparsers: argparse._SubParsersAction) -> None:
"""
Marker_generator subparser TILE.
Used to generate a PDF with multiple markers per page.
"""
parser = subparsers.add_parser("TILE")
Expand Down
36 changes: 26 additions & 10 deletions april_vision/cli/marker_generator/marker_tile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
Used to generate an image tile which can be customised.
These image tiles can be arranged on a page in the different modes.
"""
from typing import NamedTuple

import numpy as np
Expand All @@ -11,12 +16,16 @@


class coord(NamedTuple):
"""Simple class to store coordinates."""

x: int
y: int


def generate_tag_array(tag_data: ApriltagFamily, tag_id: int) -> NDArray:
"""
Generate a marker array for a given tag family and tag ID.
Uses the tag family object to generate a marker, returns this data as a 2d numpy array
where each of the cells is 1 pixel of the marker.
"""
Expand Down Expand Up @@ -53,8 +62,10 @@ def generate_tag_array(tag_data: ApriltagFamily, tag_id: int) -> NDArray:
class MarkerTile:
"""
Used to generate an image tile which can be customised.
These image tiles can be arranged on a page in the different modes
These image tiles can be arranged on a page in the different modes.
"""

def __init__(
self,
tag_data: ApriltagFamily,
Expand All @@ -64,7 +75,8 @@ def __init__(
):
"""
Generate a basic marker, no overlays, scaled to the correct size.
The marker PIL.Image can be accessed via MarkerTile.image
The marker PIL.Image can be accessed via MarkerTile.image.
"""
self.tag_data = tag_data
self.tag_id = tag_id
Expand Down Expand Up @@ -101,8 +113,9 @@ def add_border_line(
border_colour: str,
) -> None:
"""
Add a line arround the board of the marker,
changes the current marker design in place.
Add a line around the border of the marker.
This changes the current marker design in place.
"""
bordered_image = ImageOps.expand(
self.image,
Expand All @@ -129,8 +142,9 @@ def add_centre_ticks(
tick_colour: str,
) -> None:
"""
Add tick lines half way along the border of the marker,
changes the current marker design in place.
Add tick lines half way along the border of the marker.
This changes the current marker design in place.
"""
img_size = self.image.size[0]
image_draw = ImageDraw.Draw(self.image)
Expand Down Expand Up @@ -172,8 +186,9 @@ def add_id_number(
text_colour: str,
) -> None:
"""
Add the ID number in the top left square of the white border,
changes the current marker design in place.
Add the ID number in the top left square of the white border.
This changes the current marker design in place.
"""
# Add text to the image
marker_square_size = mm_to_pixels(self.pixel_size)
Expand Down Expand Up @@ -211,8 +226,9 @@ def add_description_border(
double_text: bool = False,
) -> None:
"""
Expand the marker by one marker square and add description text to this area,
changes the current marker design in place.
Expand the marker by one marker square and add description text to this area.
This changes the current marker design in place.
"""
marker_square_size = mm_to_pixels(self.pixel_size)

Expand Down
12 changes: 6 additions & 6 deletions april_vision/cli/marker_generator/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Utility functions for the marker generator."""
import logging
from enum import Enum
from typing import List, Tuple
Expand All @@ -17,6 +18,7 @@
def parse_marker_ranges(marker_family: ApriltagFamily, range_str: str) -> List[int]:
"""
Utility function to parse the provided range of markers.
Also checks bounds against the marker family.
"""
if range_str == "ALL":
Expand All @@ -37,24 +39,22 @@ def parse_marker_ranges(marker_family: ApriltagFamily, range_str: str) -> List[i


def mm_to_pixels(mm: float) -> int:
"""
Convert millimeters to pixels
"""
"""Convert millimeters to pixels."""
inches = mm / 25.4
return int(inches * DPI)


class PageSize(Enum):
"""
Enum to define the dimentions of different page sizes
"""
"""Enum to define the dimentions of different page sizes."""

A3 = (297, 420)
A3L = (420, 297)
A4 = (210, 297)
A4L = (297, 210)

@property
def pixels(self) -> Tuple[int, int]:
"""Return the page size in pixels."""
return (
mm_to_pixels(self.value[0]),
mm_to_pixels(self.value[1]),
Expand Down
1 change: 1 addition & 0 deletions april_vision/cli/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


def create_subparser(subparsers: argparse._SubParsersAction) -> None:
"""Subparser for the tools commands."""
parser = subparsers.add_parser(
"tools",
description="A collection of useful tools",
Expand Down
6 changes: 6 additions & 0 deletions april_vision/cli/tools/family_details.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
Provide the details about a marker family.
The values are extracted from the AprilTag binary.
"""
import argparse
import logging

Expand All @@ -9,6 +14,7 @@


def main(args: argparse.Namespace) -> None:
"""Provide the details about a marker family."""
tag_data = get_tag_family(args.tag_family)
print(tag_data)

Expand Down
2 changes: 2 additions & 0 deletions april_vision/cli/tools/list_cameras.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""List out the available cameras connected to the system."""
import argparse

from tabulate import tabulate
Expand All @@ -7,6 +8,7 @@


def main(args: argparse.Namespace) -> None:
"""List out the available cameras connected to the system."""
cameras = find_cameras(calibrations, include_uncalibrated=True)
print(tabulate(cameras, headers="keys"))

Expand Down
Loading

0 comments on commit a31141b

Please sign in to comment.