def _sample_patch()

in src/open_vp_cal/framework/sample_patch.py [0:0]


    def _sample_patch(self) -> None:
        """
        Detect the macbeth chart in the patch and extract the samples for each of the swatches on the macbeth chart
        """
        first_patch_frame, last_patch_frame = self.calculate_first_and_last_patch_frame()
        # We trim a number of frames off either side of the patch to ensure we remove multiplexing
        sample_results = SamplePatchResults()
        samples = []
        white_balance_matrix = self.get_white_balance_matrix_from_slate()
        for frame_num in range(first_patch_frame + self.trim_frames,
                               (last_patch_frame - self.trim_frames) + 1):
            frame = self.led_wall.sequence_loader.get_frame(frame_num)
            sample_results.frames.append(frame)

            # Extract our region
            section_orig = frame.extract_roi(self.led_wall.roi)

            # White balance the images so we increase the detection likelihood of
            # success
            section_orig = imaging_utils.apply_matrix_to_img_buf(
                section_orig, white_balance_matrix
            )

            section_display_np_array = imaging_utils.image_buf_to_np_array(section_orig)
            imaging_utils.apply_color_converstion_to_np_array(
                section_display_np_array,
                str(self.led_wall.input_plate_gamut),
                "ACEScct",
            )

            # Run the detections
            detections = detect_colour_checkers_segmentation(
                section_display_np_array, additional_data=True)

            for colour_checker_swatches_data in detections:
                # Get the swatch colours
                swatch_colours, _, _ = colour_checker_swatches_data.values
                swatch_colours = np.array(swatch_colours, dtype=np.float32)

                # Reshape the number of swatches from a 24, 3 array to an x, y, 3 array
                num_swatches = swatch_colours.shape[0]
                factor_pairs = find_factors_pairs(num_swatches)
                x, y = factor_pairs[0]
                array_x_y_3 = swatch_colours.reshape(x, y, 3)

                # Convert the colours back to the input plate gamut
                imaging_utils.apply_color_converstion_to_np_array(
                    array_x_y_3,
                    "ACEScct",
                    str(self.led_wall.input_plate_gamut))

                # Inverse the white balance back to the original values
                inv_wb_matrix = np.linalg.inv(white_balance_matrix)
                array_x_y_3 = array_x_y_3 @ inv_wb_matrix


                # Convert From Input To reference gamut So all our samples are in ACES
                imaging_utils.apply_color_converstion_to_np_array(
                    array_x_y_3,
                    str(self.led_wall.input_plate_gamut),
                    str(self.led_wall.project_settings.reference_gamut)
                )

                # Reshape the array back to a 24, 3 array
                swatch_colours = array_x_y_3.reshape(num_swatches, 3)
                samples.append(swatch_colours)

        # Compute the mean for each tuple index across all tuples, if the
        # detection fails, and we get nans, then we replace the nans with black patches
        # as these are not used in the calibration directly
        averaged_tuple = np.mean(np.array(samples), axis=0)
        if not np.isnan(averaged_tuple).any():
            sample_results.samples = averaged_tuple.tolist()
        else:
            list_of_zeros = [[0.0, 0.0, 0.0] for _ in range(24)]
            sample_results.samples = list_of_zeros
        self.sample_results = [sample_results]