
    |h                     ~    d dl mZ 	 d dlmZ d dlmZ dZ G d d	      Z	d
 Z
d Zy# e$ r dZ G d de      ZY %w xY w)    )multiscale_basic_features)NotFittedError)RandomForestClassifierTFc                       e Zd Zy)r   N)__name__
__module____qualname__     d/var/www/html/test/engine/venv/lib/python3.12/site-packages/skimage/future/trainable_segmentation.pyr   r      s    r   r   c                   *    e Zd ZdZddZd Zd Zd Zy)TrainableSegmentera  Estimator for classifying pixels.

    Parameters
    ----------
    clf : classifier object, optional
        classifier object, exposing a ``fit`` and a ``predict`` method as in
        scikit-learn's API, for example an instance of
        ``RandomForestClassifier`` or ``LogisticRegression`` classifier.
    features_func : function, optional
        function computing features on all pixels of the image, to be passed
        to the classifier. The output should be of shape
        ``(m_features, *labels.shape)``. If None,
        :func:`skimage.feature.multiscale_basic_features` is used.

    Methods
    -------
    compute_features
    fit
    predict
    Nc                 z    |+t         rt        dd      | _        || _        y t        d      || _        || _        y )Nd   )n_estimatorsn_jobszOPlease install scikit-learn or pass a classifier instanceto TrainableSegmenter.)has_sklearnr   clfImportErrorfeatures_func)selfr   r   s      r   __init__zTrainableSegmenter.__init__%   sG    ;1s2N + "- 
 DH*r   c                 ^    | j                   t        | _         | j                  |      | _        y )N)r   r   features)r   images     r   compute_featuresz#TrainableSegmenter.compute_features2   s(    %!:D**51r   c                 h    | j                  |       t        || j                  | j                         y)a  Train classifier using partially labeled (annotated) image.

        Parameters
        ----------
        image : ndarray
            Input image, which can be grayscale or multichannel, and must have a
            number of dimensions compatible with ``self.features_func``.
        labels : ndarray of ints
            Labeled array of shape compatible with ``image`` (same shape for a
            single-channel image). Labels >= 1 correspond to the training set and
            label 0 to unlabeled pixels to be segmented.
        N)r   fit_segmenterr   r   )r   r   labelss      r   fitzTrainableSegmenter.fit7   s&     	e$fdmmTXX6r   c                 ~    | j                   t        | _         | j                  |      }t        || j                        S )a  Segment new image using trained internal classifier.

        Parameters
        ----------
        image : ndarray
            Input image, which can be grayscale or multichannel, and must have a
            number of dimensions compatible with ``self.features_func``.

        Raises
        ------
        NotFittedError if ``self.clf`` has not been fitted yet (use ``self.fit``).
        )r   r   predict_segmenterr   )r   r   r   s      r   predictzTrainableSegmenter.predictG   s9     %!:D%%e, 48844r   )NN)r   r   r	   __doc__r   r   r!   r$   r
   r   r   r   r      s    *+2
7 5r   r   c                 d    | dkD  }||   }| |   j                         }|j                  ||       |S )a_  Segmentation using labeled parts of the image and a classifier.

    Parameters
    ----------
    labels : ndarray of ints
        Image of labels. Labels >= 1 correspond to the training set and
        label 0 to unlabeled pixels to be segmented.
    features : ndarray
        Array of features, with the first dimension corresponding to the number
        of features, and the other dimensions correspond to ``labels.shape``.
    clf : classifier object
        classifier object, exposing a ``fit`` and a ``predict`` method as in
        scikit-learn's API, for example an instance of
        ``RandomForestClassifier`` or ``LogisticRegression`` classifier.

    Returns
    -------
    clf : classifier object
        classifier trained on ``labels``

    Raises
    ------
    NotFittedError if ``self.clf`` has not been fitted yet (use ``self.fit``).
    r   )ravelr!   )r    r   r   masktraining_datatraining_labelss         r   r   r   Z   s;    2 A:DTNMTl((*OGGM?+Jr   c                 v   | j                   }| j                  dkD  r| j                  d|d   f      } 	 |j                  |       }|j                  |dd       }|S # t        $ r t	        d      t
        $ rB}|j                  r/d|j                  d   v rt        |j                  d   dz   dz         |d}~ww xY w)	a  Segmentation of images using a pretrained classifier.

    Parameters
    ----------
    features : ndarray
        Array of features, with the last dimension corresponding to the number
        of features, and the other dimensions are compatible with the shape of
        the image to segment, or a flattened image.
    clf : classifier object
        trained classifier object, exposing a ``predict`` method as in
        scikit-learn's API, for example an instance of
        ``RandomForestClassifier`` or ``LogisticRegression`` classifier. The
        classifier must be already trained, for example with
        :func:`skimage.future.fit_segmenter`.

    Returns
    -------
    output : ndarray
        Labeled array, built from the prediction of the classifier.
       r   zWYou must train the classifier `clf` firstfor example with the `fit_segmenter` function.z#x must consist of vectors of lengthr   
zLMaybe you did not use the same type of features for training the classifier.N)shapendimreshaper$   r   
ValueErrorargs)r   r   shpredicted_labelserroutputs         r   r#   r#   z   s    * 
B}}q##RBL1;;x0 %%b"g.FM  
=
 	
  88=!L`a  Is   A B86=B33B8N)skimage.featurer   sklearn.exceptionsr   sklearn.ensembler   r   r   	Exceptionr   r   r#   r
   r   r   <module>r;      sS    5	17KH5 H5V@*e  K s   ' <<