leaf.data_prep

Module Contents

leaf.data_prep.N_QR_CODES: int = 8
leaf.data_prep.determine_parameters(img: numpy.array) dict[str:int]

Determine cropping parameters based on image resolution.

Supports A4 scanned at 600dpi and 1200dpi. Raises an error if the resolution is unsupported.

Parameters:

img (np.array) – Input image as a NumPy array.

Returns:

Dictionary with keys ‘cropping_size’, ‘scale_factor’, ‘qr_offset_x’, and ‘qr_offset_y’.

Return type:

dict[str, int]

Raises:

Exception – If image resolution does not match predefined formats.

leaf.data_prep.is_hidden_file(file_path: str) bool

Check if a file is hidden (Unix-style or Windows).

Parameters:

file_path (str) – Full path to the file.

Returns:

True if the file is hidden, False otherwise.

Return type:

bool

leaf.data_prep.check_file(file_path: str) bool

Validate if the file exists, is not hidden, and is a readable image.

Parameters:

file_path (str) – Full path to the file.

Returns:

True if valid, False otherwise.

Return type:

bool

leaf.data_prep.prepare_folder(folder_path: str, export_path: str, error_logs_path: str, manual_correction: bool = False, debug: bool = False, correction_export_path: str = None) None

Process a folder of images for QR detection and cropping.

Optionally allows manual correction of failed detections.

Parameters:
  • folder_path (str) – Path to the folder containing input images.

  • export_path (str) – Path to export cropped image patches.

  • error_logs_path (str) – Path to write error logs.

  • manual_correction (bool, optional) – If True, enables manual correction. Defaults to False.

  • debug (bool, optional) – If True, enables debug output. Defaults to False.

  • correction_export_path (str, optional) – Path for corrected exports if manual correction is enabled.

leaf.data_prep.correction(error_logs_path: pathlib.Path, correction_export_path: pathlib.Path) None

Launch an interactive tool for manual correction of image crops.

Parameters:
  • error_logs_path (Path) – Path to the file listing failed detections.

  • correction_export_path (Path) – Path to export manually cropped patches.

leaf.data_prep.find_qr_codes(image_path: str, debug: bool = False) List[Dict[str, object]]

Detect QR codes in an image and return their info.

Parameters:
  • image_path (str) – Path to the image file.

  • debug (bool, optional) – If True, shows debug visualization. Defaults to False.

Returns:

List of QR code dictionaries with ‘coordinates’ and ‘value’.

Return type:

List[Dict[str, object]]

leaf.data_prep.crop_qr_code_patches(image_path: str, qr_codes_info: List[Dict[str, object]], save_dir: pathlib.Path, debug: bool = False) None

Crop patches around detected QR codes and save them.

Parameters:
  • image_path (str) – Path to the input image.

  • qr_codes_info (List[Dict[str, object]]) – List of QR code metadata with coordinates and values.

  • save_dir (Path) – Directory to save cropped patches.

  • debug (bool, optional) – If True, shows cropped patches. Defaults to False.

class leaf.data_prep.BoundingBoxSelector(image_path: str, qr_codes: List[Dict[str, object]], box_size: Tuple[int, int], scale_factor: float, export_dir: pathlib.Path)

GUI tool for manually selecting and exporting image patches based on bounding boxes.

Initialize the bounding box selector.

Parameters:
  • image_path (str) – Path to the image being processed.

  • qr_codes (List[Dict[str, object]]) – List of detected QR codes.

  • box_size (Tuple[int, int]) – Width and height of the crop box.

  • scale_factor (float) – Scale factor for display.

  • export_dir (Path) – Directory to export cropped patches.

original_image: numpy.ndarray
image: numpy.ndarray
orig_box_size: Tuple[int, int]
box_size: Tuple[int, int]
user_boxes = None
cid
qr_codes
scale_factor
selected_box = None
export_dir
reset_button
confirm_button
done_button
text_box
filename = None
on_press(event)

Handle mouse click event to define a bounding box.

show()

Display the interactive selector interface.

reset(event)

Clear the currently drawn bounding box.

confirm(event)

Confirm the bounding box and trigger patch save.

done(event)

Close the interface after completion.

submit(text)

Handle filename input for saved patch.

Parameters:

text (str) – Filename to save the cropped patch as.