
    ]jgJ              	          d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZ d dlmZ d dlZd dlZ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j@                  e!      Z"ddhZ#dhZ$ edejJ                  jM                  ejN                              Z( ejR                  d      df ejR                  d      df ejR                  d      df ejR                  d      df ejR                  d      df ejR                  d      df ejR                  d      dfge_*         G d de jV                        Z,d Z-d  Z.d0d!Z/	 	 	 	 	 d1d"Z0d# Z1d$ Z2d% Z3d& Z4d' Z5d( Z6d) Z7d2d*Z8d+ Z9d, Z:d- Z;d3d.Z<d/ Z=y)4    N)defaultdict)deepcopy)
itemgetter)urlparse)Image)etree)TreebankWordTokenizer)get_env)safe_build_pathLabelChoiceFilterLOCAL_FILES_DOCUMENT_ROOT)defaultz([:,])([^\d])z \1 \2z([:,])$z \1 z\.\.\.z ... z	[;@#$/%&]z \g<0> z([^\.])(\.)([\]\)}>"\']*)\s*$z\1 \2\3 z[?!]z([^'])' z\1 ' c                       e Zd ZddZy)ExpandFullPathNc           	          t        || j                  t        j                  j	                  t        j                  j                  |                   y N)setattrdestospathabspath
expanduser)selfparser	namespacevaluesoption_strings        J/root/env/lib/python3.12/site-packages/label_studio_sdk/converter/utils.py__call__zExpandFullPath.__call__3   s.    	499bggoobgg6H6H6P&QR    r   )__name__
__module____qualname__r!    r"   r    r   r   2   s    Sr"   r   c                     d}g }| j                         D ]7  }t        |      r%|j                  ||f       |t        |      dz   z  }3|dz  }9 |S )Nr      )splitlenappend)text	tok_startouttoks       r    tokenizer0   7   s[    I
Czz| s8JJY'(SA%INI Jr"   c                    t               j                  |       D cg c]  }| }}|D cg c]  \  }}| || |f }}}|rGt        |D cg c]*  }|j                  d      d uxr |j                  d      d u, c}      rt	        t        |t        d                  }|j                  d      }|d   }|d   dz
  }	d}
g g }}|D ]  \  }}|j                  |       |t        |      z   dz
  }|}||	kD  r+|r)|j                  d      }|d   }|d   dz
  }	d}
||	k  rn|r)|r||k  s|j                  d      s|j                  d       ||k  r"|	|k\  r|j                  |
|d   d   z          d	}
|j                  d        ||fS |D cg c]  \  }}|	 }}}dgt        |      z  }||fS c c}w c c}}w c c}w c c}}w )
Nstartend)keyr   r(   zB-labelsOzI-)
r	   span_tokenizeallgetlistsortedr   popr+   r*   )r,   spanstokentoken_index_tuplesr2   r3   tokens_and_idxspan
span_startspan_endprefixtokenstagstoken_start	token_endtoken_start_ind_s                    r    create_tokens_and_tagsrK   C   s    12@@F  BTT:5#tE#.TNT 	
 HHWT)IdhhuoT.II	
 VEz'':;<yy|']
;?2"0 	!E;MM% c%j(1,  *O
 ) 99Q<D!%gJ#E{QH!F"h.  9z1(9KC y(X-HFT(^A%667C 9	!B 4< )77HE1%77us6{"4<c U	
R 8s   	F1F6/F<Gc                     |r|S t         j                  j                  d      }|sC| rAt         j                  j	                  | d      }t         j                  j                  |      sd}|st        d      |S )zIReturn either upload_dir, or path by LS_UPLOAD_DIR, or project_dir/uploadLS_UPLOAD_DIRuploadNzTCan't find upload dir: either LS_UPLOAD_DIR or project should be passed to converter)r   environr9   r   joinexistsFileNotFoundError)project_dir
upload_dirs     r    _get_upload_dirrU   y   sf    0J+WW\\+x8
ww~~j)Jb
 	
 r"   c                 
   | j                  d      xr d| v }| j                  d      }|rt        ||      }t        j                  j	                  | j                  dd            }t        ||      }	t        j                  d|	 d| j                  |	|             |rt        j                  |	|       |rZt        j                  j                  t        j                  j                  |      t        j                  j                  |            S |	S |r| j!                  dd	      d
   j!                  d      \  }}
t#        t        j                  j	                  |
            }
t        t$        |
      }	t        j                  j'                  |	      st)        |	      |rt        j                  |	|       |	S |t        j                  j+                  t        j                  j                  t-        |       j                              \  }}| | }t        j                  j                  ||      }	t        j                  j'                  |	      r|dz   t/        j0                  | j3                         t#        t4        j4                  j7                         j9                               j3                         z         j;                         d d z   |z   }t        j                  j                  ||      }	t        j                  j'                  |	      st        j=                  dj                  | |	             |r`t?        j@                  |       }|jC                          tE        jF                  |	d      5 }|jI                  |jJ                         d d d        |rZt        j                  j                  t        j                  j                  |      t        j                  j                  |            S |	S # 1 sw Y   gxY w)Nz/data/z?d=z/data/uploadz/data/upload/ zCopy z to )filepath
output_dirr(   rJ      zDownload {url} to {filepath})urlrX   wbmode)&
startswithrU   urllibparseunquotereplacer   loggerdebugformatshutilcopyr   r   rP   basenamer)   strr   rQ   rR   splitextr   hashlibmd5encodedatetimenow	timestamp	hexdigestinforequestsr9   raise_for_statusioopenwritecontent)r\   rY   filenamerS   return_relative_pathrT   download_resourcesis_local_fileis_uploaded_filerX   dir_pathrj   extrfouts                  r    downloadr      s    NN8,=#M~~n5$[*=
<<''OR(HI":x8H:T*.55!j 6 	

 KK*-77<<  ,bgg.>.>x.H   YYx3B7==eD(v||++H56"#<hGww~~h'#H--KK*-(()9)9(3-:L:L)MN#Zu%77<<
H577>>(#++JJL3x'8'8'<'<'>'H'H'J#K#R#R#TT)+bq""
   ww||J1H77>>(#299cH9UVS!A - &

199%&ww||BGG,,Z8"'':J:J8:TUUO	& &s   7O99Pc                 @    t        j                  |       j                  S r   )r   rx   size)
image_paths    r    get_image_sizer      s    ::j!&&&r"   c                     t        j                  |       }|j                  \  }}t        |j	                               }|||fS r   )r   rx   r   r*   getbands)r   iwhcs        r    get_image_size_and_channelsr      s8    

:A66DAqAJJLAa7Nr"   c                     t        j                  | d      5 }|j                         t        |j	                               z  cd d d        S # 1 sw Y   y xY w)Nr   r^   )waverx   
getnframesfloatgetframerate)
audio_pathfs     r    get_audio_durationr      sA    	:C	( 8A||~ann&6 778 8 8s   *AAc                 n    t         j                  j                  |       st        j                  |        y y r   )r   r   rQ   makedirs)r   s    r    
ensure_dirr      s#    77>>(#
H $r"   c           	         | si S d }d }d }	 t        j                  |       }i i t        t              }}}|j                         D ]  }	 ||	      r|	j                  |	j                  d   j                  d      d}
i }|	j                  j                  d      d	k(  r|	j                  j                  d
      rd|	j                  d
   d}n[|	j                  j                  d      rd|	j                  d   d}n-|	j                  j                  d      rd|	j                  d   d}|r||
d<   |
||	j                  d   <   nC ||	      r;|	j                  |	j                  d   j                  d      d||	j                  d   <   |	j                  t        vrQ ||	|      }|^|	j                  j                  d      xs |	j                  j                  d      }|sMt        j                  dj!                  t        j"                  |	d      j%                         dd              t        |	j                        ||   |<    |j'                         D ]e  \  }}
g |
d<   |
d   D ]:  }||vrt        j                  d| d| d        $|
d   j)                  ||          < t+        ||         |
d!<   ||   |
d"<   g |S # t         j                  $ r}t        t	        |            d}~ww xY w)#a  
    :param config_string: Label config string
    :return: structured config of the form:
    {
        "<ControlTag>.name": {
            "type": "ControlTag",
            "to_name": ["<ObjectTag1>.name", "<ObjectTag2>.name"],
            "inputs: [
                {"type": "ObjectTag1", "value": "<ObjectTag1>.value"},
                {"type": "ObjectTag2", "value": "<ObjectTag2>.value"}
            ],
            "labels": ["Label1", "Label2", "Label3"] // taken from "alias" if exists or "value"
    }
    c                 r    | j                   j                  d      xr | j                   j                  d      S )Nnamevalue)attribr9   tags    r    _is_input_tagz#parse_config.<locals>._is_input_tag   s'    zz~~f%A#**..*AAr"   c                     | j                   j                  d      xr/ | j                   j                  d      xr | j                  t        vS )Nr   toName)r   r9   r   _NOT_CONTROL_TAGSr   s    r    _is_output_tagz$parse_config.<locals>._is_output_tag   s>    JJNN6" 1

x(100	
r"   c                 r    | }	 |j                         }|y |j                  j                  d      }||v r|S 5)Nr   )	getparentr   r9   )r   outputsparentr   s       r    _get_parent_output_tag_namez1parse_config.<locals>._get_parent_output_tag_name   sE    %%'F~==$$V,Dw r"   Nr   ,)typeto_name	perRegiontruewhenTagNamer   )r   r   whenLabelValuelabelwhenChoiceValuechoiceconditionalsr   r   $)r   r   aliaszDInspecting tag {tag_name}... found no "value" or "alias" attributes.unicodeencoding2   )tag_nameinputsr   zto_name=z" is specified for output tag name=z', but we can't find it among input tagsr5   labels_attrs)r   
fromstringXMLSyntaxError
ValueErrorrk   r   dictiterr   r   r)   r9   lstrip_LABEL_TAGSre   rf   rg   tostringstripitemsr+   r:   )config_stringr   r   r   xml_treeer   r   r5   r   tag_infor   parent_nameactual_value
output_taginput_tag_names                   r    parse_configr      s    	B
	!##M2 !"k$&7VGF}} &E# #CJJx4H4N4Ns4STHLzz~~k*f4::>>-0,13::m;T#ULZZ^^$45 ' #

+; <$L ZZ^^$56 ( #

+< =$L +7(*2GCJJv&'3G,33C8*F3::f%& 77+%1#w?"::>>'2McjjnnW6MLZaa!&i!H!N!N!PQTRT!U b  594D{#L1M&EN !( 6
H&y1 	>NV+~..PQ[P\ ]< < X%%f^&<=	> "&"45#)*#5 6 No  !Q  !s   K K0K++K0c                    t        |       t        |      k(  sJ t        dt        j                  t        j                  | t        j
                  |d            t        j                  |t        j
                  | d            z
        z        S )z.https://en.wikipedia.org/wiki/Shoelace_formulag      ?r(   )r*   r   npabsdotroll)xys     r    get_polygon_arear   C  sb     q6SVrvvbffQ162771a=9QQRRSSr"   c                     t        |       t        |      k(  sJ t        |       t        |      t        |       t        |      f\  }}}}||||z
  ||z
  gS r   )r*   minmax)r   r   x1y1x2y2s         r    get_polygon_bounding_boxr   K  sU    q6SVVSVSVSV3NBBBGR"W%%r"   c                     | d   }t        |t              r|j                  d|      }|S t        |t              r|r|S t	        |      S )z)Get annotator id or email from annotationcompleted_byemail)
isinstancer   r9   intrk   )itemr   int_id	annotators       r    get_annotatorr   R  sH    ^$I)T"MM'73	)S!fy>r"   c                     d}t        | dd      5 }|dk7  rJ|j                  d      }|j                         r'|dk(  r
	 d d d        y|d	k(  r
	 d d d        y
	 d d d        y	 d d d        y# 1 sw Y   yxY w)Nr   r   zutf-8r   rW   r(   {r   [r:   invalidempty)rx   readisspace)r{   charr   s      r    get_json_root_typer   _  s    D	hg	. !bj66!9D ||~ s{  s{ $ % * +* s   .A+A+A++A4c                    g }d}| D ]  }t        |      }|j                  d      }|dk(  r)t        |d         dk(  r|j                  |d   d          M|dk(  r)t        |d         dk(  r|j                  |d   d          {|d	v r"|j                  |j	                  d
|             |j                  |        |dv rt        |      dk(  r|d   S |S )z
    :param v: list of regions or results
    :return: label name as is if there is only 1 item in result `v`, else list of label names
    Nr   Choiceschoicesr(   r   TextArear,   )Chatchatmessager   )r   r   )r   r<   r*   r+   r9   )vr.   tag_typer   js        r    prettify_resultr   y  s    
 CH 
QK55=y S9%6!%;JJq|A'#AfI!(;JJqy|$00JJquu]A./JJqM
 !88SX]3q6SPSSr"   c                     d| v rd| v rd| v rd| v sy	 | d   }| d   }| d   }| d   }||||y||dz  z   dz  }||dz  z   dz  }|dz  }|dz  }||||fS # t         t        f$ r Y yw xY w)a  
    Convert LS annotation to Yolo format.

    Args:
        label (dict): Dictionary containing annotation information including:
            - width (float): Width of the object.
            - height (float): Height of the object.
            - x (float): X-coordinate of the top-left corner of the object.
            - y (float): Y-coordinate of the top-left corner of the object.

    Returns:
        tuple or None: If the conversion is successful, returns a tuple (x, y, w, h) representing
        the coordinates and dimensions of the object in Yolo format, where (x, y) are the center
        coordinates of the object, and (w, h) are the width and height of the object respectively.
    r   r   widthheightN   d   )	TypeErrorr   )r   r   r   r   r   s        r    convert_annotation_to_yolor    s    " 5LSE\g.>8uCT#J#J'N(O9	QY!)QY#QY#GG aA: z" s   A  A A('A(c                    d| v rd| v rd| v rd| v rd| v rd| v rd| v sy| d   | d   }}| d   d	z  |z  }| d   d	z  |z  }| d   d	z  |z  }| d   d	z  |z  }t        j                  | j                  dd
            }t        j                  |      t        j                  |      }
}	||f|||	z  z   |||
z  z   f|||	z  z   ||
z  z
  |||
z  z   ||	z  z   f|||
z  z
  |||	z  z   fg}|r|D cg c]  }|d
   |z  |d   |z  f c}S |S c c}w )av  
    Convert LS annotation to Yolo OBB format.

    Args:
        label (dict): Dictionary containing annotation information including:
            - original_width (int): Original width of the image.
            - original_height (int): Original height of the image.
            - x (float): X-coordinate of the top-left corner of the object in percentage of the original width.
            - y (float): Y-coordinate of the top-left corner of the object in percentage of the original height.
            - width (float): Width of the object in percentage of the original width.
            - height (float): Height of the object in percentage of the original height.
            - rotation (float, optional): Rotation angle of the object in degrees (default is 0).
        normalize (bool, optional): Whether to normalize the coordinates to the range [0, 1] (default is True).

    Returns:
        list of tuple or None: List of tuples containing the coordinates of the object in Yolo OBB format.
            Each tuple represents a corner of the bounding box in the order:
            (top-left, top-right, bottom-right, bottom-left).
    original_widthoriginal_heightr   r   r   r  rotationNr  r   r(   )mathradiansr9   cossin)r   	normalize	org_width
org_heightr   r   r   r   r	  r  r  coordscoords                r    convert_annotation_to_yolo_obbr    st   , 	E!&5L5Lu%!"23U;L5MzIc
S9$Ac
S:%Agy(Ah#
*A||EIIj!45Hxx!488H#5C 
A	
QWa!c'k"	
QWq3w	AGa#g 56	
QWa!c'k"	F KQR%qI%uQx*'<=RR Ss   'Dc                    t        j                  | t         j                        j                  d      }t        j                  |dddf         }t        j                  |dddf         }t         j
                  j                  |d   |d   z
        }t         j
                  j                  |d   |d   z
        }|d   |d   z
  }|d	   |d
   z
  }	t        j                  t        j                  |	|            }
||dz  t        j                  t        j                  |
            z  z
  |dz  t        j                  t        j                  |
            z  z   }||dz  t        j                  t        j                  |
            z  z
  |dz  t        j                  t        j                  |
            z  z
  }||z  dz  }||z  dz  }||z  dz  }||z  dz  }|||||
||dS )ai  
    Convert YOLO Oriented Bounding Box (OBB) format to Label Studio format.

    Args:
        xyxyxyxy (list): List of 8 float values representing the absolute pixel coordinates
                         of the OBB in the format [x1, y1, x2, y2, x3, y3, x4, y4].
        original_width (int): Original width of the image.
        original_height (int): Original height of the image.

    Returns:
        dict: Dictionary containing the converted bounding box with the following keys:
              - x: X-coordinate of the top-left corner of the bounding box in percentage.
              - y: Y-coordinate of the top-left corner of the bounding box in percentage.
              - width: Width of the bounding box in percentage.
              - height: Height of the bounding box in percentage.
              - rotation: Rotation angle of the bounding box in degrees.
    )dtype)r[   r  Nr   r(      )r(   r   )r   r   )r(   r(   )r   r(   r  r  )r   r   r   r  r	  r  r  )r   arrayfloat64reshapemeanlinalgnormdegreesarctan2r  r  r  )xyxyxyxyr  r  r  center_xcenter_yr   r  dxdyr   
top_left_x
top_left_yr   r   s                  r    convert_yolo_obb_to_annotationr&    s   & XXhbjj199&AF wwvad|$Hwwvad|$H IINN6!9vay01EYY^^F1Iq	12F 
t	$B	t	$B


2::b"%&A 	19rzz!}-
-	.A:

1.
.	/  	19rzz!}-
-	.A:

1.
.	/  
n	$+A	o	%,A^#s*E&#-F (* r"   )NN)NNFNT)NF)T)>argparserp   rm   rw   loggingr
  r   rerh   ra   r   collectionsr   ri   r   operatorr   urllib.parser   numpyr   ru   PILr   lxmlr   nltk.tokenize.treebankr	   Alabel_studio_sdk._extensions.label_studio_tools.core.utils.paramsr
   =label_studio_sdk._extensions.label_studio_tools.core.utils.ior   	getLoggerr#   re   r   r   r   r   sepr   compilePUNCTUATIONActionr   r0   rK   rU   r   r   r   r   r   r   r   r   r   r   r   r  r  r&  r&   r"   r    <module>r8     s      	   	 	    #   !     8 U Y			8	$!  $)@ 
 RZZ !9-RZZ
W%RZZ	H%RZZz*

34 RZZ*%RZZh'%  !SX__ S
	3l& ?D'8

`FT&
4T*"J4n=r"   