L i!ddlmZmZmZmZddlmZmZmZm Z m Z ddl m Z m Z erddlmZer ddlZddlmZmZerdd lmZe j,eZee d Gd d e Zy)) TYPE_CHECKINGAnyUnionoverload)add_end_docstringsis_torch_availableis_vision_availableloggingrequires_backends)Pipelinebuild_pipeline_init_args) load_imageN)(MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES)ImageT)has_image_processorc 2eZdZdZdZdZdZdZfdZdZ e de e dfd e d e d eee e ffd Ze de ee edfd e d e d eeee e ffd Zd e eee e feeee e ffffd ZddZdZddZddd ee effdZxZS)ObjectDetectionPipelinea Object detection pipeline using any `AutoModelForObjectDetection`. This pipeline predicts bounding boxes of objects and their classes. Example: ```python >>> from transformers import pipeline >>> detector = pipeline(model="facebook/detr-resnet-50") >>> detector("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png") [{'score': 0.997, 'label': 'bird', 'box': {'xmin': 69, 'ymin': 171, 'xmax': 396, 'ymax': 507}}, {'score': 0.999, 'label': 'bird', 'box': {'xmin': 398, 'ymin': 105, 'xmax': 767, 'ymax': 507}}] >>> # x, y are expressed relative to the top left hand corner. ``` Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial) This object detection pipeline can currently be loaded from [`pipeline`] using the following task identifier: `"object-detection"`. See the list of available models on [huggingface.co/models](https://huggingface.co/models?filter=object-detection). FTNct||i||jdk(rtd|jdt |dt j}|jt|j|y)NtfzThe z is only available in PyTorch.vision) super__init__ framework ValueError __class__r rcopyupdatercheck_model_type)selfargskwargsmappingrs m/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/pipelines/object_detection.pyrz ObjectDetectionPipeline.__init__8sn $)&) >>T !tDNN#33QRS S$):??ACD g&c Di}d|vr|d|d<i}d|vr|d|d<|i|fS)Ntimeout threshold)r"r$preprocess_paramspostprocess_kwargss r&_sanitize_parametersz,ObjectDetectionPipeline._sanitize_parametersCsL  +1)+< i ( & .4[.A { + "&888r'imagez Image.Imager#r$returncyNr+r"r/r#r$s r&__call__z ObjectDetectionPipeline.__call__Lsmpr'cyr2r+r3s r&r4z ObjectDetectionPipeline.__call__Os&)r'cZd|vrd|vr|jd|d<t||i|S)ai Detect objects (bounding boxes & classes) in the image(s) passed as inputs. Args: inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing an HTTP(S) link pointing to an image - A string containing a local path to an image - An image loaded in PIL directly The pipeline accepts either a single image or a batch of images. Images in a batch must all be in the same format: all as HTTP(S) links, all as local paths, or all as PIL images. threshold (`float`, *optional*, defaults to 0.5): The probability necessary to make a prediction. timeout (`float`, *optional*, defaults to None): The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and the call may block forever. Return: A list of dictionaries or a list of list of dictionaries containing the result. If the input is a single image, will return a list of dictionaries, if the input is a list of several images, will return a list of list of dictionaries corresponding to each image. The dictionaries contain the following keys: - **label** (`str`) -- The class label identified by the model. - **score** (`float`) -- The score attributed by the model for that label. - **box** (`list[dict[str, int]]`) -- The bounding box of detected object in image's original size. imagesinputs)poprr4)r"r#r$rs r&r4z ObjectDetectionPipeline.__call__Ts=@ v (&"8%zz(3F8 w000r'cJt||}tj|j|jgg}|j |gd}|j dk(r|j|j}|j|j|d|dd}||d<|S)N)r)pt)r7return_tensorswordsboxes)textr>r< target_size) rtorch IntTensorheightwidthimage_processorrtodtype tokenizer)r"r/r)r@r8s r& preprocessz"ObjectDetectionPipeline.preprocessxs5'2oo ekk'B&CD %%eWT%J >>T !YYtzz*F >> %^^w`d^eF +} r'c|jd}|jdi|}|jd|i|}|j|d|d<|S)Nr@bboxr+)r9modelrrH)r" model_inputsr@outputs model_outputss r&_forwardz ObjectDetectionPipeline._forwards^"&&}5 $**,|,))=+*Q*QR >> %$0$8M& !r'c 6|d}j|dj\fd}|djdjdj d\}}|jDcgc]%}j j j|'}}|djdD cgc] } ||  } } gd} t|j|| D cgc] } | d|kDs tt| | "} } | Sjj|||}|d}|d }|d }|d } |j|d <|Dcgc]3}j j j|j5c}|d <| Dcgc]}j|c}|d <gd} t|d |d |d D cgc]} tt| | } } | Scc}wcc} wcc} wcc}wcc}wcc} w) Nr@rc jtj|dzdz |dzdz |dzdz |dzdz gS)Nrir r)_get_bounding_boxrATensor)rKrCr"rDs r& unnormalizez8ObjectDetectionPipeline.postprocess..unnormalizesi--LL"T!W_t3#d1g-4"T!W_t3#d1g-4   r'logits)dimrK)scorelabelboxscoreslabelsr>)rHtolistsqueezesoftmaxmaxrLconfigid2labelzipdictrEpost_process_object_detectionitemrT)r"rOr*r@rVr]classes predictionr^rKr>keysvals annotationraw_annotationsraw_annotationr[r\rCrDs` @@r& postprocessz#ObjectDetectionPipeline.postprocesss$#M2 >> %(N113MFE ,H5==a@HHRHPTTY[T\OFGOV~~O_`djj''00<`F`3@3H3P3PQR3ST4[&TET,DH0 H>H!8H #HHr\z torch.Tensorc|jdk7r td|jj\}}}}||||d}|S)a% Turns list [xmin, xmax, ymin, ymax] into dict { "xmin": xmin, ... } Args: box (`torch.Tensor`): Tensor containing the coordinates in corners format. Returns: bbox (`dict[str, int]`): Dict containing the coordinates in corners format. r;z9The ObjectDetectionPipeline is only available in PyTorch.)xminyminxmaxymax)rrintr_)r"r\rrrsrtrurKs r&rTz)ObjectDetectionPipeline._get_bounding_boxsS >>T !XY Y!$!1!1!3dD$    r'r2)g?)__name__ __module__ __qualname____doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerrr.rrstrrlistrfr4rIrPrprvrT __classcell__)rs@r&rrs(0O #O '9peC$67ppspW[\`adfiai\jWkpp )49d=&99:)CF)RU) d4S>" #))"15d38n1EtDQUVY[^V^Q_L`Ga1a+b"1H +Z^S#Xr'r)typingrrrrutilsrr r r r baserr image_utilsrrAmodels.auto.modeling_autorrPILr get_loggerrwloggerrr+r'r&rss66kk4(    H %,FGqhqHqr'