
    2Vh&'                     l    d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ  ed       G d de             Z	y	)
    )keras_export)BaseImagePreprocessingLayer)clip_to_image_size)convert_format)image_utilszkeras.layers.CenterCropc                   d     e Zd ZdZdZd fd	ZddZddZ	 ddZ	 ddZ	ddZ
d	 Z fd
Z xZS )
CenterCropa  A preprocessing layer which crops images.

    This layers crops the central portion of the images to a target size. If an
    image is smaller than the target size, it will be resized and cropped
    so as to return the largest possible window in the image that matches
    the target aspect ratio.

    Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`).

    Input shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., height, width, channels)`, in `"channels_last"` format,
        or `(..., channels, height, width)`, in `"channels_first"` format.

    Output shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., target_height, target_width, channels)`,
        or `(..., channels, target_height, target_width)`,
        in `"channels_first"` format.

    If the input height/width is even and the target height/width is odd (or
    inversely), the input image is left-padded by 1 pixel.

    **Note:** This layer is safe to use inside a `tf.data` pipeline
    (independently of which backend you're using).

    Args:
        height: Integer, the height of the output shape.
        width: Integer, the width of the output shape.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, width)`. It defaults to the
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json`. If you never set it, then it will be
            `"channels_last"`.
    Fc                 D    t        |   dd|i| || _        || _        y )Ndata_format )super__init__heightwidth)selfr   r   r   kwargs	__class__s        n/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/center_crop.pyr   zCenterCrop.__init__9   s&    ;[;F;
    c                     t        |t              r|d   }n|}| j                  j                  j	                  |      }d|iS )Nimagesinput_shape)
isinstancedictbackendcoreshape)r   datatrainingseedr   r   s         r   get_random_transformationz$CenterCrop.get_random_transformation>   s?    dD!(^FF!!''/u%%r   c                     |S Nr   )r   labelstransformationr   s       r   transform_labelszCenterCrop.transform_labelsF   s    r   c                      fd} fd}|d   } ||      \  }}t        | j                  d||      }| j                  z
  }	| j                  z
  }
|	dk\  rL|
dk\  rGt	        |	dz        }t	        |
dz        }| j                  z   }| j                  z   } ||||||      }n|}|} j                  } j                  }t	        t        ||z        |z        }t        t        ||      d      }t	        t        ||z        |z        }t        t        ||      d      }t	        t        ||z
        dz        }t	        t        ||z
        dz        }|}|}||z   }||z   } ||||||      }t        |dd	||      }t        |d	d j                   j                        }t        | j                   j                  d
      }t        |d j                   j                   j                        }|S )Nc                 Z    j                   dk(  r| d   }| d   }||fS | d   }| d   }||fS )Nchannels_first)r   )r   input_heightinput_widthr   s      r   _get_height_widthz>CenterCrop.transform_bounding_boxes.<locals>._get_height_widthL   sP    #33*2)"o  ,,  +2)"o,,r   c                    | d   }
j                   j                  j                  |dd      \  }}}}	
j                   j                  j                  |||      |z
  }
j                   j                  j                  |||      |z
  }
j                   j                  j                  |||      |z
  }
j                   j                  j                  |	||      |z
  }	
j                   j                  j	                  ||||	gd      | d<   | S )Nboxes   r+   )axis)r   numpysplitclipconcatenate)bounding_boxesh_endh_startw_endw_startbboxesx1y1x2y2r   s             r   _get_clipped_bboxz>CenterCrop.transform_bounding_boxes.<locals>._get_clipped_bboxU   s    #G,F!\\//55fab5INBB##((We<wFB##((We<wFB##((We<wFB##((We<wFB&*ll&8&8&D&DRR r 'E 'N7# "!r   r   xyxy)sourcetargetr   r   r         rel_xyxy)r8   r   r   bounding_box_format)	r   rI   r   r   intfloatmaxminr   )r   r8   r%   r   r/   rB   r   init_height
init_widthh_diffw_diffr:   r<   r9   r;   r   r   target_heighttarget_widthcrop_height
crop_widthcrop_box_hstartcrop_box_wstarts   `                      r   transform_bounding_boxesz#CenterCrop.transform_bounding_boxesI   s'   	-
	" %]3"3K"@Z'++
 t{{*djj(Q;6Q;&1*oG&1*oGdkk)Edjj(E.wwN E F KKM::LeEM$9:\IJKc&+6:KU6L#89MIJJS
3Q7J!%(<"="ABO!%
(:";a"?@O%G%G#k1E#j0E.wwN ,!" N ,!{{jjN ,);;** &	
 (++;;**
 r   c                 *    | j                  |||      S )N)r   )transform_images)r   segmentation_masksr%   r   s       r   transform_segmentation_masksz'CenterCrop.transform_segmentation_masks   s#     $$ % 
 	
r   c                    | j                   j                  || j                        }| j                  dk(  r|j                  d   }|j                  d   }n|j                  d   }|j                  d   }||t        d|j                         || j                  z
  }|| j                  z
  }t        |dz        }	t        |dz        }
|dk\  r|dk\  rt        |j                        dk(  re| j                  dk(  r+|d d d d |	|	| j                  z   |
|
| j                  z   f   S |d d |	|	| j                  z   |
|
| j                  z   d d f   S t        |j                        d	k(  r_| j                  dk(  r(|d d |	|	| j                  z   |
|
| j                  z   f   S ||	|	| j                  z   |
|
| j                  z   d d f   S t        j                  || j                  | j                  g| j                  | j                   
      S )Nr)   r*   r+   r,   zeAt this time, CenterCrop can only process images with a static spatial shape. Received: inputs.shape=rF   r   r2      )r   backend_module)r   castcompute_dtyper   r   
ValueErrorr   r   rJ   lenr   smart_resize)r   r   r%   r   inputsrN   rO   rP   rQ   r:   r<   s              r   rZ   zCenterCrop.transform_images   s6   ""64+=+=>// ,,r*Kb)J ,,r*Kb)J*"4117@  t{{*djj(fqj/fqj/Q;6Q;6<< A%##'77!'DKK"77'DJJ"668  g33g

22  V\\"a'##'77!'DKK"77'DJJ"668 
 g33g

22 
 ''[[$**%((<<	
 	
r   c                    t        |      }t        |d   t         t        f      st        |      dvrt	        d      t        |      dk(  ra| j
                  dk(  r)| j                  |d<   | j                  |d<   t        |      S | j                  |d<   | j                  |d<   t        |      S | j
                  dk(  r)| j                  |d<   | j                  |d<   t        |      S | j                  |d<   | j                  |d<   t        |      S )	Nr   )r^   r2   zc`input_shape` must be a non-nested tuple or list of rank-1 with size 3 (unbatched) or 4 (batched). r2   channels_lastrG   rF   r^   )listr   tuplerc   rb   r   r   r   )r   r   s     r   compute_output_shapezCenterCrop.compute_output_shape   s   ;'k!ntUm49
9 E  {q ?2!%A!%A [!! "&A!%A [!! ?2!%A!%A [!! "&A!%A[!!r   c                 t    t         |          }| j                  | j                  | j                  d}i ||S )N)r   r   r   )r   
get_configr   r   r   )r   base_configconfigr   s      r   rl   zCenterCrop.get_config
  s@    g(*kkZZ++

 )+(((r   r#   )TN)T)NT)__name__
__module____qualname____doc___USE_BASE_FACTORr   r!   r&   rX   r\   rZ   rj   rl   __classcell__)r   s   @r   r	   r	      sL    %N 
& 8<fR <@
7
r"2) )r   r	   N)
keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   Lkeras.src.layers.preprocessing.image_preprocessing.bounding_boxes.convertersr   r   keras.src.utilsr   r	   r   r   r   <module>ry      sA    - ( '(B), B) )B)r   