


"""simple docstring"""







import importlib
import json
import os
from collections import OrderedDict
from typing import Dict, Optional, Union

# Build the list of all image processors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...image_processing_utils import ImageProcessingMixin
from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)


lowerCamelCase_       :       Optional[int]      						=  logging.get_logger(__name__)

lowerCamelCase_       :       List[str]      						=  OrderedDict(
    [
        ("""align""", """EfficientNetImageProcessor"""),
        ("""beit""", """BeitImageProcessor"""),
        ("""bit""", """BitImageProcessor"""),
        ("""blip""", """BlipImageProcessor"""),
        ("""blip-2""", """BlipImageProcessor"""),
        ("""bridgetower""", """BridgeTowerImageProcessor"""),
        ("""chinese_clip""", """ChineseCLIPImageProcessor"""),
        ("""clip""", """CLIPImageProcessor"""),
        ("""clipseg""", """ViTImageProcessor"""),
        ("""conditional_detr""", """ConditionalDetrImageProcessor"""),
        ("""convnext""", """ConvNextImageProcessor"""),
        ("""convnextv2""", """ConvNextImageProcessor"""),
        ("""cvt""", """ConvNextImageProcessor"""),
        ("""data2vec-vision""", """BeitImageProcessor"""),
        ("""deformable_detr""", """DeformableDetrImageProcessor"""),
        ("""deit""", """DeiTImageProcessor"""),
        ("""deta""", """DetaImageProcessor"""),
        ("""detr""", """DetrImageProcessor"""),
        ("""dinat""", """ViTImageProcessor"""),
        ("""donut-swin""", """DonutImageProcessor"""),
        ("""dpt""", """DPTImageProcessor"""),
        ("""efficientformer""", """EfficientFormerImageProcessor"""),
        ("""efficientnet""", """EfficientNetImageProcessor"""),
        ("""flava""", """FlavaImageProcessor"""),
        ("""focalnet""", """BitImageProcessor"""),
        ("""git""", """CLIPImageProcessor"""),
        ("""glpn""", """GLPNImageProcessor"""),
        ("""groupvit""", """CLIPImageProcessor"""),
        ("""imagegpt""", """ImageGPTImageProcessor"""),
        ("""instructblip""", """BlipImageProcessor"""),
        ("""layoutlmv2""", """LayoutLMv2ImageProcessor"""),
        ("""layoutlmv3""", """LayoutLMv3ImageProcessor"""),
        ("""levit""", """LevitImageProcessor"""),
        ("""mask2former""", """Mask2FormerImageProcessor"""),
        ("""maskformer""", """MaskFormerImageProcessor"""),
        ("""mgp-str""", """ViTImageProcessor"""),
        ("""mobilenet_v1""", """MobileNetV1ImageProcessor"""),
        ("""mobilenet_v2""", """MobileNetV2ImageProcessor"""),
        ("""mobilevit""", """MobileViTImageProcessor"""),
        ("""mobilevit""", """MobileViTImageProcessor"""),
        ("""mobilevitv2""", """MobileViTImageProcessor"""),
        ("""nat""", """ViTImageProcessor"""),
        ("""oneformer""", """OneFormerImageProcessor"""),
        ("""owlvit""", """OwlViTImageProcessor"""),
        ("""perceiver""", """PerceiverImageProcessor"""),
        ("""pix2struct""", """Pix2StructImageProcessor"""),
        ("""poolformer""", """PoolFormerImageProcessor"""),
        ("""regnet""", """ConvNextImageProcessor"""),
        ("""resnet""", """ConvNextImageProcessor"""),
        ("""sam""", """SamImageProcessor"""),
        ("""segformer""", """SegformerImageProcessor"""),
        ("""swiftformer""", """ViTImageProcessor"""),
        ("""swin""", """ViTImageProcessor"""),
        ("""swin2sr""", """Swin2SRImageProcessor"""),
        ("""swinv2""", """ViTImageProcessor"""),
        ("""table-transformer""", """DetrImageProcessor"""),
        ("""timesformer""", """VideoMAEImageProcessor"""),
        ("""tvlt""", """TvltImageProcessor"""),
        ("""upernet""", """SegformerImageProcessor"""),
        ("""van""", """ConvNextImageProcessor"""),
        ("""videomae""", """VideoMAEImageProcessor"""),
        ("""vilt""", """ViltImageProcessor"""),
        ("""vit""", """ViTImageProcessor"""),
        ("""vit_hybrid""", """ViTHybridImageProcessor"""),
        ("""vit_mae""", """ViTImageProcessor"""),
        ("""vit_msn""", """ViTImageProcessor"""),
        ("""xclip""", """CLIPImageProcessor"""),
        ("""yolos""", """YolosImageProcessor"""),
    ]
)

lowerCamelCase_       :       List[Any]      						=  _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES)
def _A						(						lowercase		):







    """simple docstring"""






    for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items():
        if class_name in extractors:
            a					=model_type_to_module_name(_lowercase		)

            a					=importlib.import_module(f'''.{module_name}'''	,   '''transformers.models'''		)
            try:
                return getattr(_lowercase	,   _lowercase		)
            except AttributeError:
                continue

    for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items():
        if getattr(_lowercase	,   '''__name__'''	,   _lowercase		) == class_name:
            return extractor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
    a					=importlib.import_module('''transformers'''		)
    if hasattr(_lowercase	,   _lowercase		):
        return getattr(_lowercase	,   _lowercase		)

    return None
def _A						(						lowercase	,   lowercase = None	,   lowercase = False	,   lowercase = False	,   lowercase = None	,   lowercase = None	,   lowercase = None	,   lowercase = False	,   **lowercase	,   ):







    """simple docstring"""
    a					=get_file_from_repo(
        _lowercase	,   _lowercase	,   cache_dir=_lowercase	,   force_download=_lowercase	,   resume_download=_lowercase	,   proxies=_lowercase	,   use_auth_token=_lowercase	,   revision=_lowercase	,   local_files_only=_lowercase	,   )
    if resolved_config_file is None:
        logger.info(
            '''Could not locate the image processor configuration file, will try to use the model config instead.'''		)
        return {}

    with open(_lowercase	,   encoding='''utf-8'''		) as reader:
        return json.load(_lowercase		)






class 				__A    :






 """simple docstring"""




 def __init__(		self						) ->					Dict:
     raise EnvironmentError(
         '''AutoImageProcessor is designed to be instantiated '''
         '''using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.'''						)


 @classmethod
 @replace_list_option_in_docstrings(lowerCAmelCase__						)
 def 				SCREAMING_SNAKE_CASE							(		cls			,   __A			,   **__A						) ->					str:
     a					=kwargs.pop('''config'''			,   lowerCAmelCase__						)
     a					=kwargs.pop('''trust_remote_code'''			,   lowerCAmelCase__						)
     a					=True

     a					=ImageProcessingMixin.get_image_processor_dict(lowerCAmelCase__			,   **lowerCAmelCase__						)
     a					=config_dict.get('''image_processor_type'''			,   lowerCAmelCase__						)
     a					=None
     if "AutoImageProcessor" in config_dict.get('''auto_map'''			,   {}						):
         a					=config_dict["auto_map"]["AutoImageProcessor"]

     # If we still don't have the image processor class, check if we're loading from a previous feature extractor config
     # and if so, infer the image processor class from there.
     if image_processor_class is None and image_processor_auto_map is None:
         a					=config_dict.pop('''feature_extractor_type'''			,   lowerCAmelCase__						)
         if feature_extractor_class is not None:
             logger.warning(
                 '''Could not find image processor class in the image processor config or the model config. Loading'''
                 ''' based on pattern matching with the model\'s feature extractor configuration.'''						)
             a					=feature_extractor_class.replace('''FeatureExtractor'''			,   '''ImageProcessor'''						)
         if "AutoFeatureExtractor" in config_dict.get('''auto_map'''			,   {}						):
             a					=config_dict["auto_map"]["AutoFeatureExtractor"]
             a					=feature_extractor_auto_map.replace('''FeatureExtractor'''			,   '''ImageProcessor'''						)
             logger.warning(
                 '''Could not find image processor auto map in the image processor config or the model config.'''
                 ''' Loading based on pattern matching with the model\'s feature extractor configuration.'''						)

        # If we don't find the image processor class in the image processor config, let's try the model config.
     if image_processor_class is None and image_processor_auto_map is None:
         if not isinstance(lowerCAmelCase__			,   lowerCAmelCase__						):
             a					=AutoConfig.from_pretrained(lowerCAmelCase__			,   **lowerCAmelCase__						)
         # It could be in `config.image_processor_type``
         a					=getattr(lowerCAmelCase__			,   '''image_processor_type'''			,   lowerCAmelCase__						)
         if hasattr(lowerCAmelCase__			,   '''auto_map'''						) and "AutoImageProcessor" in config.auto_map:
             a					=config.auto_map["AutoImageProcessor"]

     if image_processor_class is not None:
         a					=image_processor_class_from_name(lowerCAmelCase__						)

     a					=image_processor_auto_map is not None
     a					=image_processor_class is not None or type(lowerCAmelCase__						) in IMAGE_PROCESSOR_MAPPING
     a					=resolve_trust_remote_code(
         lowerCAmelCase__			,   lowerCAmelCase__			,   lowerCAmelCase__			,   lowerCAmelCase__						)

     if has_remote_code and trust_remote_code:
         a					=get_class_from_dynamic_module(
             lowerCAmelCase__			,   lowerCAmelCase__			,   **lowerCAmelCase__						)
         a					=kwargs.pop('''code_revision'''			,   lowerCAmelCase__						)
         if os.path.isdir(lowerCAmelCase__						):
             image_processor_class.register_for_auto_class()
         return image_processor_class.from_dict(lowerCAmelCase__			,   **lowerCAmelCase__						)
     elif image_processor_class is not None:
         return image_processor_class.from_dict(lowerCAmelCase__			,   **lowerCAmelCase__						)
     # Last try: we use the IMAGE_PROCESSOR_MAPPING.
     elif type(lowerCAmelCase__						) in IMAGE_PROCESSOR_MAPPING:
         a					=IMAGE_PROCESSOR_MAPPING[type(lowerCAmelCase__						)]
         return image_processor_class.from_dict(lowerCAmelCase__			,   **lowerCAmelCase__						)

     raise ValueError(
         f'''Unrecognized image processor in {pretrained_model_name_or_path}. Should have a '''
         f'''`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following '''
         f'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys()						)}'''						)





 @staticmethod
 def 				SCREAMING_SNAKE_CASE							(		__A			,   __A						) ->					Optional[int]:
     IMAGE_PROCESSOR_MAPPING.register(lowerCAmelCase__			,   lowerCAmelCase__						)



"""simple docstring"""







from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       import torch

       from transformers.modeling_outputs import BaseModelOutput

       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

lowerCamelCase_       :       Dict      						=  logging.get_logger(__name__)






@add_end_docstrings(_SCREAMING_SNAKE_CASE      )
class 				__A    (	_SCREAMING_SNAKE_CASE      ):






 """simple docstring"""




 def __init__(		self			,   **__A						) ->					Dict:
     super().__init__(**__A						)

     if self.framework == "tf":
         raise ValueError(f'''The {self.__class__} is only available in PyTorch.'''						)

     requires_backends(self			,   '''vision'''						)
     self.check_model_type(__A						)


 def __call__(		self			,   __A			,   __A = None			,   **__A			,   ) ->					List[str]:
     if "text_queries" in kwargs:
         a					=kwargs.pop('''text_queries'''						)

     if isinstance(__A			,   (str, Image.Image)						):
         a					={'''image''': image, '''candidate_labels''': candidate_labels}
     else:
         a					=image
     a					=super().__call__(__A			,   **__A						)
     return results


 def 				SCREAMING_SNAKE_CASE							(		self			,   **__A						) ->					Optional[Any]:
     a					={}
     if "threshold" in kwargs:
         a					=kwargs['''threshold''']
     if "top_k" in kwargs:
         a					=kwargs['''top_k''']
     return {}, {}, postprocess_params


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					str:
     a					=load_image(inputs['''image''']						)
     a					=inputs['''candidate_labels''']
     if isinstance(__A			,   __A						):
         a					=candidate_labels.split(''','''						)

     a					=torch.tensor([[image.height, image.width]]			,   dtype=torch.intaa						)
     for i, candidate_label in enumerate(__A						):
         a					=self.tokenizer(__A			,   return_tensors=self.framework						)
         a					=self.image_processor(__A			,   return_tensors=self.framework						)
         yield {
             "is_last": i == len(__A						) - 1,
             "target_size": target_size,
             "candidate_label": candidate_label,
             **text_inputs,
             **image_features,
         }


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					List[Any]:
     a					=model_inputs.pop('''target_size'''						)
     a					=model_inputs.pop('''candidate_label'''						)
     a					=model_inputs.pop('''is_last'''						)

     a					=self.model(**__A						)

     a					={'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs}
     return model_outputs


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A=0.1			,   __A=None						) ->					List[str]:
     a					=[]
     for model_output in model_outputs:
         a					=model_output['''candidate_label''']
         a					=BaseModelOutput(__A						)
         a					=self.image_processor.post_process_object_detection(
             outputs=__A			,   threshold=__A			,   target_sizes=model_output['''target_size''']						)[0]

         for index in outputs["scores"].nonzero():
             a					=outputs['''scores'''][index].item()
             a					=self._get_bounding_box(outputs['''boxes'''][index][0]						)

             a					={'''score''': score, '''label''': label, '''box''': box}
             results.append(__A						)

     a					=sorted(__A			,   key=lambda __A						: x["score"]			,   reverse=__A						)
     if top_k:
         a					=results[:top_k]

     return results





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					Dict[str, int]:
     if self.framework != "pt":
         raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.'''						)
     a		,				a		,				a		,				a					=box.int().tolist()
     a					={
         '''xmin''': xmin,
         '''ymin''': ymin,
         '''xmax''': xmax,
         '''ymax''': ymax,
     }
     return bbox