
    9i6                     H   S SK r S SKrS SKJrJr  S SKrS SKrS SKrS SK	J
r
JrJrJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S SKJrJrJr  S S	KJr  S S
KJr  S SKJ r   S\l!        \ " 5       r"S/r#\RH                  " \RJ                  \RL                  S9 " S S\5      5       r'g)    N)AnyDict)Image	ImageDraw	ImageFile	ImageFont)	Pipelines)
OutputKeys)InputPipeline)	PIPELINES)_get_anchorsgeneratepost_process)	LoadImage)Tasks)
get_loggerTTBSDetectionPipeline)module_namec                      ^  \ rS rSrSrS/SSSS.r\S 5       rS	\4U 4S
 jjr	S\
S\\\4   4S jrS\\\4   S\\\4   4S jrS\\\4   S\\\4   4S jrSrU =r$ )r      a  TBS Detection Pipeline.

Example:

```python
>>> from modelscope.pipelines import pipeline

>>> tbs_detect = pipeline(Tasks.image_object_detection, model='landingAI/LD_CytoBrainCerv')
>>> tbs_detect(input='data/test/images/tbs_detection.jpg')
   {
    "boxes": [
        [
        446.9007568359375,
        36.374977111816406,
        907.0919189453125,
        337.439208984375
        ],
        [
        454.3310241699219,
        336.08477783203125,
        921.26904296875,
        641.7871704101562
        ]
    ],
    "labels": [
        ["Positive"]
    ],
    "scores": [
        0.9296008944511414,
        0.9260380268096924
    ]
    }
>>> #
```
positive)  r      g      ?g333333?)class_namesmodel_image_size
confidenceiouc                 N    XR                   ;   a  U R                   U   $ SU-   S-   $ )NzUnrecognized attribute name '')	_defaults)clsns     n/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/cv/tbs_detection_pipeline.pyget_defaults!TBSDetectionPipeline.get_defaultsK   s+    ==##2Q6<<    modelc                    > [         TU ]  " SUSS.UD6  U R                  R                  U R                  5        [        U 5      U l        [        U 5        g)z$
model: model id on modelscope hub.
F)r(   auto_collateN )super__init____dict__updater!   r   anchorsr   )selfr(   kwargs	__class__s      r$   r-   TBSDetectionPipeline.__init__R   sE     	Cu5CFCT^^,#D)r'   inputreturnc                 d    [         R                  " U5      nUR                  [        5      nX!S.nU$ )a  
Detect objects (bounding boxes) in the image(s) passed as inputs.

Args:
    input (`Image` or `List[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 or opencv directly

        The pipeline accepts either a single image or a batch of images. Images in a batch must all be in the
        same format.


Return:
    A dictionary of result or a list of dictionary of result. If the input is an image, a dictionary
    is returned. If input is a list of image, a list of dictionary is returned.

    The dictionary contain the following keys:

    - **scores** (`List[float]`) -- The detection score for each card in the image.
    - **boxes** (`List[float]) -- The bounding boxe [x1, y1, x2, y2] of detected objects in in image's
        original size.
    - **labels** (`List[str]`, optional) -- The boxes's class_names of detected object in image.
)imgimg_path)r   convert_to_ndarrayastypefloat)r1   r5   r8   results       r$   
preprocessTBSDetectionPipeline.preprocess[   s/    6 **51jj0r'   c                 2   US   R                  [        R                  5      n[        R                  " US5      nUR                  [        R
                  5      n[        R                  " US-  S5      n[        R                  " U5      R                  [        R                  5      n[        R                  " USS9n[        R                  R                  U R                  S5      n[        R                   " USS	9nU" UR#                  5       5      nXaS
   S.nU$ )Nr8   )r   r   g     o@)   r      r   )dimzpytorch_yolov4.ptT)weights_onlyr9   )datar9   )r;   npuint8cv2resizefloat32	transposetorch
from_numpytypeFloatTensor	unsqueezeospathjoinr(   loadcuda)r1   r5   r8   tmp_inp
model_pathr(   outputsr=   s           r$   forwardTBSDetectionPipeline.forward{   s    El!!"((+jjj)jj$,,sU{I6""7+001B1BCoog1-WW\\$**.AB


:D9
#!Z/@Ar'   c                     [        XS   US   5      u  pEUc$  [        R                  / [        R                  / 0nU$ [        R                  UR	                  5       [        R
                  S/[        R                  U0nU$ )NrE   r9   Positive)r   r
   SCORESBOXEStolistLABELS)r1   r5   argsr2   bboxesscoresrX   s          r$   postprocess TBSDetectionPipeline.postprocess   sv     &d&M5;LM>!(("j.>.>CGNv}}
|f

 r'   )r0   )__name__
__module____qualname____firstlineno____doc__r!   classmethodr%   strr-   r   r   r   r>   rY   rd   __static_attributes____classcell__)r3   s   @r$   r   r      s    "H #|)	I = =c  $sCx. @T#s(^ S#X c3h !%c3h r'   )(colorsysrQ   typingr   r   rH   numpyrF   rL   PILr   r   r   r   modelscope.metainfor	   modelscope.outputsr
   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   1modelscope.pipelines.cv.tbs_detection_utils.utilsr   r   r   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.loggerr   LOAD_TRUNCATED_IMAGESlogger__all__register_moduleimage_object_detectiontbs_detectionr   r+   r'   r$   <module>r      s     	  
   6 6 ) ) 5 2M M / + ."&	 	!
" 	  i.E.EGu8 uGur'   