
    qi                         d dl Z d dlmZ ddlmZ ddlmZmZmZ  e       r
d dl Z d dl m	Z	  ej                  e      Z	 ddeeef   ded	edz  d
edeeef   f
dZ G d de      ZdgZy)    N)DetrImageProcessorFast   )center_to_corners_format)
TensorTypeis_torch_availablelogging)nn
image_sizesizemax_sizemod_sizereturnc                    | \  }}d}|St        t        ||f            }t        t        ||f            }||z  |z  |kD  r||z  |z  }t        t	        |            }||k  r*|}	||t        ||z  |z        }
nTt        ||z  |z        }
nB||k  r||k(  s
||k  r
||k(  r||}	}
n)|}
||t        ||z  |z        }	nt        ||z  |z        }	|xt        j                  t        j                  |	      |      j                         }t        j                  t        j                  |
      |      j                         }|	|z
  }	|
|z
  }
|
|	fS )a  
    Computes the output image size given the input image size and the desired output size with multiple of divisible_size.

    Args:
        image_size (`tuple[int, int]`):
            The input image size.
        size (`int`):
            The desired output size.
        max_size (`int`, *optional*):
            The maximum allowed output size.
        mod_size (`int`, *optional*):
            The size to make multiple of mod_size.
    N)	floatminmaxintroundtorch	remaindertensoritem)r
   r   r   r   heightwidthraw_sizemin_original_sizemax_original_sizeowohow_modoh_mods                Y/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/yolos/modular_yolos.pyget_size_with_aspect_ratior#      sn     MFEH!#vuo"67!#vuo"670047(B"336GGHuX'Dv~H$8X&./BTF]U*+B
E/fn%6/etmBH$8X%./BTE\F*+Bb!18<AACb!18<AAC&[&[8O    c                   >    e Zd Z	 ddedeee   z  fdZd Zd Z	d Z
y)	YolosImageProcessorFastN	thresholdtarget_sizesc                    |j                   |j                  }}|"t        |      t        |      k7  rt        d      t        j
                  j                  |d      }|dddf   j                  d      \  }}t        |      }	|t        |t              rMt        j                  |D 
cg c]  }
|
d   	 c}
      }t        j                  |D 
cg c]  }
|
d   	 c}
      }n|j                  d      \  }}t        j                  ||||gd      j                  |	j                         }|	|dddddf   z  }	g }t#        |||	      D ]3  \  }}}|||kD     }|||kD     }|||kD     }|j%                  |||d       5 |S c c}
w c c}
w )	a  
        Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
        bottom_right_x, bottom_right_y) format. Only supports PyTorch.

        Args:
            outputs ([`YolosObjectDetectionOutput`]):
                Raw outputs of the model.
            threshold (`float`, *optional*):
                Score threshold to keep object detection predictions.
            target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*):
                Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size
                `(height, width)` of each image in the batch. If unset, predictions will not be resized.
        Returns:
            `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
            in the batch as predicted by the model.
        NzTMake sure that you pass in as many target sizes as the batch dimension of the logits.r      )dim)scoreslabelsboxes)logits
pred_boxeslen
ValueErrorr	   
functionalsoftmaxr   r   
isinstancelistr   Tensorunbindstacktodevicezipappend)selfoutputsr'   r(   
out_logitsout_bboxprobr-   r.   r/   iimg_himg_w	scale_fctresultsslbscorelabelboxs                        r"   post_process_object_detectionz5YolosImageProcessorFast.post_process_object_detectionG   s   &  '~~w/A/AH
#:#l"33 j  }}$$Z4c3B3h++B/ )2 #,-L%Aqad%ABL%Aqad%AB+2215uUE5%$@aHKKELLYIIaqj11E6651 	MGAq!a)m$Ea)m$EA	M"CNNeusKL		M  &B%As   (F Fc                     t        d      )NzHSegmentation post-processing is not implemented for Deformable DETR yet.NotImplementedErrorr?   s    r"   "post_process_instance_segmentationz:YolosImageProcessorFast.post_process_instance_segmentation|   s    !"lmmr$   c                     t        d      )NzQSemantic segmentation post-processing is not implemented for Deformable DETR yet.rQ   rS   s    r"   "post_process_semantic_segmentationz:YolosImageProcessorFast.post_process_semantic_segmentation       !"uvvr$   c                     t        d      )NzQPanoptic segmentation post-processing is not implemented for Deformable DETR yet.rQ   rS   s    r"   "post_process_panoptic_segmentationz:YolosImageProcessorFast.post_process_panoptic_segmentation   rW   r$   )g      ?N)__name__
__module____qualname__r   r   r7   tuplerO   rT   rV   rY    r$   r"   r&   r&   F   s6    X\3"'3=G$u+=U3jnwwr$   r&   )N   )r   3transformers.models.detr.image_processing_detr_fastr   image_transformsr   utilsr   r   r   r	   
get_loggerrZ   loggerr]   r   r#   r&   __all__r^   r$   r"   <module>rf      s     V 8   
		H	% Z\.c3h.'*.69Dj.SV.
38_.b=w4 =w@ %
%r$   